-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc15.py
103 lines (83 loc) · 2.68 KB
/
aoc15.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Read input
inp = []
# Uncomment this for Part 1
# with open("input15.txt") as f:
# ls = f.readlines()
# for l in ls:
# inp.append([int(c) for c in l.strip()])
# Uncomment this for Part 2
with open("input15.txt") as f:
ls = f.readlines()
for l in ls:
ns = []
n = [int(c) for c in l.strip()]
for i in range(5):
ns += list(map(lambda x: x+i if x+i <=9 else x+i-9,n))
inp.append(ns)
ydim = len(inp)
for i in range(1,5):
for j in range(ydim):
inp.append(list(map(lambda x: x+i if x+i <=9 else x+i-9,inp[j])))
# From here the common part starts
xdim = len(inp[0])
ydim = len(inp)
risk = [[0 for _ in range(xdim)] for _ in range(ydim)]
# First we run a regular dynamic programming routine,
# but this does not account for up and left steps.
s = 0
for i in range(1,xdim):
s = s+inp[0][i]
risk[0][i] = s
s = 0
for i in range(1,ydim):
s = s+inp[i][0]
risk[i][0] = s
for i in range(1,ydim):
for j in range(1,xdim):
m = min(risk[i][j-1],risk[i-1][j])
risk[i][j] = m + inp[i][j]
# Find the places where taking a detour would make the
# path less risky.
def findfaults():
res = []
for i in range(ydim):
for j in range(xdim):
if i+j>0:
surround = []
if i > 0:
surround.append(risk[i-1][j])
if j > 0:
surround.append(risk[i][j-1])
if i < ydim-1:
surround.append(risk[i+1][j])
if j < xdim-1:
surround.append(risk[i][j+1])
if risk[i][j] > min(surround) + inp[i][j]:
res.append((i,j,min(surround) + inp[i][j]))
return res
# Fix the risk estimates
def fixrisks():
for i in range(ydim):
for j in range(xdim):
if i+j>0:
surround = []
if i > 0:
surround.append(risk[i-1][j])
if j > 0:
surround.append(risk[i][j-1])
if i < ydim-1:
surround.append(risk[i+1][j])
if j < xdim-1:
surround.append(risk[i][j+1])
if risk[i][j] > min(surround) + inp[i][j]:
risk[i][j] = min(surround) + inp[i][j]
# Run the suboptimality detection and fixing as long as there is need
# and hope this process won't take forever.
# On my machine it takes about 20 seconds for the full problem.
faults = findfaults()
while faults:
for f in faults:
risk[f[0]][f[1]] = f[2]
fixrisks()
faults = findfaults()
print(risk[ydim-1][xdim-1])