Skip to content

Commit b5f09d2

Browse files
committed
improve rust day 1
1 parent caec60c commit b5f09d2

File tree

7 files changed

+494
-11
lines changed

7 files changed

+494
-11
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
22
"mypy-type-checker.cwd": "${workspaceFolder}/2024/python",
3-
"mypy-type-checker.importStrategy": "fromEnvironment"
3+
"mypy-type-checker.importStrategy": "fromEnvironment",
4+
"rust-analyzer.restartServerOnConfigChange": true,
5+
"rust-analyzer.linkedProjects": ["2024/rust/Cargo.toml"]
46
}

2024/python/src/day06.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
from enum import Enum
2+
from pathlib import Path
3+
4+
GRID_HEIGHT = 130
5+
GRID_WIDTH = 130
6+
7+
input_file = Path(__file__).parent / "day06_input.txt"
8+
input = input_file.read_text()
9+
input_grid: list[list[str]] = [list(line) for line in input.splitlines()]
10+
11+
visited_cells = [[False] * GRID_WIDTH] * GRID_HEIGHT
12+
13+
print("grid height", len(input_grid))
14+
print("grid width", len(input_grid[0]))
15+
16+
17+
class Direction(Enum):
18+
NORTH = 0
19+
EAST = 1
20+
SOUTH = 2
21+
WEST = 3
22+
23+
24+
def find_guard_coordinates(grid: list[list[str]]) -> tuple[int, int]:
25+
for i, row in enumerate(grid):
26+
for j, cell in enumerate(row):
27+
if cell == "^":
28+
return (i, j)
29+
return (-1, -1)
30+
31+
32+
guard_direction = Direction.NORTH
33+
guard_coordinates = find_guard_coordinates(input_grid)
34+
35+
# override position of initial guard location with "." to allow for re-visiting
36+
input_grid[guard_coordinates[0]][guard_coordinates[1]] = "."
37+
38+
39+
while True:
40+
x, y = guard_coordinates
41+
if x < 0 or x >= GRID_HEIGHT or y < 0 or y >= GRID_WIDTH:
42+
break
43+
visited_cells[x][y] = True
44+
cell_ahead = None
45+
match guard_direction:
46+
case Direction.NORTH:
47+
cell_ahead = input_grid[x - 1][y]
48+
# if cell_ahead == "#":
49+
# print("turning east")
50+
# guard_direction = Direction.EAST
51+
# else:
52+
# print("moving north to", x - 1, y)
53+
# guard_coordinates = (x - 1, y)
54+
case Direction.EAST:
55+
cell_ahead = input_grid[x][y + 1]
56+
# if cell_ahead == "#":
57+
# print("turning south")
58+
# guard_direction = Direction.SOUTH
59+
# else:
60+
# print("moving east to", x, y + 1)
61+
# guard_coordinates = (x, y + 1)
62+
case Direction.SOUTH:
63+
cell_ahead = input_grid[x + 1][y]
64+
# if cell_ahead == "#":
65+
# print("turning west")
66+
# guard_direction = Direction.WEST
67+
# else:
68+
# print("moving south to", x + 1, y)
69+
# guard_coordinates = (x + 1, y)
70+
case Direction.WEST:
71+
cell_ahead = input_grid[x][y - 1]
72+
# if cell_ahead == "#":
73+
# print("turning north")
74+
# guard_direction = Direction.NORTH
75+
# else:
76+
# print("moving west to", x, y - 1)
77+
# guard_coordinates = (x, y - 1)
78+
match cell_ahead:
79+
case "#":
80+
match guard_direction:
81+
case Direction.NORTH:
82+
print("turning east")
83+
guard_direction = Direction.EAST
84+
case Direction.EAST:
85+
print("turning south")
86+
guard_direction = Direction.SOUTH
87+
case Direction.SOUTH:
88+
print("turning west")
89+
guard_direction = Direction.WEST
90+
case Direction.WEST:
91+
print("turning north")
92+
guard_direction = Direction.NORTH
93+
case ".":
94+
match guard_direction:
95+
case Direction.NORTH:
96+
print("moving north to", x - 1, y)
97+
guard_coordinates = (x - 1, y)
98+
case Direction.EAST:
99+
print("moving east to", x, y + 1)
100+
guard_coordinates = (x, y + 1)
101+
case Direction.SOUTH:
102+
print("moving south to", x + 1, y)
103+
guard_coordinates = (x + 1, y)
104+
case Direction.WEST:
105+
print("moving west to", x, y - 1)
106+
guard_coordinates = (x, y - 1)
107+
108+
print("guard coordinates", guard_coordinates)
109+
110+
visited_count = 0
111+
visited_grid = input_grid.copy()
112+
113+
for i, visited_row in enumerate(visited_cells):
114+
for j, visited_cell in enumerate(visited_row):
115+
if visited_cell is True:
116+
visited_grid[i][j] = "X"
117+
visited_count += 1
118+
119+
print("visited count", visited_count)
120+
121+
visited_file = Path(__file__).parent / "day06_visited.txt"
122+
visited_file.write_text("\n".join(["".join(row) for row in visited_grid]))

2024/python/src/day06_challenge.txt

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
--- Day 6: Guard Gallivant ---
2+
3+
The Historians use their fancy device again, this time to whisk you all away to the North Pole prototype suit manufacturing lab... in the year 1518! It turns out that having direct access to history is very convenient for a group of historians.
4+
5+
You still have to be careful of time paradoxes, and so it will be important to avoid anyone from 1518 while The Historians search for the Chief. Unfortunately, a single guard is patrolling this part of the lab.
6+
7+
Maybe you can work out where the guard will go ahead of time so that The Historians can search safely?
8+
9+
You start by making a map (your puzzle input) of the situation. For example:
10+
11+
....#.....
12+
.........#
13+
..........
14+
..#.......
15+
.......#..
16+
..........
17+
.#..^.....
18+
........#.
19+
#.........
20+
......#...
21+
22+
The map shows the current position of the guard with ^ (to indicate the guard is currently facing up from the perspective of the map). Any obstructions - crates, desks, alchemical reactors, etc. - are shown as #.
23+
24+
Lab guards in 1518 follow a very strict patrol protocol which involves repeatedly following these steps:
25+
26+
If there is something directly in front of you, turn right 90 degrees.
27+
Otherwise, take a step forward.
28+
29+
Following the above protocol, the guard moves up several times until she reaches an obstacle (in this case, a pile of failed suit prototypes):
30+
31+
....#.....
32+
....^....#
33+
..........
34+
..#.......
35+
.......#..
36+
..........
37+
.#........
38+
........#.
39+
#.........
40+
......#...
41+
42+
Because there is now an obstacle in front of the guard, she turns right before continuing straight in her new facing direction:
43+
44+
....#.....
45+
........>#
46+
..........
47+
..#.......
48+
.......#..
49+
..........
50+
.#........
51+
........#.
52+
#.........
53+
......#...
54+
55+
Reaching another obstacle (a spool of several very long polymers), she turns right again and continues downward:
56+
57+
....#.....
58+
.........#
59+
..........
60+
..#.......
61+
.......#..
62+
..........
63+
.#......v.
64+
........#.
65+
#.........
66+
......#...
67+
68+
This process continues for a while, but the guard eventually leaves the mapped area (after walking past a tank of universal solvent):
69+
70+
....#.....
71+
.........#
72+
..........
73+
..#.......
74+
.......#..
75+
..........
76+
.#........
77+
........#.
78+
#.........
79+
......#v..
80+
81+
By predicting the guard's route, you can determine which specific positions in the lab will be in the patrol path. Including the guard's starting position, the positions visited by the guard before leaving the area are marked with an X:
82+
83+
....#.....
84+
....XXXXX#
85+
....X...X.
86+
..#.X...X.
87+
..XXXXX#X.
88+
..X.X.X.X.
89+
.#XXXXXXX.
90+
.XXXXXXX#.
91+
#XXXXXXX..
92+
......#X..
93+
94+
In this example, the guard will visit 41 distinct positions on your map.
95+
96+
Predict the path of the guard. How many distinct positions will the guard visit before leaving the mapped area?
97+
98+
99+
100+
101+

0 commit comments

Comments
 (0)