aboutsummaryrefslogtreecommitdiff
path: root/2024/04/solve.py
blob: 65b90507a21bc5b5af93ae5f0e858c884a9aba34 (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
#!/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 scan(what, y, x, yshift, xshift):
    return (
        get(get(L, y), x) == what[0]
        and get(get(L, y + 1 * yshift), x + 1 * xshift) == what[1]
        and get(get(L, y + 2 * yshift), x + 2 * xshift) == what[2]
        and get(get(L, y + 3 * yshift), x + 3 * xshift) == what[3]
    )


def part1():
    res = 0

    for y in range(len(L)):
        for x in range(len(L[y])):
            # horizontal
            if scan("XMAS", y, x, 0, 1):
                res += 1

            # horizontal inversed
            if scan("SAMX", y, x, 0, 1):
                res += 1

            # diagonal right
            if scan("XMAS", y, x, 1, 1):
                res += 1

            # diagonal right inversed
            if scan("SAMX", y, x, 1, 1):
                res += 1

            # vertical
            if scan("XMAS", y, x, 1, 0):
                res += 1

            # vertical inversed
            if scan("SAMX", y, x, 1, 0):
                res += 1

            # diagonal left
            if scan("XMAS", y, x, 1, -1):
                res += 1

            # diagonal left inversed
            if scan("SAMX", y, x, 1, -1):
                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()