-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.py
More file actions
83 lines (71 loc) · 2.18 KB
/
15.py
File metadata and controls
83 lines (71 loc) · 2.18 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
76
77
78
79
80
81
82
83
print(chr(27)+'[2j')
print('\033c')
from collections import deque
import heapq
f = open('15.test', 'r')
f = open('15.input', 'r')
lines = [x.strip() for x in f.readlines()]
cave = []
for line in lines:
cave.append([int(x) for x in line])
def print_cave(cave):
print('-- Cave')
for row in cave:
print(''.join([str(x) for x in row]))
def get_neighbours(cave, position):
x,y = position
neighbours = [
(x + 1, y), (x, y + 1),
(x - 1, y), (x, y - 1),
]
return [(nx,ny)
for (nx,ny)
in neighbours
if 0 <= nx < len(cave[0])
and 0 <= ny < len(cave)
]
def quickest_path(cave):
START = (0,0)
ROWS = len(cave)-1
COLS = len(cave[0])-1
queue = [(ROWS+COLS, 0, START)]
heapq.heapify(queue)
cache = {(0,0): 0 }
while len(queue) > 0:
heuristic, path_score, position = heapq.heappop(queue)
if position not in cache or path_score < cache[position]:
cache[position] = path_score
if position in cache and path_score > cache[position]:
continue
neighbours = get_neighbours(cave, position)
next_step = [
(path_score + cave[ny][nx], (nx,ny))
for (nx,ny) in neighbours
]
for step in next_step:
score, pos = step
if pos in cache and cache[pos] <= step[0]:
continue
heuristic = score + (ROWS - pos[1]) * 1.5 + (COLS - pos[0]) * 1.5
queue.append((heuristic, score, pos))
last_pos = (COLS, ROWS)
result = cache[last_pos]
return result
print("Solution part1: %d" % quickest_path(cave))
cave2 = []
ROWS = len(cave)
COLS = len(cave[0])
for y in range(ROWS * 5):
cave2.append([])
ydelta = int(y / ROWS)
for x in range(COLS*5):
xdelta = int(x / COLS)
old_value = cave[y%ROWS][x%COLS]
new_value = (old_value + xdelta + ydelta)
new_value = new_value - 9 if new_value > 9 else new_value
cave2[y].append(new_value)
print("Solution part2: %d" % quickest_path(cave2))
# print_cave(cave2)
# for y in range(ROWS+1):
# print('\t'.join([str(cache[(x,y)]) for x in range(COLS+1)]))
# 360 too low