From a4b81dac45b46d562f55a0fc1d73f8b5cf18e279 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 15 Dec 2021 10:21:33 +0100 Subject: [PATCH] 2021: d15: ex1: add solution --- 2021/d15/ex1/ex1.py | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 2021/d15/ex1/ex1.py diff --git a/2021/d15/ex1/ex1.py b/2021/d15/ex1/ex1.py new file mode 100755 index 0000000..4399aba --- /dev/null +++ b/2021/d15/ex1/ex1.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python + +import heapq +import sys +from typing import Iterator, List, NamedTuple, Set + + +class Point(NamedTuple): + x: int + y: int + + +def solve(input: List[str]) -> int: + levels = [[int(c) for c in line] for line in input] + + def neighbours(p: Point) -> Iterator[Point]: + for dx, dy in (-1, 0), (1, 0), (0, -1), (0, 1): + x, y = p.x + dx, p.y + dy + if x < 0 or x >= len(levels): + continue + if y < 0 or y >= len(levels[0]): + continue + yield Point(x, y) + + def djikstra(start: Point, end: Point) -> int: + # Priority queue of (distance, point) + queue = [(0, start)] + seen: Set[Point] = set() + + while len(queue) > 0: + dist, p = heapq.heappop(queue) + if p == end: + return dist + # We must have seen p with a smaller distance before + if p in seen: + continue + # First time encountering p, must be the smallest distance to it + seen.add(p) + # Add all neighbours to be visited + for n in neighbours(p): + heapq.heappush(queue, (dist + levels[n.x][n.y], n)) + + assert False # Sanity check + + return djikstra(Point(0, 0), Point(len(levels) - 1, len(levels[0]) - 1)) + + +def main() -> None: + input = [line.strip() for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()