/
day15.py
281 lines (215 loc) · 7.65 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import numpy as np
from dataclasses import dataclass
from collections import deque, namedtuple
@dataclass
class Monster:
x: int
y: int
type: str
hp: int
def load_file(filename):
grid = []
monsters = []
f = open(filename)
for y, line in enumerate(f.readlines()):
n_line = ""
for x, char in enumerate(line):
if char in "GE":
monsters.append(Monster(x, y, char, 200))
n_line += "."
elif char in "#.":
n_line += char
grid.append(n_line)
f.readline()
f.close()
return grid, monsters
class Dungeon:
def __init__(self, grid, monsters):
self.grid = grid
self.__w = len(grid[0])
self.__h = len(grid)
self.update_monsters(monsters)
pass
def update_monsters(self, monsters):
self.monsters = monsters
self.monsters.sort(key=lambda m: m.y * self.__h + m.x)
self._mlocs = {(m.x, m.y): m for m in monsters}
def __getitem__(self, t):
x = t[0]
y = t[1]
if x < 0 or x > self.__w - 1:
return "#"
if y < 0 or y > self.__h - 1:
return "#"
if (x, y) in self._mlocs:
return self._mlocs[(x, y)]
else:
return self.grid[y][x]
def __str__(self):
out = ""
for y in range(self.__h):
monsters = " "
for x in range(self.__w):
c = self[x, y]
if type(c) is Monster and c.hp > 0:
out += c.type
monsters += f"{c.type}({c.hp}) "
else:
out += c
out += monsters + '\n'
return out
def paths_to_enemies(self, m):
enemies = []
def step_to_enemy(loc, prevsteps):
for dir in [(0, -1), (-1, 0), (1, 0), (0, 1)]:
nextloc = (loc[0] + dir[0], loc[1] + dir[1])
if nextloc in prevsteps:
continue
nextcell = self[nextloc]
# print(loc, nextloc, nextcell)
if type(nextcell) is Monster:
if nextcell.type != m.type:
enemies.append((prevsteps, nextcell))
return
else:
continue
if nextcell == '.':
step_to_enemy(nextloc, prevsteps + [nextloc])
else:
continue
step_to_enemy((m.x, m.y), [])
return enemies
def bfspaths_to_enemies(self, m):
enemy_loc = []
seen = set([(m.x, m.y)])
front = deque([((m.x, m.y), None, 0)])
#found = False
while front:
loc, prev, depth = front.popleft()
# visited.add(loc)
# print("visited: ", len(seen), len(front))
for dir in [(0, -1), (-1, 0), (1, 0), (0, 1)]:
nextloc = (loc[0] + dir[0], loc[1] + dir[1])
# if nextloc
if nextloc in seen:
continue
seen.add(nextloc)
cell = self[nextloc]
if type(cell) is Monster and cell.type != m.type:
enemy_loc.append(((loc, prev, depth), cell))
found = True
elif cell == '.':
# if loc not in seen:
front.append((nextloc, (loc, prev, depth), depth+1))
# visited.add(loc)
# print(visited)
enemies = []
for eloc in enemy_loc:
location, enemy = eloc
# print("eloc", eloc)
path = []
curloc = location
while True:
if not curloc[1]:
break
path.append(curloc[0])
curloc = curloc[1]
path.reverse()
enemies.append((enemy, path))
return enemies
def simulate_step(self, elvepower=3):
dungeon.monsters.sort(
key=lambda currmonster: currmonster.y * self.__w + currmonster.x)
def weakest_in_range(m):
enemies = []
for dir in [(0, -1), (-1, 0), (1, 0), (0, 1)]:
loc = m.x+dir[0], m.y + dir[1]
if loc in self._mlocs and self._mlocs[loc].type != m.type:
enemies.append(self._mlocs[loc])
if enemies:
enemies.sort(key=lambda x: x.hp)
return enemies[0]
return None
def path_order(path_to_enemy):
path = path_to_enemy[1]
order = len(path) * self.__w * self.__h
if path:
lastpos = path[-1]
order += self.__w * lastpos[1] + lastpos[0]
return order
monsters_to_move = self.monsters.copy()
while monsters_to_move:
currmonster = monsters_to_move.pop(0)
# print("curr", currmonster)
if currmonster not in self.monsters:
continue
# find paths to monsters
paths = self.bfspaths_to_enemies(currmonster)
paths.sort(key=path_order)
# print(paths)
if paths:
shortestPath = paths[0][1]
# closestEnemy = paths[0][0]
# print("shortest", currmonster, shortestPath)
if shortestPath:
currmonster.x = shortestPath[0][0]
currmonster.y = shortestPath[0][1]
enemy_to_hit = weakest_in_range(currmonster)
if enemy_to_hit:
# print("enemy in range", closestEnemy)
hit = elvepower if currmonster.type == "E" else 3
#print("hitting with: ", hit)
enemy_to_hit.hp -= hit
if enemy_to_hit.hp <= 0:
if enemy_to_hit in monsters_to_move:
monsters_to_move.remove(enemy_to_hit)
if enemy_to_hit in self.monsters:
self.monsters.remove(enemy_to_hit)
self.update_monsters(self.monsters)
#
#
self.update_monsters(self.monsters)
# def simulate_step(self):
grid, monsters = load_file("day15.txt")
step = 0
dungeon = Dungeon(grid.copy(), monsters.copy())
while True:
dungeon.simulate_step()
print("step:", step)
print(dungeon)
goblins = list(filter(lambda m: m.type == "G", dungeon.monsters))
elves = list(filter(lambda m: m.type == "E", dungeon.monsters))
# print(dungeon.monsters)
#print(len(dungeon.monsters), len(goblins), len(elves))
if not goblins or not elves:
print("Solution 1: ", sum(map(lambda x: x.hp, dungeon.monsters)) * step)
break
step += 1
elvepower = 3
found = False
while not found:
grid, monsters = load_file("day15.txt")
dungeon = Dungeon(grid.copy(), monsters.copy())
origElves = len(list(filter(lambda m: m.type == "E", dungeon.monsters)))
elvepower += 1
step = 0
print("elvePower", elvepower)
while True:
dungeon.simulate_step(elvepower)
#print("step:", step)
# print(dungeon)
goblins = list(filter(lambda m: m.type == "G", dungeon.monsters))
elves = list(filter(lambda m: m.type == "E", dungeon.monsters))
if len(elves) < origElves:
print("fight failed")
break
# print(dungeon.monsters)
#print(len(dungeon.monsters), len(goblins), len(elves))
if not goblins:
print("Solution2: ", sum(map(lambda x: x.hp, dungeon.monsters)) * step)
found = True
break
step += 1
# for monster in
# print(grid, monsters)
# print(dungeon)