-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
49 lines (34 loc) · 1.69 KB
/
solution.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
# -*- coding: utf-8 -*-
from heapq import heappop, heappush
def get_map_risk_level_from_input(data):
lines = data.split("\n")
return [list(map(int, line)) for line in lines]
def read_file(filename):
with open(filename) as file:
data = file.read()
return get_map_risk_level_from_input(data)
def get_neighbour_coords(x, y):
return [[x + nx, y + ny] for nx, ny in [(1, 0), (0, 1), (-1, 0), (0, -1)]]
def get_lowest_total_risk(map_risk_level, num_path):
heap = [(0, 0, 0)]
position_checked = {(0, 0)}
while heap:
distance, x, y = heappop(heap)
if x == num_path * len(map_risk_level) - 1 and y == num_path * len(map_risk_level[0]) - 1:
return distance
for neighbour_x, neighbour_y in get_neighbour_coords(x, y):
if neighbour_x < 0 or neighbour_y < 0 \
or neighbour_x >= num_path * len(map_risk_level) or neighbour_y >= num_path * len(map_risk_level):
continue
quotient_x, remainder_x = divmod(neighbour_x, len(map_risk_level))
quotient_y, remainder_y = divmod(neighbour_y, len(map_risk_level[0]))
n = ((map_risk_level[remainder_x][remainder_y] + quotient_x + quotient_y) - 1) % 9 + 1
if (neighbour_x, neighbour_y) not in position_checked:
position_checked.add((neighbour_x, neighbour_y))
heappush(heap, (distance + n, neighbour_x, neighbour_y))
if __name__ == "__main__":
map_risk_level = read_file("input.txt")
first_solution = get_lowest_total_risk(map_risk_level, num_path=1)
print(first_solution)
second_solution = get_lowest_total_risk(map_risk_level, num_path=5)
print(second_solution)