-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC_day18.py
58 lines (43 loc) · 1.63 KB
/
AoC_day18.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
from AoC_day16 import dijkstra
def parse_input(in_file, grid_size, count):
maze = np.ones((grid_size+1, grid_size+1))
with open(in_file, "r") as fh:
for _, line in zip(range(count), fh):
pos = line.strip().split(",")
maze[int(pos[1]), int(pos[0])] = 0
return maze
def get_pixels(in_file):
pixels = []
with open(in_file, "r") as fh:
for line in fh:
pos = line.strip().split(",")
pixels.append((int(pos[1])+1, int(pos[0])+1))
return pixels
def part_1(maze):
end = int(maze.shape[0])
maze = np.pad(maze, pad_width=1, mode='constant', constant_values=0)
return dijkstra(maze, (1,1), (end, end), 0, False)
def part_2(pixels, grid_size):
maze = np.ones((grid_size + 1, grid_size + 1))
end = int(maze.shape[0])
maze = np.pad(maze, pad_width=1, mode='constant', constant_values=0)
for i, pixel in enumerate(pixels):
maze[pixel] = 0
# started with 1024 fro pt.1, needed to re-run a couple of times so picked something closer
if i < 2870:
continue
try:
dijkstra(maze, (1, 1), (end, end), 0, False)
except Exception:
# Input is off-by-one due to padding and backwards.
return (pixel[1]-1, pixel[0]-1)
return maze
input_ex = ("AoC_input/day18_ex.txt", 6, 12)
input_main = ("AoC_input/day18.txt", 70, 1024)
maze = parse_input(*input_main)
score, _ = part_1(maze)
print("Part.1: ", score)
pixels = get_pixels(input_main[0])
block = part_2(pixels, input_main[1])
print("Part.2: ", block)