Day 22
[advent-of-code-15.git] / advent22.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 5,
6 "metadata": {
7 "collapsed": true
8 },
9 "outputs": [],
10 "source": [
11 "import copy"
12 ]
13 },
14 {
15 "cell_type": "code",
16 "execution_count": 7,
17 "metadata": {
18 "collapsed": false
19 },
20 "outputs": [
21 {
22 "data": {
23 "text/plain": [
24 "[{'boss_hp': -4, 'cost': 53, 'name': 'Magic missile'},\n",
25 " {'boss_hp': -2, 'cost': 73, 'name': 'Drain', 'pc_hp': 2},\n",
26 " {'cost': 113,\n",
27 " 'name': 'Shield',\n",
28 " 'ongoing': [{'armour': 7},\n",
29 " {'armour': 7},\n",
30 " {'armour': 7},\n",
31 " {'armour': 7},\n",
32 " {'armour': 7},\n",
33 " {'armour': 7}]},\n",
34 " {'cost': 173,\n",
35 " 'name': 'Poison',\n",
36 " 'ongoing': [{'boss_hp': -3},\n",
37 " {'boss_hp': -3},\n",
38 " {'boss_hp': -3},\n",
39 " {'boss_hp': -3},\n",
40 " {'boss_hp': -3},\n",
41 " {'boss_hp': -3}]},\n",
42 " {'cost': 229,\n",
43 " 'name': 'Recharge',\n",
44 " 'ongoing': [{'mana': 101},\n",
45 " {'mana': 101},\n",
46 " {'mana': 101},\n",
47 " {'mana': 101},\n",
48 " {'mana': 101}]}]"
49 ]
50 },
51 "execution_count": 7,
52 "metadata": {},
53 "output_type": "execute_result"
54 }
55 ],
56 "source": [
57 "spells = [\n",
58 " {'name': 'Magic missile', 'cost': 53, 'boss_hp': -4},\n",
59 " {'name': 'Drain', 'cost': 73, 'boss_hp': -2, 'pc_hp': 2},\n",
60 " {'name': 'Shield', 'cost': 113, 'ongoing': [{'armour': 7}] * 6},\n",
61 " {'name': 'Poison', 'cost': 173, 'ongoing': [{'boss_hp': -3}] * 6},\n",
62 " {'name': 'Recharge', 'cost': 229, 'ongoing': [{'mana': 101}] * 5}]\n",
63 "spells"
64 ]
65 },
66 {
67 "cell_type": "code",
68 "execution_count": 153,
69 "metadata": {
70 "collapsed": false
71 },
72 "outputs": [],
73 "source": [
74 "initial_state = {'pc_hp': 50, 'mana': 500, 'boss_hp': 58, 'boss_damage': 9, 'ongoing': [], \n",
75 " 'spent': 0, 'cast': []}"
76 ]
77 },
78 {
79 "cell_type": "code",
80 "execution_count": 154,
81 "metadata": {
82 "collapsed": true
83 },
84 "outputs": [],
85 "source": [
86 "def cast_spell(spell, state):\n",
87 " new_state = copy.deepcopy(state)\n",
88 " new_state['mana'] -= spell['cost']\n",
89 " new_state['spent'] += spell['cost']\n",
90 " new_state['cast'] += [spell['name']]\n",
91 " if 'boss_hp' in spell:\n",
92 " new_state['boss_hp'] += spell['boss_hp']\n",
93 " if 'pc_hp' in spell:\n",
94 " new_state['pc_hp'] += spell['pc_hp']\n",
95 " if 'ongoing' in spell:\n",
96 " new_state['ongoing'] += [spell['ongoing']]\n",
97 " return new_state"
98 ]
99 },
100 {
101 "cell_type": "code",
102 "execution_count": 155,
103 "metadata": {
104 "collapsed": false
105 },
106 "outputs": [
107 {
108 "data": {
109 "text/plain": [
110 "{'boss_damage': 9,\n",
111 " 'boss_hp': 54,\n",
112 " 'cast': ['Magic missile'],\n",
113 " 'mana': 447,\n",
114 " 'ongoing': [],\n",
115 " 'pc_hp': 50,\n",
116 " 'spent': 53}"
117 ]
118 },
119 "execution_count": 155,
120 "metadata": {},
121 "output_type": "execute_result"
122 }
123 ],
124 "source": [
125 "cast_spell(spells[0], initial_state)"
126 ]
127 },
128 {
129 "cell_type": "code",
130 "execution_count": 156,
131 "metadata": {
132 "collapsed": false
133 },
134 "outputs": [
135 {
136 "data": {
137 "text/plain": [
138 "{'boss_damage': 9,\n",
139 " 'boss_hp': 58,\n",
140 " 'cast': [],\n",
141 " 'mana': 500,\n",
142 " 'ongoing': [],\n",
143 " 'pc_hp': 50,\n",
144 " 'spent': 0}"
145 ]
146 },
147 "execution_count": 156,
148 "metadata": {},
149 "output_type": "execute_result"
150 }
151 ],
152 "source": [
153 "initial_state"
154 ]
155 },
156 {
157 "cell_type": "code",
158 "execution_count": 157,
159 "metadata": {
160 "collapsed": false
161 },
162 "outputs": [
163 {
164 "data": {
165 "text/plain": [
166 "{'boss_damage': 9,\n",
167 " 'boss_hp': 58,\n",
168 " 'cast': ['Shield', 'Poison'],\n",
169 " 'mana': 214,\n",
170 " 'ongoing': [[{'armour': 7},\n",
171 " {'armour': 7},\n",
172 " {'armour': 7},\n",
173 " {'armour': 7},\n",
174 " {'armour': 7},\n",
175 " {'armour': 7}],\n",
176 " [{'boss_hp': -3},\n",
177 " {'boss_hp': -3},\n",
178 " {'boss_hp': -3},\n",
179 " {'boss_hp': -3},\n",
180 " {'boss_hp': -3},\n",
181 " {'boss_hp': -3}]],\n",
182 " 'pc_hp': 50,\n",
183 " 'spent': 286}"
184 ]
185 },
186 "execution_count": 157,
187 "metadata": {},
188 "output_type": "execute_result"
189 }
190 ],
191 "source": [
192 "s2 = cast_spell(spells[2], initial_state)\n",
193 "cast_spell(spells[3], s2)"
194 ]
195 },
196 {
197 "cell_type": "code",
198 "execution_count": 97,
199 "metadata": {
200 "collapsed": true
201 },
202 "outputs": [],
203 "source": [
204 "def valid_spells(spells, state):\n",
205 " valid_spells = []\n",
206 " for spell in spells:\n",
207 " add_this_spell = True\n",
208 " if spell['cost'] > state['mana']:\n",
209 " add_this_spell = False\n",
210 " if 'ongoing' in spell:\n",
211 " for s in spell['ongoing'][0]:\n",
212 " for status in state['ongoing']:\n",
213 " if s in status[0]:\n",
214 " add_this_spell = False\n",
215 " if add_this_spell:\n",
216 " valid_spells += [spell]\n",
217 " return valid_spells"
218 ]
219 },
220 {
221 "cell_type": "code",
222 "execution_count": 98,
223 "metadata": {
224 "collapsed": false
225 },
226 "outputs": [
227 {
228 "data": {
229 "text/plain": [
230 "[{'boss_hp': -4, 'cost': 53, 'name': 'Magic missile'},\n",
231 " {'boss_hp': -2, 'cost': 73, 'name': 'Drain', 'pc_hp': 2},\n",
232 " {'cost': 113,\n",
233 " 'name': 'Shield',\n",
234 " 'ongoing': [{'armour': 7},\n",
235 " {'armour': 7},\n",
236 " {'armour': 7},\n",
237 " {'armour': 7},\n",
238 " {'armour': 7},\n",
239 " {'armour': 7}]},\n",
240 " {'cost': 173,\n",
241 " 'name': 'Poison',\n",
242 " 'ongoing': [{'boss_hp': -3},\n",
243 " {'boss_hp': -3},\n",
244 " {'boss_hp': -3},\n",
245 " {'boss_hp': -3},\n",
246 " {'boss_hp': -3},\n",
247 " {'boss_hp': -3}]},\n",
248 " {'cost': 229,\n",
249 " 'name': 'Recharge',\n",
250 " 'ongoing': [{'mana': 101},\n",
251 " {'mana': 101},\n",
252 " {'mana': 101},\n",
253 " {'mana': 101},\n",
254 " {'mana': 101}]}]"
255 ]
256 },
257 "execution_count": 98,
258 "metadata": {},
259 "output_type": "execute_result"
260 }
261 ],
262 "source": [
263 "valid_spells(spells, initial_state)"
264 ]
265 },
266 {
267 "cell_type": "code",
268 "execution_count": 158,
269 "metadata": {
270 "collapsed": false
271 },
272 "outputs": [
273 {
274 "data": {
275 "text/plain": [
276 "{'boss_damage': 9,\n",
277 " 'boss_hp': 58,\n",
278 " 'cast': ['Shield', 'Poison'],\n",
279 " 'mana': 214,\n",
280 " 'ongoing': [[{'armour': 7},\n",
281 " {'armour': 7},\n",
282 " {'armour': 7},\n",
283 " {'armour': 7},\n",
284 " {'armour': 7},\n",
285 " {'armour': 7}],\n",
286 " [{'boss_hp': -3},\n",
287 " {'boss_hp': -3},\n",
288 " {'boss_hp': -3},\n",
289 " {'boss_hp': -3},\n",
290 " {'boss_hp': -3},\n",
291 " {'boss_hp': -3}]],\n",
292 " 'pc_hp': 50,\n",
293 " 'spent': 286}"
294 ]
295 },
296 "execution_count": 158,
297 "metadata": {},
298 "output_type": "execute_result"
299 }
300 ],
301 "source": [
302 "s2 = cast_spell(spells[2], initial_state)\n",
303 "s3 = cast_spell(spells[3], s2)\n",
304 "s3"
305 ]
306 },
307 {
308 "cell_type": "code",
309 "execution_count": 159,
310 "metadata": {
311 "collapsed": false
312 },
313 "outputs": [
314 {
315 "data": {
316 "text/plain": [
317 "[{'boss_hp': -4, 'cost': 53, 'name': 'Magic missile'},\n",
318 " {'boss_hp': -2, 'cost': 73, 'name': 'Drain', 'pc_hp': 2}]"
319 ]
320 },
321 "execution_count": 159,
322 "metadata": {},
323 "output_type": "execute_result"
324 }
325 ],
326 "source": [
327 "valid_spells(spells, s3)"
328 ]
329 },
330 {
331 "cell_type": "code",
332 "execution_count": 48,
333 "metadata": {
334 "collapsed": true
335 },
336 "outputs": [],
337 "source": [
338 "def boss_turn(state):\n",
339 " new_state = apply_ongoing(state)\n",
340 " if new_state['boss_hp'] > 0:\n",
341 " new_state['pc_hp'] -= max(new_state['boss_damage'] - new_state['armour'], 1)\n",
342 " return new_state"
343 ]
344 },
345 {
346 "cell_type": "code",
347 "execution_count": 67,
348 "metadata": {
349 "collapsed": true
350 },
351 "outputs": [],
352 "source": [
353 "def apply_ongoing(state):\n",
354 " new_state = copy.deepcopy(state)\n",
355 " new_state['armour'] = 0\n",
356 " new_state['ongoing'] = []\n",
357 " for status in state['ongoing']:\n",
358 " for k in status[0]:\n",
359 " new_state[k] += status[0][k]\n",
360 " if len(status) > 1:\n",
361 " new_state['ongoing'] += [status[1:]]\n",
362 " return new_state"
363 ]
364 },
365 {
366 "cell_type": "code",
367 "execution_count": 160,
368 "metadata": {
369 "collapsed": false
370 },
371 "outputs": [
372 {
373 "data": {
374 "text/plain": [
375 "{'armour': 7,\n",
376 " 'boss_damage': 9,\n",
377 " 'boss_hp': 55,\n",
378 " 'cast': ['Shield', 'Poison'],\n",
379 " 'mana': 214,\n",
380 " 'ongoing': [[{'armour': 7},\n",
381 " {'armour': 7},\n",
382 " {'armour': 7},\n",
383 " {'armour': 7},\n",
384 " {'armour': 7}],\n",
385 " [{'boss_hp': -3},\n",
386 " {'boss_hp': -3},\n",
387 " {'boss_hp': -3},\n",
388 " {'boss_hp': -3},\n",
389 " {'boss_hp': -3}]],\n",
390 " 'pc_hp': 48,\n",
391 " 'spent': 286}"
392 ]
393 },
394 "execution_count": 160,
395 "metadata": {},
396 "output_type": "execute_result"
397 }
398 ],
399 "source": [
400 "boss_turn(s3)"
401 ]
402 },
403 {
404 "cell_type": "code",
405 "execution_count": 161,
406 "metadata": {
407 "collapsed": false
408 },
409 "outputs": [
410 {
411 "data": {
412 "text/plain": [
413 "{'armour': 0,\n",
414 " 'boss_damage': 8,\n",
415 " 'boss_hp': 10,\n",
416 " 'cast': ['Poison'],\n",
417 " 'mana': 77,\n",
418 " 'ongoing': [[{'boss_hp': -3},\n",
419 " {'boss_hp': -3},\n",
420 " {'boss_hp': -3},\n",
421 " {'boss_hp': -3},\n",
422 " {'boss_hp': -3}]],\n",
423 " 'pc_hp': 2,\n",
424 " 'spent': 173}"
425 ]
426 },
427 "execution_count": 161,
428 "metadata": {},
429 "output_type": "execute_result"
430 }
431 ],
432 "source": [
433 "test_state_1 = {'pc_hp': 10, 'mana': 250, 'boss_hp': 13, 'boss_damage': 8, 'ongoing': [], 'spent': 0, 'cast': []}\n",
434 "s2 = cast_spell([s for s in spells if s['name'] == 'Poison'][0], test_state_1)\n",
435 "s3 = boss_turn(s2)\n",
436 "s3"
437 ]
438 },
439 {
440 "cell_type": "code",
441 "execution_count": 162,
442 "metadata": {
443 "collapsed": false
444 },
445 "outputs": [
446 {
447 "data": {
448 "text/plain": [
449 "{'armour': 0,\n",
450 " 'boss_damage': 8,\n",
451 " 'boss_hp': 0,\n",
452 " 'cast': ['Poison', 'Magic missile'],\n",
453 " 'mana': 24,\n",
454 " 'ongoing': [[{'boss_hp': -3}, {'boss_hp': -3}, {'boss_hp': -3}]],\n",
455 " 'pc_hp': 2,\n",
456 " 'spent': 226}"
457 ]
458 },
459 "execution_count": 162,
460 "metadata": {},
461 "output_type": "execute_result"
462 }
463 ],
464 "source": [
465 "s4 = apply_ongoing(s3)\n",
466 "s5 = cast_spell([s for s in spells if s['name'] == 'Magic missile'][0], s4)\n",
467 "s6 = boss_turn(s5)\n",
468 "s6"
469 ]
470 },
471 {
472 "cell_type": "code",
473 "execution_count": 163,
474 "metadata": {
475 "collapsed": false
476 },
477 "outputs": [
478 {
479 "data": {
480 "text/plain": [
481 "{'armour': 0,\n",
482 " 'boss_damage': 8,\n",
483 " 'boss_hp': 14,\n",
484 " 'cast': ['Recharge'],\n",
485 " 'mana': 122,\n",
486 " 'ongoing': [[{'mana': 101}, {'mana': 101}, {'mana': 101}, {'mana': 101}]],\n",
487 " 'pc_hp': 2,\n",
488 " 'spent': 229}"
489 ]
490 },
491 "execution_count": 163,
492 "metadata": {},
493 "output_type": "execute_result"
494 }
495 ],
496 "source": [
497 "test_state_2 = {'pc_hp': 10, 'mana': 250, 'boss_hp': 14, 'boss_damage': 8, 'ongoing': [], 'spent': 0, 'cast': []}\n",
498 "s2 = cast_spell([s for s in spells if s['name'] == 'Recharge'][0], test_state_2)\n",
499 "s3 = boss_turn(s2)\n",
500 "s3"
501 ]
502 },
503 {
504 "cell_type": "code",
505 "execution_count": 164,
506 "metadata": {
507 "collapsed": false
508 },
509 "outputs": [
510 {
511 "data": {
512 "text/plain": [
513 "{'armour': 7,\n",
514 " 'boss_damage': 8,\n",
515 " 'boss_hp': 14,\n",
516 " 'cast': ['Recharge', 'Shield'],\n",
517 " 'mana': 211,\n",
518 " 'ongoing': [[{'mana': 101}, {'mana': 101}],\n",
519 " [{'armour': 7}, {'armour': 7}, {'armour': 7}, {'armour': 7}, {'armour': 7}]],\n",
520 " 'pc_hp': 1,\n",
521 " 'spent': 342}"
522 ]
523 },
524 "execution_count": 164,
525 "metadata": {},
526 "output_type": "execute_result"
527 }
528 ],
529 "source": [
530 "s4 = apply_ongoing(s3)\n",
531 "s5 = cast_spell([s for s in spells if s['name'] == 'Shield'][0], s4)\n",
532 "s6 = boss_turn(s5)\n",
533 "s6"
534 ]
535 },
536 {
537 "cell_type": "code",
538 "execution_count": 165,
539 "metadata": {
540 "collapsed": false
541 },
542 "outputs": [
543 {
544 "data": {
545 "text/plain": [
546 "{'armour': 7,\n",
547 " 'boss_damage': 8,\n",
548 " 'boss_hp': 12,\n",
549 " 'cast': ['Recharge', 'Shield', 'Drain'],\n",
550 " 'mana': 340,\n",
551 " 'ongoing': [[{'armour': 7}, {'armour': 7}, {'armour': 7}]],\n",
552 " 'pc_hp': 2,\n",
553 " 'spent': 415}"
554 ]
555 },
556 "execution_count": 165,
557 "metadata": {},
558 "output_type": "execute_result"
559 }
560 ],
561 "source": [
562 "s7 = apply_ongoing(s6)\n",
563 "s8 = cast_spell([s for s in spells if s['name'] == 'Drain'][0], s7)\n",
564 "s9 = boss_turn(s8)\n",
565 "s9"
566 ]
567 },
568 {
569 "cell_type": "code",
570 "execution_count": 166,
571 "metadata": {
572 "collapsed": false
573 },
574 "outputs": [
575 {
576 "data": {
577 "text/plain": [
578 "{'armour': 7,\n",
579 " 'boss_damage': 8,\n",
580 " 'boss_hp': 9,\n",
581 " 'cast': ['Recharge', 'Shield', 'Drain', 'Poison'],\n",
582 " 'mana': 167,\n",
583 " 'ongoing': [[{'armour': 7}],\n",
584 " [{'boss_hp': -3},\n",
585 " {'boss_hp': -3},\n",
586 " {'boss_hp': -3},\n",
587 " {'boss_hp': -3},\n",
588 " {'boss_hp': -3}]],\n",
589 " 'pc_hp': 1,\n",
590 " 'spent': 588}"
591 ]
592 },
593 "execution_count": 166,
594 "metadata": {},
595 "output_type": "execute_result"
596 }
597 ],
598 "source": [
599 "s10 = apply_ongoing(s9)\n",
600 "s11 = cast_spell([s for s in spells if s['name'] == 'Poison'][0], s10)\n",
601 "s12 = boss_turn(s11)\n",
602 "s12"
603 ]
604 },
605 {
606 "cell_type": "code",
607 "execution_count": 167,
608 "metadata": {
609 "collapsed": false
610 },
611 "outputs": [
612 {
613 "data": {
614 "text/plain": [
615 "{'armour': 0,\n",
616 " 'boss_damage': 8,\n",
617 " 'boss_hp': -1,\n",
618 " 'cast': ['Recharge', 'Shield', 'Drain', 'Poison', 'Magic missile'],\n",
619 " 'mana': 114,\n",
620 " 'ongoing': [[{'boss_hp': -3}, {'boss_hp': -3}, {'boss_hp': -3}]],\n",
621 " 'pc_hp': 1,\n",
622 " 'spent': 641}"
623 ]
624 },
625 "execution_count": 167,
626 "metadata": {},
627 "output_type": "execute_result"
628 }
629 ],
630 "source": [
631 "s13 = apply_ongoing(s12)\n",
632 "s14 = cast_spell([s for s in spells if s['name'] == 'Magic missile'][0], s13)\n",
633 "s15 = boss_turn(s14)\n",
634 "s15"
635 ]
636 },
637 {
638 "cell_type": "code",
639 "execution_count": 90,
640 "metadata": {
641 "collapsed": true
642 },
643 "outputs": [],
644 "source": [
645 "def finished(state):\n",
646 " return state['boss_hp'] <= 0 or state['pc_hp'] <= 0\n",
647 "\n",
648 "def victory(state):\n",
649 " return finished(state) and state['pc_hp'] > 0\n",
650 "\n",
651 "def defeat(state):\n",
652 " return finished(state) and state['pc_hp'] <= 0"
653 ]
654 },
655 {
656 "cell_type": "code",
657 "execution_count": 125,
658 "metadata": {
659 "collapsed": false
660 },
661 "outputs": [],
662 "source": [
663 "def ahistoric(state):\n",
664 " return {k: state[k] for k in state if k != 'cast'}"
665 ]
666 },
667 {
668 "cell_type": "code",
669 "execution_count": 186,
670 "metadata": {
671 "collapsed": false
672 },
673 "outputs": [
674 {
675 "data": {
676 "text/plain": [
677 "{'armour': 7,\n",
678 " 'boss_damage': 9,\n",
679 " 'boss_hp': 0,\n",
680 " 'cast': ['Poison',\n",
681 " 'Recharge',\n",
682 " 'Drain',\n",
683 " 'Poison',\n",
684 " 'Recharge',\n",
685 " 'Shield',\n",
686 " 'Poison',\n",
687 " 'Magic missile',\n",
688 " 'Magic missile'],\n",
689 " 'mana': 241,\n",
690 " 'ongoing': [[{'boss_hp': -3}, {'boss_hp': -3}]],\n",
691 " 'pc_hp': 1,\n",
692 " 'spent': 1269}"
693 ]
694 },
695 "execution_count": 186,
696 "metadata": {},
697 "output_type": "execute_result"
698 }
699 ],
700 "source": [
701 "agenda = [initial_state]\n",
702 "closed = []\n",
703 "while agenda:\n",
704 " current_state = agenda[0]\n",
705 " new_states = []\n",
706 " if ahistoric(current_state) not in closed:\n",
707 " closed += [ahistoric(current_state)]\n",
708 " # print(current_state)\n",
709 " if victory(current_state):\n",
710 " # return current_state\n",
711 " break\n",
712 " for spell in valid_spells(spells, current_state):\n",
713 " s2 = cast_spell(spell, current_state)\n",
714 " if victory(s2):\n",
715 " new_states += [s2]\n",
716 " else:\n",
717 " s3 = boss_turn(s2)\n",
718 " if victory(s3):\n",
719 " new_states += [s3]\n",
720 " if not finished(s3):\n",
721 " new_states += [apply_ongoing(s3)]\n",
722 " # print(new_states)\n",
723 " states_to_add = [s for s in new_states \n",
724 " if ahistoric(s) not in closed\n",
725 " if len(s['cast']) <= 50]\n",
726 " agenda = sorted(states_to_add + agenda[1:], key=lambda s: s['spent'])\n",
727 " # agenda = new_states + agenda[1:]\n",
728 "current_state"
729 ]
730 },
731 {
732 "cell_type": "markdown",
733 "metadata": {},
734 "source": [
735 "#Part 2"
736 ]
737 },
738 {
739 "cell_type": "code",
740 "execution_count": 182,
741 "metadata": {
742 "collapsed": true
743 },
744 "outputs": [],
745 "source": [
746 "def player_bleed(state):\n",
747 " new_state = copy.deepcopy(state)\n",
748 " new_state['pc_hp'] -= 1\n",
749 " return new_state"
750 ]
751 },
752 {
753 "cell_type": "code",
754 "execution_count": 185,
755 "metadata": {
756 "collapsed": false
757 },
758 "outputs": [
759 {
760 "data": {
761 "text/plain": [
762 "{'armour': 0,\n",
763 " 'boss_damage': 9,\n",
764 " 'boss_hp': -1,\n",
765 " 'cast': ['Poison',\n",
766 " 'Recharge',\n",
767 " 'Shield',\n",
768 " 'Poison',\n",
769 " 'Recharge',\n",
770 " 'Shield',\n",
771 " 'Poison',\n",
772 " 'Magic missile',\n",
773 " 'Magic missile'],\n",
774 " 'mana': 201,\n",
775 " 'ongoing': [[{'boss_hp': -3}]],\n",
776 " 'pc_hp': 12,\n",
777 " 'spent': 1309}"
778 ]
779 },
780 "execution_count": 185,
781 "metadata": {},
782 "output_type": "execute_result"
783 }
784 ],
785 "source": [
786 "agenda = [initial_state]\n",
787 "closed = []\n",
788 "while agenda:\n",
789 " current_state = agenda[0]\n",
790 " new_states = []\n",
791 " if ahistoric(current_state) not in closed:\n",
792 " closed += [ahistoric(current_state)]\n",
793 " # print(current_state)\n",
794 " if victory(current_state):\n",
795 " # return current_state\n",
796 " break\n",
797 " for spell in valid_spells(spells, current_state):\n",
798 " s2 = cast_spell(spell, current_state)\n",
799 " if victory(s2):\n",
800 " new_states += [s2]\n",
801 " else:\n",
802 " s3 = boss_turn(s2)\n",
803 " if victory(s3):\n",
804 " new_states += [s3]\n",
805 " s4 = player_bleed(s3)\n",
806 " if not finished(s4):\n",
807 " new_states += [apply_ongoing(s4)]\n",
808 " # print(new_states)\n",
809 " states_to_add = [s for s in new_states \n",
810 " if ahistoric(s) not in closed\n",
811 " if len(s['cast']) <= 50]\n",
812 " agenda = sorted(states_to_add + agenda[1:], key=lambda s: s['spent'])\n",
813 " # agenda = new_states + agenda[1:]\n",
814 "current_state"
815 ]
816 },
817 {
818 "cell_type": "code",
819 "execution_count": null,
820 "metadata": {
821 "collapsed": true
822 },
823 "outputs": [],
824 "source": []
825 }
826 ],
827 "metadata": {
828 "kernelspec": {
829 "display_name": "Python 3",
830 "language": "python",
831 "name": "python3"
832 },
833 "language_info": {
834 "codemirror_mode": {
835 "name": "ipython",
836 "version": 3
837 },
838 "file_extension": ".py",
839 "mimetype": "text/x-python",
840 "name": "python",
841 "nbconvert_exporter": "python",
842 "pygments_lexer": "ipython3",
843 "version": "3.4.3"
844 }
845 },
846 "nbformat": 4,
847 "nbformat_minor": 0
848 }