Skip to content

Commit b47a9b6

Browse files
committed
Add solution to 2024-12-06
1 parent 83a34f0 commit b47a9b6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

2024/day06/solutions.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
with open("input") as f:
2+
ls = f.read().strip().split("\n")
3+
4+
board = {i + 1j * j: x for i, l in enumerate(ls) for j, x in enumerate(l)}
5+
6+
# Part 1
7+
start = next(w for w, x in board.items() if x == "^")
8+
walls = {w for w, x in board.items() if x == "#"}
9+
seen = set()
10+
z = start
11+
dz = -1
12+
while z in board:
13+
seen.add(z)
14+
if z + dz in walls:
15+
dz *= -1j
16+
continue
17+
z += dz
18+
19+
print(len(seen))
20+
21+
22+
# Part 2
23+
def loops(x):
24+
new_walls = walls | {x}
25+
z = start
26+
dz = -1
27+
seen = set()
28+
while z in board:
29+
if (z, dz) in seen:
30+
return True
31+
seen.add((z, dz))
32+
if z + dz in new_walls:
33+
dz *= -1j
34+
continue
35+
z += dz
36+
return False
37+
38+
39+
print(sum(map(loops, seen)))

0 commit comments

Comments
 (0)