Add partially complete 2021-15 solution in Python

Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
akp 2021-12-15 23:06:41 +00:00
parent 7b86b36048
commit 47c497080e
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
4 changed files with 103 additions and 0 deletions

View file

@ -0,0 +1,2 @@
# [Day 15: Chiton](https://adventofcode.com/2021/day/15)

View file

@ -0,0 +1,17 @@
{
"inputFile": "input.txt",
"testCases": {
"one": [
{
"input": "1163751742\n1381373672\n2136511328\n3694931569\n7463417111\n1319128137\n1359912421\n3125421639\n1293138521\n2311944581\n",
"expected": "40"
}
],
"two": [
{
"input": "1163751742\n1381373672\n2136511328\n3694931569\n7463417111\n1319128137\n1359912421\n3125421639\n1293138521\n2311944581\n",
"expected": "315"
}
]
}
}

View file

@ -0,0 +1,83 @@
import heapq
import queue
from typing import Any, Tuple, List, Dict, Optional
from aocpy import BaseChallenge
from dataclasses import dataclass
Point = Tuple[int, int]
Nodes = Dict[Point, int]
@dataclass
class Graph:
nodes: Nodes
edges: Dict[Point, List[Point]]
def get_adjacent_points(point: Point) -> List[Point]:
x, y = point
return [
(x, y - 1),
(x - 1, y),
(x + 1, y),
(x, y + 1),
]
def parse(instr: str) -> Graph:
nodes = {}
edges = {}
for y, line in enumerate(instr.strip().splitlines()):
for x, char in enumerate(line):
nodes[(x, y)] = int(char)
for node in nodes:
edges[node] = edges.get(node, []) + [
p for p in get_adjacent_points(node) if p in nodes
]
return Graph(nodes, edges)
def get_shortest_path_len(graph: Graph) -> int:
# ty https://old.reddit.com/r/adventofcode/comments/rgqzt5/2021_day_15_solutions/hooca55/
start = (0, 0)
end = (
max(x for x, _ in graph.nodes),
max(y for _, y in graph.nodes),
)
dist: Dict[Point, int] = {start: 0}
pq: List[Tuple[int, Point]] = []
for v in graph.nodes:
if v != start:
dist[v] = int(1e9)
heapq.heappush(pq, (dist[v], v))
while pq:
_, u = heapq.heappop(pq)
if u == end:
return dist[end]
for v in graph.edges[u]:
if v not in graph.nodes:
continue
alt = dist[u] + graph.nodes[v]
if alt < dist[v]:
dist[v] = alt
heapq.heappush(pq, (alt, v))
return dist[end]
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
g = parse(instr)
return get_shortest_path_len(g)
@staticmethod
def two(instr: str) -> Any:
raise NotImplementedError

View file

@ -26,6 +26,7 @@ Solutions to the [2021 Advent of Code](https://adventofcode.com/2021).
| 12 - Passage Pathing | ★ ★ | [Go](12-passagePathing/go) | I couldn't tell you how it works, but it does kinda work and I think I have a vague idea (external help was used). |
| 13 - Transparent Origami | ★ ★ | [Python](13-transparentOrigami/py), [Nim](13-transparentOrigami/nim) | I got stuck for hours on an intermittent off-by-one error. :( |
| 14 - Extended Polymerization | ★ ★ | [Python](14-extendedPolymerization/py) | Another off-by-one error, but this time it was because of dodgy division. Wonderful. |
| 15 - Chiton | ★ ☆ | [Python](15-chiton/py) | Pathfinding is hard |
<!-- PARSE END -->