Solve 2025 day 7
This commit is contained in:
59
2025/d07.py
Normal file
59
2025/d07.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
from lib import get_data, Grid2D
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
|
data = """.......S.......
|
||||||
|
...............
|
||||||
|
.......^.......
|
||||||
|
...............
|
||||||
|
......^.^......
|
||||||
|
...............
|
||||||
|
.....^.^.^.....
|
||||||
|
...............
|
||||||
|
....^.^...^....
|
||||||
|
...............
|
||||||
|
...^.^...^.^...
|
||||||
|
...............
|
||||||
|
..^...^.....^..
|
||||||
|
...............
|
||||||
|
.^.^.^.^.^...^.
|
||||||
|
..............."""
|
||||||
|
data = get_data(__file__)
|
||||||
|
|
||||||
|
g = Grid2D(data)
|
||||||
|
beams = [g.find('S')[0][1]]
|
||||||
|
|
||||||
|
splits = 0
|
||||||
|
for row in range(1, g.n_rows - 1):
|
||||||
|
new_beams = set()
|
||||||
|
for beam in beams:
|
||||||
|
field = g[(row, beam)]
|
||||||
|
if field == "^":
|
||||||
|
splits += 1
|
||||||
|
new_beams.add(beam - 1)
|
||||||
|
new_beams.add(beam + 1)
|
||||||
|
elif field == ".":
|
||||||
|
new_beams.add(beam)
|
||||||
|
else:
|
||||||
|
assert False, "unexpected field"
|
||||||
|
beams = list(new_beams)
|
||||||
|
# print(beams)
|
||||||
|
|
||||||
|
print(splits)
|
||||||
|
|
||||||
|
beams = {g.find('S')[0][1]: 1}
|
||||||
|
for row in range(1, g.n_rows - 1):
|
||||||
|
new_beams = defaultdict(int)
|
||||||
|
for beam, count in beams.items():
|
||||||
|
field = g[(row, beam)]
|
||||||
|
if field == "^":
|
||||||
|
new_beams[beam - 1] += count
|
||||||
|
new_beams[beam + 1] += count
|
||||||
|
elif field == ".":
|
||||||
|
new_beams[beam] += count
|
||||||
|
else:
|
||||||
|
assert False, "unexpected field"
|
||||||
|
beams = new_beams
|
||||||
|
|
||||||
|
print(sum(beams.values()))
|
||||||
|
|
||||||
@@ -20,7 +20,13 @@ stressful and this will actually be more fun. Thank you Eric Wastl and let's go!
|
|||||||
time because I always kind of avoid that. I could probably decrease the
|
time because I always kind of avoid that. I could probably decrease the
|
||||||
complexity from O(n^2) to O(n log(n)) but I am happy that I've implemented
|
complexity from O(n^2) to O(n log(n)) but I am happy that I've implemented
|
||||||
merging at all.
|
merging at all.
|
||||||
- Day 6:
|
- Day 6: Transposing some rows and cols. Fun and good to keep the brain fit
|
||||||
|
but not really hard. I would have been way too slow for the leaderboard
|
||||||
|
in pre-AI years.
|
||||||
|
- Day 7: Grid puzzle that required non-naiv implementation for part 2. Took
|
||||||
|
me a second to realize that I could not save the coordinates by list but
|
||||||
|
had to use a count to be efficient.
|
||||||
|
- Day 8:
|
||||||
|
|
||||||
|
|
||||||
## AoC 2024
|
## AoC 2024
|
||||||
|
|||||||
Reference in New Issue
Block a user