-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.py
222 lines (167 loc) · 4.43 KB
/
day15.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import numpy as np
with open('inputs/day15.txt') as f:
arr = []
for l in f:
if l.strip() == "":
break
arr.append([c for c in l.strip()])
arr = np.array(arr,dtype=str)
moves = ""
for l in f:
moves += l.strip()
X,Y = np.meshgrid(np.arange(arr.shape[1]),np.arange(arr.shape[0]))
wall_x = X[arr == "#"]
wall_y = Y[arr == "#"]
bot_x = X[arr == "@"]
bot_y = Y[arr == "@"]
box_x = X[arr == "O"]
box_y = Y[arr == "O"]
walls = set(zip(wall_x,wall_y))
boxes = set(zip(box_x,box_y))
bot = (bot_x[0],bot_y[0])
def ptr_right(loc):
(x,y) = loc
return (x+1,y)
def ptr_left(loc):
(x,y) = loc
return (x-1,y)
def ptr_up(loc):
(x,y) = loc
return (x,y-1)
def ptr_down(loc):
(x,y) = loc
return (x,y+1)
dir = {
"<": ptr_left,
">": ptr_right,
"^": ptr_up,
"v": ptr_down
}
def move_box(loc,d):
next_pt = dir[d](loc)
if next_pt in walls:
return False
if next_pt in boxes:
r = move_box(next_pt,d)
if not r:
return False
boxes.remove(loc)
boxes.add(next_pt)
return True
def move_bot(loc,d):
global bot
next_pt = dir[d](loc)
if next_pt in walls:
return False
if next_pt in boxes:
r = move_box(next_pt,d)
if not r:
return False
bot = next_pt
return True
for m in moves:
move_bot(bot,m)
gps_total = 0
for (x,y) in boxes:
gps_total += 100*y + x
print(gps_total)
def expand(v):
if v == "#":
return "##"
if v == "@":
return "@."
if v == "O":
return "[]"
if v == ".":
return ".."
arr2 = np.vectorize(expand)(arr)
arr3 = np.zeros((arr2.shape[1],2*arr2.shape[0]),dtype=str)
for i in range(0,arr2.shape[0]):
arr3[i] = list("".join(arr2[i]))
print(arr3)
X2,Y2 = np.meshgrid(np.arange(arr3.shape[1]),np.arange(arr3.shape[0]))
wall_x2 = X2[arr3 == "#"]
wall_y2 = Y2[arr3 == "#"]
box_x2 = X2[arr3 == "["]
box_y2 = Y2[arr3 == "["]
box_x2_r = X2[arr3 == "]"]
box_y2_r = Y2[arr3 == "]"]
bot_x2 = X2[arr3 == "@"]
bot_y2 = Y2[arr3 == "@"]
walls2 = set(zip(wall_x2,wall_y2))
boxes2 = set(zip(box_x2,box_y2))
boxes2_r = set(zip(box_x2_r,box_y2_r))
bot2 = (bot_x2[0],bot_y2[0])
def move_box2(loc,d,move_set=set()):
next_pt = dir[d](loc)
next_r_pt = dir[d](ptr_right(loc))
move_set.add(loc)
move_set.add(ptr_right(loc))
if next_pt in walls2 or next_r_pt in walls2:
return False
if next_pt in boxes2 and next_pt not in move_set:
r = move_box2(next_pt,d,move_set)
if not r:
return False
if next_r_pt in boxes2 and next_r_pt not in move_set:
r = move_box2(next_r_pt,d,move_set)
if not r:
return False
if next_pt in boxes2_r and next_pt not in move_set:
r = move_box2(ptr_left(next_pt),d,move_set)
if not r:
return False
if next_r_pt in boxes2_r and next_r_pt not in move_set:
r = move_box2(ptr_left(next_r_pt),d,move_set)
if not r:
return False
return True
def move_bot2(d):
global bot2
next_pt = dir[d](bot2)
if next_pt in walls2:
#print("failed to move: ",d,(bot2,next_pt), "wall")
return False
moves = set()
if next_pt in boxes2:
r = move_box2(next_pt,d,moves)
if not r:
#print("failed to move: ",d,(bot2,next_pt), "box_block")
return False
if next_pt in boxes2_r:
r = move_box2(ptr_left(next_pt),d,moves)
if not r:
#print("failed to move: ",d,(bot2,next_pt), "box_r_block")
return False
def box2_add(n):
return lambda: boxes2.add(n)
def box2_r_add(n):
return lambda: boxes2_r.add(n)
defer = []
for m in moves:
n = dir[d](m)
if m in boxes2:
boxes2.remove(m)
defer.append(box2_add(n))
if m in boxes2_r:
boxes2_r.remove(m)
defer.append(box2_r_add(n))
for df in defer:
df()
#print("move: ",d,(bot2,next_pt))
bot2 = next_pt
return True
for m in moves:
move_bot2(m)
arr4 = np.zeros(arr3.shape,dtype=str)
arr4[:,:] = "."
for w in walls2:
arr4[w[1],w[0]] = "#"
sum_long = 0
for b in boxes2:
sum_long += 100*b[1] + b[0]
arr4[b[1],b[0]] = "["
for b in boxes2_r:
arr4[b[1],b[0]] = "]"
arr4[bot2[1],bot2[0]] = "@"
print(sum_long)