Skip to content

Commit 7134e9d

Browse files
authored
Merge pull request #166 from GriceTurrble/feat/2024-day6
Feat/2024-day6
2 parents 0db0907 + 2a65040 commit 7134e9d

File tree

4 files changed

+214
-13
lines changed

4 files changed

+214
-13
lines changed

2024/inputs/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/inputs/tests/test_day06.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
....#.....
2+
.........#
3+
..........
4+
..#.......
5+
.......#..
6+
..........
7+
.#..^.....
8+
........#.
9+
#.........
10+
......#...

2024/python/src/grice_py_aoc_2024/day06/main.py

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,92 @@
11
from __future__ import annotations
22

33
import time
4+
from collections import deque
45
from pathlib import Path
6+
from typing import TypeVar
57

68
DIR = Path(__file__).parent
79
FILE = DIR.parents[3] / "inputs" / f"{DIR.stem}.txt"
810

11+
directions = deque(["up", "right", "down", "left"])
912

10-
def part1(contents: str):
11-
return "Not done yet!"
13+
GUARD = "^"
14+
15+
16+
def _find_guard(grid: list[str]) -> tuple[int, int]:
17+
for x, line in enumerate(grid):
18+
if GUARD in line:
19+
return (x, line.index(GUARD))
20+
raise ValueError("Did not find the guard!")
21+
22+
23+
T = TypeVar("T")
24+
25+
26+
def _new_direction(directions: deque[T]) -> T:
27+
"""Returns the direction at index 0.
28+
29+
Directions is mutated by this function: it is rotated,
30+
moving the first direction to the end of the sequence.
31+
"""
32+
direction = directions[0]
33+
directions.rotate(-1)
34+
return direction
35+
36+
37+
def part1(grid: list[str]) -> int:
38+
guard = _find_guard(grid=grid)
39+
len_col, len_row = len(grid), len(grid[0])
40+
directions = deque(
41+
[
42+
(-1, 0), # up
43+
(0, 1), # right
44+
(1, 0), # down
45+
(0, -1), # left
46+
],
47+
)
48+
49+
# Get the starting direction.
50+
direction = _new_direction(directions)
51+
52+
# Gather set of all coordinates we've seen
53+
visited = set()
54+
visited.add(guard)
55+
56+
while True:
57+
next_space = (guard[0] + direction[0], guard[1] + direction[1])
58+
nx, ny = next_space
59+
if not (0 <= nx < len_col) or not (0 <= ny < len_row):
60+
# The next space is outside the walls; the guard leaves the area
61+
break
62+
if grid[nx][ny] == "#":
63+
# Guard hits an obstacle.
64+
# Does not move, but changes direction for next time.
65+
direction = _new_direction(directions)
66+
else:
67+
# The guard moves into the new space.
68+
guard = (nx, ny)
69+
# Add this position to visited spaces
70+
visited.add(guard)
71+
72+
return len(visited)
1273

1374

14-
def part2(contents: str):
75+
def part2(grid: list[str]):
76+
# TODO
1577
return "Not done yet!"
1678

1779

1880
def main():
19-
contents = FILE.read_text()
81+
grid = FILE.read_text().strip().split("\n")
2082

2183
_start1 = time.perf_counter()
22-
result1 = part1(contents)
84+
result1 = part1(grid=grid)
2385
_delta1 = time.perf_counter() - _start1
2486
print(f">> Part 1: {result1} ({_delta1:.6f}s)")
2587

2688
_start2 = time.perf_counter()
27-
result2 = part2(contents)
89+
result2 = part2(grid=grid)
2890
_delta2 = time.perf_counter() - _start2
2991
print(f">> Part 2: {result2} ({_delta2:.6f}s)")
3092

0 commit comments

Comments
 (0)