aboutsummaryrefslogtreecommitdiff
path: root/2024/04/solve.py
blob: 77c9419cbdc10eb0506a5e0b1c9048d62ffdf116 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/env python


L = [l.strip() for l in open("input").readlines()]


# AAAAAAAAAAAAAAAAAH PYTHON WHY DO YOU SUPPORT -1 INDEX
# THIS COST ME 20 MINUTES
def get(l, idx):
    if idx >= len(l) or idx < 0:
        return "."
    return l[idx]


def part1():
    res = 0

    for y in range(len(L)):
        for x in range(len(L[y])):
            # horizontal
            if (
                get(get(L, y), x) == "X"
                and get(get(L, y), x + 1) == "M"
                and get(get(L, y), x + 2) == "A"
                and get(get(L, y), x + 3) == "S"
            ):
                res += 1

            # horizontal inversed
            if (
                get(get(L, y), x) == "S"
                and get(get(L, y), x + 1) == "A"
                and get(get(L, y), x + 2) == "M"
                and get(get(L, y), x + 3) == "X"
            ):
                res += 1

            # diagonal right
            if (
                get(get(L, y), x) == "X"
                and get(get(L, y + 1), x + 1) == "M"
                and get(get(L, y + 2), x + 2) == "A"
                and get(get(L, y + 3), x + 3) == "S"
            ):
                res += 1

            # diagonal right inversed
            if (
                get(get(L, y), x) == "S"
                and get(get(L, y + 1), x + 1) == "A"
                and get(get(L, y + 2), x + 2) == "M"
                and get(get(L, y + 3), x + 3) == "X"
            ):
                res += 1

            # vertical
            if (
                get(get(L, y), x) == "X"
                and get(get(L, y + 1), x) == "M"
                and get(get(L, y + 2), x) == "A"
                and get(get(L, y + 3), x) == "S"
            ):
                res += 1

            # vertical inversed
            if (
                get(get(L, y), x) == "S"
                and get(get(L, y + 1), x) == "A"
                and get(get(L, y + 2), x) == "M"
                and get(get(L, y + 3), x) == "X"
            ):
                res += 1

            # diagonal left
            if (
                get(get(L, y), x) == "X"
                and get(get(L, y + 1), x - 1) == "M"
                and get(get(L, y + 2), x - 2) == "A"
                and get(get(L, y + 3), x - 3) == "S"
            ):
                res += 1

            # diagonal left inversed
            if (
                get(get(L, y), x) == "S"
                and get(get(L, y + 1), x - 1) == "A"
                and get(get(L, y + 2), x - 2) == "M"
                and get(get(L, y + 3), x - 3) == "X"
            ):
                res += 1
    print(res)


def part2():
    res = 0

    for y in range(len(L)):
        for x in range(len(L[y])):
            if (
                (
                    get(get(L, y), x) == "S"
                    and get(get(L, y + 1), x + 1) == "A"
                    and get(get(L, y + 2), x + 2) == "M"
                )
                or (
                    get(get(L, y), x) == "M"
                    and get(get(L, y + 1), x + 1) == "A"
                    and get(get(L, y + 2), x + 2) == "S"
                )
            ) and (
                (
                    get(get(L, y), x + 2) == "S"
                    and get(get(L, y + 1), x + 1) == "A"
                    and get(get(L, y + 2), x) == "M"
                )
                or (
                    get(get(L, y), x + 2) == "M"
                    and get(get(L, y + 1), x + 1) == "A"
                    and get(get(L, y + 2), x) == "S"
                )
            ):
                res += 1
    print(res)


part1()
part2()