Skip to content

Commit

Permalink
Day 24: Blizzard Basin
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 24, 2022
1 parent 91d0716 commit 7ec2081
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ Development occurs in language-specific directories:
|[Day21.hs](hs/src/Day21.hs)|[Day21.kt](kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day21.kt)|[day21.py](py/aoc2022/day21.py)|[day21.rs](rs/src/day21.rs)|
|[Day22.hs](hs/src/Day22.hs)|[Day22.kt](kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day22.kt)|[day22.py](py/aoc2022/day22.py)|[day22.rs](rs/src/day22.rs)|
|[Day23.hs](hs/src/Day23.hs)|[Day23.kt](kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day23.kt)|[day23.py](py/aoc2022/day23.py)|[day23.rs](rs/src/day23.rs)|
|[Day24.hs](hs/src/Day24.hs)|[Day24.kt](kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day24.kt)|
|[Day24.hs](hs/src/Day24.hs)|[Day24.kt](kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day24.kt)|[day24.py](py/aoc2022/day24.py)|
62 changes: 62 additions & 0 deletions py/aoc2022/day24.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Day 24: Blizzard Basin
"""

import heapq

SAMPLE_INPUT = ["#.######", "#>>.<^<#", "#.<..<<#", "#>v.><>#", "#<^v^^>#", "######.#"]


def _search(lines, start, end, time=0):
end_x, end_y = end
seen, queue = {(start, time)}, [(0, (start, time))]
while queue:
position, time = heapq.heappop(queue)[1]
if position == end:
return time
x, y = position
time += 1
for x, y in [(x - 1, y), (x, y - 1), (x, y), (x, y + 1), (x + 1, y)]:
if y < 0 or y >= len(lines) or x < 1 or x >= len(line := lines[y]) - 1:
continue
if y in (0, len(lines) - 1):
if line[x] != ".":
continue
elif lines[y][(x - 1 + time) % (len(line) - 2) + 1] == "<":
continue
elif lines[y][(x - 1 - time) % (len(line) - 2) + 1] == ">":
continue
elif lines[(y - 1 + time) % (len(lines) - 2) + 1][x] == "^":
continue
elif lines[(y - 1 - time) % (len(lines) - 2) + 1][x] == "v":
continue
state = ((x, y), time)
if state not in seen:
seen.add(state)
heapq.heappush(queue, (time + abs(x - end_x) + abs(y - end_y), state))
raise LookupError()


def part1(lines):
"""
>>> part1(SAMPLE_INPUT)
18
"""
lines = [line.rstrip() for line in lines]
return _search(lines, (1, 0), (len(lines[-1]) - 2, len(lines) - 1))


def part2(lines):
"""
>>> part2(SAMPLE_INPUT)
54
"""
lines = [line.rstrip() for line in lines]
start = (1, 0)
end = (len(lines[-1]) - 2, len(lines) - 1)
return _search(
lines, start, end, _search(lines, end, start, _search(lines, start, end))
)


parts = (part1, part2)
1 change: 1 addition & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ day20 = "aoc2022.day20:parts"
day21 = "aoc2022.day21:parts"
day22 = "aoc2022.day22:parts"
day23 = "aoc2022.day23:parts"
day24 = "aoc2022.day24:parts"

[tool.black]
target_version = ["py311"]
Expand Down

0 comments on commit 7ec2081

Please sign in to comment.