Skip to content

Commit 42305a3

Browse files
committed
2024, day 6, part 1 (StopIteration)
1 parent e611567 commit 42305a3

File tree

2 files changed

+154
-16
lines changed

2 files changed

+154
-16
lines changed

2024/data/day06.txt

Lines changed: 130 additions & 0 deletions
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+
.#............#.......#..................................#....#.................................................#..............#.#

2024/day06.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def parse(lines):
3131
for x, content in enumerate(row)
3232
if content == "#"
3333
]
34-
visited = 0
34+
visited = set()
3535
return Grid(current, heading, obstacles, visited)
3636

3737

@@ -56,17 +56,33 @@ def visualize(obstacles, nxt, heading):
5656
def walk(grid):
5757
"""Walk one step and simulate the new state of the grid"""
5858
current, heading, obstacles, visited = grid
59+
visited.add(current)
60+
# visualize(obstacles, current, heading)
5961
if next_step(current, heading) in obstacles:
6062
print("turning")
6163
heading = turn_right(heading)
6264
nxt = next_step(current, heading)
63-
visited += 1
64-
print(nxt, heading, visited)
65-
visualize(obstacles, nxt, heading)
66-
print()
65+
# print(current, nxt, heading, visited)
66+
# print()
6767
return Grid(nxt, heading, obstacles, visited)
6868

6969

70+
def visit(map):
71+
lines = [list(line) for line in map.strip().splitlines()]
72+
max_y = len(lines)
73+
max_x = len(lines[0])
74+
grid = parse(lines)
75+
while (0 <= grid.current[0] <= max_x) and (0 <= grid.current[1] <= max_y):
76+
grid = walk(grid)
77+
return len(grid.visited) - 1
78+
79+
80+
def main(part: int = 1) -> int:
81+
with open("2024/data/day05.txt") as f:
82+
map = f.read()
83+
return visit(map)
84+
85+
7086
if __name__ == "__main__":
7187
map = """
7288
....#.....
@@ -80,14 +96,6 @@ def walk(grid):
8096
#.........
8197
......#..."""
8298

83-
# repeat(50, walk, parse(map))
84-
lines = [list(line) for line in map.strip().splitlines()]
85-
max_y = len(lines)
86-
max_x = len(lines[0])
87-
grid = parse(lines)
88-
walking = True
89-
while walking:
90-
grid = walk(grid)
91-
if not (0 <= grid.current[0] <= max_x) or not (0 <= grid.current[1] <= max_y):
92-
walking = False
93-
print(grid.visited)
99+
assert visit(map) == 41
100+
101+
print(main())

0 commit comments

Comments
 (0)