Skip to content

Commit ce41168

Browse files
committed
2024: Day 6
1 parent 0f2d57c commit ce41168

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

2024/python/day6/__init__.py

Whitespace-only changes.

2024/python/day6/__main__.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from utils.file import read_input
2+
3+
lines = read_input(__package__)
4+
height = len(lines)
5+
width = len(lines[0])
6+
7+
start = next((i, line.index("^")) for i, line in enumerate(lines) if "^" in line)
8+
9+
obstacles = {complex(i, j) for i, line in enumerate(lines) for j, c in enumerate(line) if c == "#"}
10+
11+
directions = [-1, 1j, 1, -1j]
12+
13+
14+
def walk(pos: complex, obstacles_: set[complex]) -> tuple[set[complex], bool]:
15+
direction = 0
16+
path: set[complex] = {pos}
17+
visited: set[tuple[complex, int]] = {(pos, direction)}
18+
while True:
19+
new_pos = pos + directions[direction]
20+
if not (0 <= new_pos.real < height and 0 <= new_pos.imag < width):
21+
break
22+
if new_pos in obstacles_:
23+
direction = (direction + 1) % 4
24+
continue
25+
if (new_pos, direction) in visited:
26+
return path, True
27+
pos = new_pos
28+
path.add(pos)
29+
visited.add((pos, direction))
30+
return path, False
31+
32+
33+
path, _ = walk(complex(*start), obstacles)
34+
print("Part 1:", len(path))
35+
36+
result = 0
37+
for i, candidate in enumerate(path):
38+
# print("Candidate", i, candidate)
39+
_path, loop = walk(complex(*start), obstacles | {candidate})
40+
if loop:
41+
result += 1
42+
43+
print("Part 2:", result)

2024/python/day6/input.txt

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
#...........#..........................#....................................#.........#................................#..........
128+
......##..#...............................................................#...............................#.........#.............
129+
.......................#.....#.........................#...#.........................#.....#..........#.#...#.....................
130+
.......................#..#.....#.....#..........................#.........#.....#....................#.....#....................#

0 commit comments

Comments
 (0)