Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024/18 #130

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aoc_wim/aoc2024/q14.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions aoc_wim/aoc2024/q18.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions aoc_wim/zgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}

Expand Down
2 changes: 1 addition & 1 deletion tests/2024/14/0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
-
27 changes: 27 additions & 0 deletions tests/2024/18/0.txt
Original file line number Diff line number Diff line change
@@ -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
Loading