Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit 271124d

Browse files
authored
Solve both parts
1 parent 4ec50e1 commit 271124d

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Day-18/python/paul2708/day18.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import math
2+
from typing import Tuple
3+
4+
import numpy as np
5+
from scipy.sparse import csr_matrix
6+
from scipy.sparse.csgraph import shortest_path
7+
from tqdm import tqdm
8+
9+
from shared.paul2708.input_reader import *
10+
from shared.paul2708.output import write
11+
12+
lines = read_plain_input(day=18)
13+
14+
SIZE = 71
15+
16+
memory = [(int(line.split(",")[0]), int(line.split(",")[1])) for line in lines]
17+
18+
19+
def position_to_vertex(position: Tuple[int, int]) -> int:
20+
return (position[0] * SIZE) + position[1]
21+
22+
23+
def compute_shortest_path(amount_of_bytes: int) -> np.ndarray:
24+
dim = SIZE * SIZE
25+
adjacency_matrix = np.zeros((dim, dim))
26+
27+
for i in range(SIZE):
28+
for j in range(SIZE):
29+
for x, y in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
30+
if 0 <= i + x < SIZE and 0 <= j + y < SIZE and not (i + x, j + y) in memory[:amount_of_bytes]:
31+
adjacency_matrix[position_to_vertex((i, j))][position_to_vertex((i + x, j + y))] = 1
32+
33+
dist_matrix = shortest_path(csgraph=csr_matrix(adjacency_matrix), indices=0)
34+
return dist_matrix[-1]
35+
36+
37+
# Part 1
38+
write(f"The shortest path after <1024 bytes> requires <{int(compute_shortest_path(1024))} steps>.")
39+
40+
# Part 2
41+
lower = 1
42+
upper = len(memory) - 1
43+
index = (upper + lower) // 2
44+
45+
with tqdm(total=math.ceil(math.log(len(memory), 2))) as progress_bar:
46+
while not (compute_shortest_path(index - 1) != np.inf and compute_shortest_path(index) == np.inf):
47+
if compute_shortest_path(index) != np.inf:
48+
lower = index
49+
else:
50+
upper = index
51+
52+
index = (upper + lower) // 2
53+
54+
progress_bar.update(1)
55+
56+
write(f"The byte <{memory[index - 1]}> destroys the path.")

0 commit comments

Comments
 (0)