From d4f482804b9777768fa4f9fedde0cd549578502b Mon Sep 17 00:00:00 2001 From: Brian Chu Date: Sun, 19 Dec 2021 22:57:37 -0800 Subject: catch up on solutions up to day 20 --- day15.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 day15.py (limited to 'day15.py') diff --git a/day15.py b/day15.py new file mode 100644 index 0000000..90aaed3 --- /dev/null +++ b/day15.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python + +import numpy as np +import math +from heapq import * +from collections import defaultdict + +with open('day15.txt') as data: + costs = [] + for line in data: + costs.append(list(line.strip())) + costs = np.array(costs).astype(int) + +def a_star(costs) -> int: + def h(point): + m, n = point + # approximate with straight line distance between points + return math.sqrt((costs.shape[0]-m)**2 + (costs.shape[1]-n)**2) + + def neighbors(point): + m, n = point + neighboring = {(0, 1), (1, 0), (0, -1), (-1, 0)} + return {(m+dx, n+dy) for dx, dy in neighboring + if 0<=m+dx 9] = 1 + +xDim, yDim = costs.shape +for i in range(5): + for j in range(5): + copied = np.copy(costs) + for _ in range(i+j): + increment(copied) + new_costs[xDim*i:xDim*(i+1), yDim*j:yDim*(j+1)] = copied + +print(a_star(new_costs)) -- cgit 1.4.1-2-gfad0