starting Day 21

This commit is contained in:
WieErWill 2023-12-21 22:01:48 +01:00
parent f71a82c854
commit 816961a2b7
3 changed files with 174 additions and 0 deletions

90
Day21/README.md Normal file
View File

@ -0,0 +1,90 @@
# Day 21: Step Counter
## Part One
You manage to catch the airship right as it's dropping someone else off on their all-expenses-paid trip to Desert Island! It even helpfully drops you off near the gardener and his massive farm.
"You got the sand flowing again! Great work! Now we just need to wait until we have enough sand to filter the water for Snow Island and we'll have snow again in no time."
While you wait, one of the Elves that works with the gardener heard how good you are at solving problems and would like your help. He needs to get his steps in for the day, and so he'd like to know which garden plots he can reach with exactly his remaining 64 steps.
He gives you an up-to-date map (your puzzle input) of his starting position (S), garden plots (.), and rocks (#). For example:
```
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........
```
The Elf starts at the starting position (S) which also counts as a garden plot. Then, he can take one step north, south, east, or west, but only onto tiles that are garden plots. This would allow him to reach any of the tiles marked O:
```
...........
.....###.#.
.###.##..#.
..#.#...#..
....#O#....
.##.OS####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........
```
Then, he takes a second step. Since at this point he could be at either tile marked O, his second step would allow him to reach any garden plot that is one step north, south, east, or west of any tile that he could have reached after the first step:
```
...........
.....###.#.
.###.##..#.
..#.#O..#..
....#.#....
.##O.O####.
.##.O#...#.
.......##..
.##.#.####.
.##..##.##.
...........
```
After two steps, he could be at any of the tiles marked O above, including the starting position (either by going north-then-south or by going west-then-east).
A single third step leads to even more possibilities:
```
...........
.....###.#.
.###.##..#.
..#.#.O.#..
...O#O#....
.##.OS####.
.##O.#...#.
....O..##..
.##.#.####.
.##..##.##.
...........
```
He will continue like this until his steps for the day have been exhausted. After a total of 6 steps, he could reach any of the garden plots marked O:
```
...........
.....###.#.
.###.##.O#.
.O#O#O.O#..
O.O.#.#.O..
.##O.O####.
.##.O#O..#.
.O.O.O.##..
.##.#.####.
.##O.##.##.
...........
```
In this example, if the Elf's goal was to get exactly 6 more steps today, he could use them to reach any of 16 garden plots.
However, the Elf actually needs to get 64 steps today, and the map he's handed you is much larger than the example map.
Starting from the garden plot marked S on your map, how many garden plots could the Elf reach in exactly 64 steps?

73
Day21/python/solution1.py Normal file
View File

@ -0,0 +1,73 @@
from collections import deque
def read_grid(file_path):
"""Reads the grid from a file and returns it along with the starting position."""
with open(file_path, 'r') as file:
grid = [list(line.strip()) for line in file.readlines()]
start = None
for i, row in enumerate(grid):
for j, cell in enumerate(row):
if cell == 'S':
start = (i, j)
break
if start:
break
assert start is not None, "Starting position 'S' not found in the grid."
print(f"Grid loaded from {file_path}. Start position: {start}")
return grid, start
def bfs(grid, start):
"""Performs BFS on the grid to track the minimum steps to each plot."""
printf("Start BFS")
steps = 64
queue = deque([(start[0], start[1], 0)])
distances = {}
while queue:
x, y, step = queue.popleft()
if step > steps:
continue # Skip plots that are more than 64 steps away
if step == steps:
distances[(x, y)] = step # Only consider plots at exactly 64 steps
else:
for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
nx, ny = x + dx, y + dy
if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]) and grid[nx][ny] == '.' and (nx, ny) not in distances:
queue.append((nx, ny, step + 1))
print(f"BFS completed. Number of plots visited: {len(distances)}")
return distances
def count_reachable_plots(distances, steps):
"""Counts the number of plots reachable in exactly the given number of steps."""
count = sum(1 for d in distances.values() if d == steps)
print(f"Number of plots reachable in exactly {steps} steps: {count}")
return count
def run_test():
"""Runs the algorithm with the test data and asserts the result."""
print("Running test...")
test_grid, test_start = read_grid("../test.txt")
distances = bfs(test_grid, test_start)
test_result = count_reachable_plots(distances, 64)
assert test_result == 16, f"Test failed: Expected 16, got {test_result}"
print("Test passed successfully.")
def main():
"""Main function to run the algorithm with the actual puzzle data."""
try:
run_test()
grid, start = read_grid("../input.txt")
distances = bfs(grid, start)
result = count_reachable_plots(distances, 64)
print(f"Final Result: Number of reachable plots in exactly 64 steps: {result}")
except AssertionError as ae:
print(f"Assertion Error: {ae}")
except FileNotFoundError as fnfe:
print(f"File not found error: {fnfe}")
except Exception as e:
print(f"Unexpected error: {e}")
if __name__ == "__main__":
main()

11
Day21/test.txt Normal file
View File

@ -0,0 +1,11 @@
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........