-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20-nope.py
153 lines (112 loc) · 2.86 KB
/
day20-nope.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import sys
inputfile = ""
if len(sys.argv) == 2:
inputfile = sys.argv[1] + ".txt"
else:
inputfile = "input" + sys.argv[0][3:5] + ".txt"
allwalls = []
start = None
endloc = None
max_x = 0
max_y = 0
costs = {}
visited = []
unvisited = []
with open(inputfile,"r") as input1:
for i,line in enumerate(input1):
line = line.rstrip()
for j,val in enumerate(line):
if val == "#":
allwalls.append((i,j))
elif val == "S":
start = (i,j)
elif val == "E":
endloc = (i,j)
if j > max_y:
max_y = j
max_x = i
def find_neighbors(start,walls):
global max_x, max_y
(x,y) = start
nbs = []
if 0 <= x-1 <= max_x and 0 <= y <= max_y and not (x-1,y) in walls:
nbs.append((x-1,y))
if 0 <= x+1 <= max_x and 0 <= y <= max_y and not (x+1,y) in walls:
nbs.append((x+1,y))
if 0 <= x <= max_x and 0 <= y-1 <= max_y and not (x,y-1) in walls:
nbs.append((x,y-1))
if 0 <= x <= max_x and 0 <= y+1 <= max_y and not (x,y+1) in walls:
nbs.append((x,y+1))
return nbs
def find_current(maxmoves = 0):
global unvisited, costs
lowest_val = -1
lowest = None
for x in unvisited:
if x in costs:
if maxmoves > 0 and costs[x] > maxmoves:
return None
if lowest_val == -1 or costs[x] < lowest_val:
lowest_val = costs[x]
lowest = x
if lowest:
return lowest
else:
print("huh?")
#raise Exception("No path found!")
def calc_costs(fr,nbs):
global costs
new_cost = 1 + costs[fr]
for nb in nbs:
if nb in costs and costs[nb] > new_cost:
costs[nb] = new_cost
elif not nb in costs:
costs[nb] = new_cost
def run(walls,maxmoves = 0):
global costs,max_y,max_y,visted,unvisited,endloc
costs = {start: 0}
visited = []
unvisited = []
for x in range(max_x+1):
for y in range(max_y+1):
if not (x,y) in walls:
unvisited.append((x,y))
current = find_current(maxmoves)
while current:
#if len(visited) % 1000 == 0:
# print(len(visited))
if current == endloc:
# print("Found "+str(current))
break
#print("Processing "+str(current))
nbs = find_neighbors(current,walls)
calc_costs(current,nbs)
#print(costs)
visited.append(current)
unvisited.remove(current)
current = find_current()
def run_part1():
global allwalls, costs, endloc
run(allwalls)
default = costs[endloc]
shorter = []
savehowmuch = 100
print(len(allwalls))
i = 0
for w in allwalls:
print(i)
i += 1
newwalls = allwalls.copy()
newwalls.remove(w)
run(newwalls,default-savehowmuch)
if costs[endloc] + savehowmuch <= default:
shorter.append(w)
print(shorter)
print(len(shorter))
def run_part2():
global allcoords,howmany
for i in range(howmany,len(allcoords)):
print(allcoords[:i])
run(allcoords[:i])
run_part1()
#run_part2()