-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday15.py
More file actions
76 lines (60 loc) · 1.59 KB
/
day15.py
File metadata and controls
76 lines (60 loc) · 1.59 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from queue import PriorityQueue
map = []
with open("day15full.txt") as f:
for line in f:
map.append( [int(x) for x in line.strip() ] )
large_map = [ [0] * len(map[0]) * 5 for _ in range(len(map) * 5) ]
for y in range(len(map)):
for x in range(len(map[y])):
for i in range(5):
for j in range(5):
new_risk = map[y][x] + i + j
while new_risk > 9:
new_risk -= 9
large_map[y + len(map)*i][x + len(map[y])*j] = new_risk
#print(large_map)
map = large_map
start = (0,0)
end = ( len(map[0]) - 1, len(map) - 1 )
def heuristic(start, end):
return abs(start[0] - end[0]) + abs(start[1] + end[1])
def neighbours(point):
x, y = point
ns = []
if x > 0:
ns += [(x-1, y)]
if x < end[0]:
ns += [(x+1, y)]
if y > 0:
ns += [(x, y-1)]
if y < end[1]:
ns += [(x, y+1)]
return ns
def travelCostTo(point):
return map[point[1]][point[0]]
frontier = PriorityQueue()
frontier.put(start, 0)
came_from = dict()
cost_so_far = dict()
came_from[start] = None
cost_so_far[start] = 0
while not frontier.empty():
current = frontier.get()
if current == end:
break
for next in neighbours(current):
new_cost = cost_so_far[current] + travelCostTo(next)
if next not in cost_so_far or cost_so_far[next] > new_cost:
cost_so_far[next] = new_cost
priority = cost_so_far[next] + heuristic(next, end)
frontier.put(next, priority)
came_from[next] = current
route = [end]
current = end
while current != start:
prev = came_from[current]
route.append( prev )
current = prev
route.reverse()
print(route)
print(cost_so_far[end])