summaryrefslogtreecommitdiff
path: root/15_2024.py
blob: 9952a8d720c5ea2f8b01f5eb8bfa64e2b303574f (plain)
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
from collections import deque

input = "data/15_2024.txt"
data = open(input).read().strip()
lines, instrs = data.split("\n\n")
lines = lines.split("\n")


def solve(lines, part2):
    rows = len(lines)
    cols = len(lines[0])
    lines = [[lines[r][c] for c in range(cols)] for r in range(rows)]
    if part2:
        big = []
        for r in range(rows):
            row = []
            for c in range(cols):
                if lines[r][c] == "#":
                    row.append("#")
                    row.append("#")
                if lines[r][c] == "O":
                    row.append("[")
                    row.append("]")
                if lines[r][c] == ".":
                    row.append(".")
                    row.append(".")
                if lines[r][c] == "@":
                    row.append("@")
                    row.append(".")
            big.append(row)
        lines = big
        cols *= 2
    for r in range(rows):
        for c in range(cols):
            if lines[r][c] == "@":
                sr, sc = r, c
                lines[r][c] = "."
    r, c = sr, sc
    for inst in instrs:
        if inst == "\n":
            continue
        dr, dc = {"^": (-1, 0), ">": (0, 1), "v": (1, 0), "<": (0, -1)}[inst]
        rr, cc = r + dr, c + dc
        if lines[rr][cc] == "#":
            continue
        elif lines[rr][cc] == ".":
            r, c = rr, cc
        elif lines[rr][cc] in ["[", "]", "O"]:
            q = deque([(r, c)])
            vis = set()
            ok = True
            while q:
                rr, cc = q.popleft()
                if (rr, cc) in vis:
                    continue
                vis.add((rr, cc))
                rrr, ccc = rr + dr, cc + dc
                if lines[rrr][ccc] == "#":
                    ok = False
                    break
                if lines[rrr][ccc] == "O":
                    q.append((rrr, ccc))
                if lines[rrr][ccc] == "[":
                    q.append((rrr, ccc))
                    assert lines[rrr][ccc + 1] == "]"
                    q.append((rrr, ccc + 1))
                if lines[rrr][ccc] == "]":
                    q.append((rrr, ccc))
                    assert lines[rrr][ccc - 1] == "["
                    q.append((rrr, ccc - 1))
            if not ok:
                continue
            while len(vis) > 0:
                for rr, cc in sorted(vis):
                    rrr, ccc = rr + dr, cc + dc
                    if (rrr, ccc) not in vis:
                        assert lines[rrr][ccc] == "."
                        lines[rrr][ccc] = lines[rr][cc]
                        lines[rr][cc] = "."
                        vis.remove((rr, cc))
            r = r + dr
            c = c + dc
    res = 0
    for r in range(rows):
        for c in range(cols):
            if lines[r][c] in ["[", "O"]:
                res += 100 * r + c
    return res


print(solve(lines, False))
print(solve(lines, True))