From 7ec2081eea936bb621a46da7fcda22e5fc0fee2d Mon Sep 17 00:00:00 2001 From: Daniel Lin Date: Sat, 24 Dec 2022 05:24:06 -0500 Subject: [PATCH] Day 24: Blizzard Basin --- README.md | 2 +- py/aoc2022/day24.py | 62 +++++++++++++++++++++++++++++++++++++++++++++ py/pyproject.toml | 1 + 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 py/aoc2022/day24.py diff --git a/README.md b/README.md index 372d47d..6f9ab8d 100644 --- a/README.md +++ b/README.md @@ -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)| diff --git a/py/aoc2022/day24.py b/py/aoc2022/day24.py new file mode 100644 index 0000000..d33c7bb --- /dev/null +++ b/py/aoc2022/day24.py @@ -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) diff --git a/py/pyproject.toml b/py/pyproject.toml index ac04040..01b3625 100644 --- a/py/pyproject.toml +++ b/py/pyproject.toml @@ -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"]