From a3a1c9a8e05c3f82bf4356c8442fe4b538c0d868 Mon Sep 17 00:00:00 2001 From: Wim Jeantine-Glenn Date: Mon, 23 Dec 2024 03:19:57 -0600 Subject: [PATCH] day 18 --- aoc_wim/aoc2024/q14.py | 4 ++-- aoc_wim/aoc2024/q18.py | 30 ++++++++++++++++++++++++++++++ aoc_wim/zgrid.py | 4 ++++ tests/2024/14/0.txt | 2 +- tests/2024/18/0.txt | 27 +++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 aoc_wim/aoc2024/q18.py create mode 100644 tests/2024/18/0.txt diff --git a/aoc_wim/aoc2024/q14.py b/aoc_wim/aoc2024/q14.py index 0cde64f..ae10c74 100644 --- a/aoc_wim/aoc2024/q14.py +++ b/aoc_wim/aoc2024/q14.py @@ -11,8 +11,8 @@ import statistics -w = extra.get("w", 101) -h = extra.get("h", 103) +w = extra.get("width", 101) +h = extra.get("height", 103) @dataclass diff --git a/aoc_wim/aoc2024/q18.py b/aoc_wim/aoc2024/q18.py new file mode 100644 index 0000000..dc91abe --- /dev/null +++ b/aoc_wim/aoc2024/q18.py @@ -0,0 +1,30 @@ +""" +--- Day 18: RAM Run --- +https://adventofcode.com/2024/day/18 +""" +from aocd import data +from aocd import extra +from aoc_wim.zgrid import ZGrid +import networkx as nx + + +w = extra.get("w", 71) +n_bytes = extra.get("n_bytes", 1024) +grid = ZGrid.fromempty(w, w, glyph=".") +zs = [complex(*map(int, x.split(","))) for x in data.split()] + +grid.update({z: "#" for z in zs[:n_bytes]}) +graph = grid.graph() +path = nx.shortest_path(graph, 0, complex(w-1, w-1)) +print("answer_a:", len(path) - 1) + +for z in zs[n_bytes:]: + grid[z] = "#" + if z not in path: + continue + graph = grid.graph() + try: + path = nx.shortest_path(graph, 0, complex(w-1, w-1)) + except nx.NetworkXNoPath: + print("answer_b:", f"{int(z.real)},{int(z.imag)}") + break diff --git a/aoc_wim/zgrid.py b/aoc_wim/zgrid.py index 34c7b80..0ae98d4 100644 --- a/aoc_wim/zgrid.py +++ b/aoc_wim/zgrid.py @@ -94,6 +94,10 @@ def transform(k): else: raise NotImplementedError(f"{type(initial_data)=}") + @classmethod + def fromempty(cls, width, height, glyph="."): + return cls({z: glyph for z in zrange(0, complex(width, height))}, on=glyph) + def offset(self, dz): self.d = {(z + dz): v for z, v in self.items()} diff --git a/tests/2024/14/0.txt b/tests/2024/14/0.txt index 67f039d..2284815 100644 --- a/tests/2024/14/0.txt +++ b/tests/2024/14/0.txt @@ -10,5 +10,5 @@ p=9,3 v=2,3 p=7,3 v=-1,2 p=2,4 v=2,-3 p=9,5 v=-3,-3 -12 # w=11,h=7 +12 # width=11,height=7 - diff --git a/tests/2024/18/0.txt b/tests/2024/18/0.txt new file mode 100644 index 0000000..4c23fa1 --- /dev/null +++ b/tests/2024/18/0.txt @@ -0,0 +1,27 @@ +5,4 +4,2 +4,5 +3,0 +2,1 +6,3 +2,4 +1,5 +0,6 +3,3 +2,6 +5,1 +1,2 +5,5 +2,5 +6,5 +1,4 +0,4 +6,4 +1,1 +6,1 +1,0 +0,5 +1,6 +2,0 +22 # w=7,n_bytes=12 +6,1