Files
python-aoc-2021/day15/part1.py
Sebastian Seedorf 93e009c5a8 Day 15
2021-12-15 14:47:47 +01:00

25 lines
773 B
Python

#!/usr/bin/env python3
import heapq
import numpy as np
arr = np.array([[int(n) for n in line.strip()] for line in open("input.txt")])
arr = np.pad(arr, (1, 1), 'constant', constant_values=(9999,))
open_list = [(arr[1, 1], (1, 1))]
closed_list = np.zeros_like(arr)
target = arr[:-2, :-2].shape
while len(open_list):
val, (y, x) = heapq.heappop(open_list)
if (y, x) == target:
print(val - arr[1, 1])
break
if closed_list[y, x] != 0:
continue
closed_list[y, x] = val
for yn, xn in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
if closed_list[y+yn, x+xn] == 0:
heapq.heappush(open_list, (arr[y+yn, x+xn]+val, (y+yn, x+xn)))
print(np.sum(closed_list[1:-1, 1:-1] != 0), arr[1:-1, 1:-1].shape[0]*arr[1:-1, 1:-1].shape[1])