summaryrefslogtreecommitdiff
path: root/2024/06/python/main.py
blob: 5fd6c3d3126b30875b1b94b1c43bd87023786767 (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
from fileinput import input

obstacles = set()
seen = set()

xmax, ymax = 0, 0
for idy, line in enumerate(input()):
    ymax = max(ymax, idy)
    for idx, c in enumerate(line.strip()):
        xmax = max(xmax, idx)
        match c:
            case "^":
                pos = complex(idx, idy)
                dir = 0 - 1j
            case "#":
                obstacles.add(complex(idx, idy))


def is_loop(pos, obst):
    seen_ = set()
    dir = 0 - 1j
    while 0 <= pos.real <= xmax and 0 <= pos.imag <= ymax:
        if (pos, dir) in seen_:
            return True
        seen_.add((pos, dir))
        if pos + dir in obstacles or pos + dir == obst:
            # Rotate cw
            dir *= 1j
            continue
        pos += dir
    return False


gold = 0
for idx in range(xmax + 1):
    for idy in range(ymax + 1):
        if complex(idx, idy) == pos:
            continue
        else:
            gold += is_loop(pos, complex(idx, idy))


while 0 <= pos.real <= xmax and 0 <= pos.imag <= ymax:
    seen.add(pos)
    if pos + dir in obstacles:
        # Rotate cw
        dir *= 1j
        continue
    pos += dir


silver = len(seen)

print("silver:", silver)
print("gold:", gold)