Day 23
authorNeil Smith <neil.git@njae.me.uk>
Wed, 23 Dec 2015 09:54:37 +0000 (09:54 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Wed, 23 Dec 2015 09:54:37 +0000 (09:54 +0000)
SIGNED.md
advent-of-code-23.html [new file with mode: 0644]
advent22.ipynb
advent23.ipynb [new file with mode: 0644]
advent23.txt [new file with mode: 0644]

index 8335e78d7b8b0a58a75dec3b862cae68dca6c429..5b357c829391d5145501a19ebaf498a246d00a29 100644 (file)
--- a/SIGNED.md
+++ b/SIGNED.md
@@ -3,19 +3,19 @@
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v2
 
-iQIcBAABCAAGBQJWeTulAAoJEJPB2e07PgbqQswP/0s/iRr+GK90BD6KAbd3ni5J
-Q0ih6b1dWRtLkpBBGzajjlKFt9xoygVjV2CjiH/f2bYH+GJQToT3QBOTRGr+7lC1
-s9UhGmFkHvZC9B2bG9Y/5D15/LCZFONMWJ23HyfIMKUNY4nX5zPf0zje7wbkSj1h
-jdZBrTS9I2UVrA6JT5beKUy0m8fih+WpdlsIV+fbc3TmXgRNwJ5gTtVg8vV2H3SY
-X8cZOYfV2cnhqQxqRclRdncypZNcpRC1vyLja2imchHVaDOB741LD42EZi6gOidH
-4WhlROqWDfdRZI4/Xk4Ugpq33OyjK95Ag75jOX1tt2vUYouZlHortIZArzjT29R/
-nR2F1tY+r8zwy6x03YKtb/bWn1zbgzp8XWFKl6qwDIMOuQEJru8u7foREJRAcESt
-A7PaOpy1YGD908/1Bc1vwvdWenUKlviRULWJoQUuCGmAkI2Ri9CfEEZxcK7pXmZl
-Kkj47XnXLXz+jtBiGG0aj+dt6wbKBOqbXY7eU3CZW5zInQAhEp01I4jcAFTIeuE8
-0Kco/OeDiocDshPfjBLhffakipP9J5bmg8fiIEAU2p+d/4nYmH5neoVxq1WXrSa6
-p6CCTU5DVmueiDg9Ohi+597vD1ylXYEAfzbQaWiQj2N/6Ghe1qwdRYV9omX2l2Wo
-jay9FFBiKYx5GtYn86O3
-=qQcY
+iQIcBAABCAAGBQJWem9kAAoJEJPB2e07PgbqXbEP/i+Et9fESLyxCAVENqKQ33kc
+Jy4uadsQxVzW2LJJeSwvRDVmnxldBuTC+bS72SusNGDP7OcEs75JeKmo1E6xSmJJ
+idh0u1d2hKsSlqn1WssrIipW8QjzRE/xPCNI5i0Pso3JMFDx7z1/yfGWDq9BcKk9
+4lG4utqsxe5O8OIkBcb4Fxy+nhsqUQb3WGdt+y0ODGZAqaELsSX0lkNOUhz35foR
+/ZNwvay6YHW3hk/+GO+4Bs5oqy1YDL5rDDtv6ov55dGIL/Vfel0bOcFldrcvH6KD
+oxX71Vd+3ic8GkMllYNZQIn9wYkNEF6ygFQ7UOBC3t75xS7XtaTJIRjqoaXOTD4c
+Fj1pt0nRaMac3TPT+7DG6o4TKmQqGHEpos3m6ubE9mWVldVHUKj3kBZ+P1qv4j4j
+GSCg3BKNbyU4umNtrmAWoijAX9nB6djwPg5DOy+WR01JlhhVpwUiuqmahunnYNqc
+/MljQi/aE3D4vrb+nrViw1JItuHrcjjSritVkttsukNxZ23z1L8rQ4DCmBjav1H6
+tKKSgxQcTncCGRZlAB5e0KIa2dkUvn0Itt3BBGV+ASWULYRaK9Kn3edt627pbZbo
+AqVytWEI+yEHP20w9Gx8DRILu0ceXLI8yq2/6PkjNgk4/FTmVSRVIeH6zecKRlq2
+GLqISlGsKEU+5IwHAUjK
+=AHmD
 -----END PGP SIGNATURE-----
 
 ```
@@ -56,6 +56,7 @@ size    exec  file                        contents
 6387            advent-of-code-20.html    3ec26a7cfb58a168b62781f097ebdb71ae3224cadde49588463370faaa3d33f9
 7780            advent-of-code-21.html    877675e47121bd4812ab461fd7cd6124c397669a5e6f35c3f19b023728976970
 10190           advent-of-code-22.html    cb57ad829bd31e59807f3f0ecc95c603eb85b2d9f9e5cbf9432e80283b689298
+6910            advent-of-code-23.html    fff58431db66635a7dc9b537f3d0d87a10ac9b20acd6fcb9cf5f8c99e6f40f39
 109395          advent-of-code.ipynb      7292eeb9a8019f6931037c70105ab96bf691074dbe963736b6429cb4d4373f51
 25607           advent01.ipynb            c33ad39a77803a6870dd74998da98e3bb9c2c2db37c34167b330a01d663717e7
 7001            advent01.txt              79312922877bdedd09ce0886a42b3d7f7ed092e2218579fb7d6ac1cb38cedebe
@@ -96,7 +97,9 @@ size    exec  file                        contents
 3377            advent20.ipynb            be9db2beee6805ac9db9f2dc145abba3275964e36e21580122bacd8436777a30
 9373            advent21.ipynb            70f49b67c911f9316c6be1404b57c76bd0b8aca94e16dd6fc1e5b39d3442b2b4
 35              advent21.txt              6c4c3bcec0bf45ee485c7595a6a5eb2ad7722bdecf275780e1cdb9993eb931d8
-21424           advent22.ipynb            8b8aac9e511209cba4637523266f00f40be7ac70eec6d1f4dd0d0b50cb64dd3f
+25431           advent22.ipynb            7eb725b13c6fc2bdadb19307330615761c297ec7bb3433cd9e3dd1da41dc940e
+4882            advent23.ipynb            fc19cc164aa4d5ddd0a8cbe930f09bd893612dae5b1a3b73b4579db9e2d41fe0
+298             advent23.txt              780557c20cbab27946114d4cf04359045dd76b6bfdb28b5bde2e1ceb2c520173
 ```
 
 #### Ignore
diff --git a/advent-of-code-23.html b/advent-of-code-23.html
new file mode 100644 (file)
index 0000000..69cc5d2
--- /dev/null
@@ -0,0 +1,150 @@
+<!DOCTYPE html>
+<html lang="en-us">
+<head>
+<meta charset="utf-8"/>
+<title>Day 23 - Advent of Code</title>
+<!--[if lt IE 9]><script src="/static/html5.js"></script><![endif]-->
+<link href='//fonts.googleapis.com/css?family=Source+Code+Pro:300&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
+<link rel="stylesheet" type="text/css" href="/static/style.css?3"/>
+<link rel="shortcut icon" href="/favicon.ico?2"/>
+</head><!--
+
+
+
+
+Oh, hello!  Funny seeing you here.
+
+I appreciate your enthusiasm, but you aren't going to find much down here.
+There certainly aren't clues to any of the puzzles.  You do risk spoiling a few
+surprises for yourself, though.  Best to play the normal way and discover
+everything as it was intended, I think.  The best surprises don't even appear
+in the source until you unlock them for real.
+
+Please be careful with automated requests; I'm not Google, and I can only take
+so much traffic.  Please be considerate so that everyone gets to play.
+
+If you're curious about how Advent of Code works, it's running on some custom
+Perl code. Other than a few integrations (auth, analytics, ads, social media),
+I built the whole thing myself, including the design, animations, prose, and
+all of the puzzles.
+
+The puzzles probably took the longest; the easiest ones were around 45 minutes
+each, but the harder ones took 2-3 hours, some even longer than that. A lot of
+effort went into building this thing - I hope you're enjoying playing it as
+much as I enjoyed making it for you!
+
+If you'd like to hang out, I'm @ericwastl on Twitter.
+
+- Eric Wastl
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+-->
+<body>
+<header><h1><a href="/">Advent of Code</a></h1><div class="user">Neil Smith <span class="star-count">46*</span></div><nav><ul><li><a href="/about">[About]</a></li><li><a href="/stats">[Stats]</a></li><li><a href="/leaderboard">[Leaderboard]</a></li><li><a href="/settings">[Settings]</a></li><li><a href="/auth/logout">[Log out]</a></li></ul></nav></header>
+
+<div id="ad">
+<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
+<!-- Advent of Code Wide Skyscraper -->
+<ins class="adsbygoogle"
+     style="display:inline-block;width:160px;height:600px"
+     data-ad-client="ca-pub-9420604735624631"
+     data-ad-slot="8014013294"></ins>
+<script>
+(adsbygoogle = window.adsbygoogle || []).push({});
+</script>
+</div><!--/ad-->
+
+<main>
+<article class="day-desc"><h2>--- Day 23: Opening the Turing Lock ---</h2><p>Little Jane Marie just got her very first computer for Christmas from some <span title="Definitely not Wintermute.">unknown benefactor</span>.  It comes with instructions and an example program, but the computer itself seems to be malfunctioning.  She's curious what the program does, and would like you to help her run it.</p>
+<p>The manual explains that the computer supports two <a href="https://en.wikipedia.org/wiki/Processor_register">registers</a> and six <a href="https://en.wikipedia.org/wiki/Instruction_set">instructions</a> (truly, it goes on to remind the reader, a state-of-the-art technology). The registers are named <code>a</code> and <code>b</code>, can hold any <a href="https://en.wikipedia.org/wiki/Natural_number">non-negative integer</a>, and begin with a value of <code>0</code>.  The instructions are as follows:</p>
+<ul>
+<li><code>hlf r</code> sets register <code>r</code> to <em>half</em> its current value, then continues with the next instruction.</li>
+<li><code>tpl r</code> sets register <code>r</code> to <em>triple</em> its current value, then continues with the next instruction.</li>
+<li><code>inc r</code> <em>increments</em> register <code>r</code>, adding <code>1</code> to it, then continues with the next instruction.</li>
+<li><code>jmp offset</code> is a <em>jump</em>; it continues with the instruction <code>offset</code> away <em>relative to itself</em>.</li>
+<li><code>jie r, offset</code> is like <code>jmp</code>, but only jumps if register <code>r</code> is <em>even</em> ("jump if even").</li>
+<li><code>jio r, offset</code> is like <code>jmp</code>, but only jumps if register <code>r</code> is <code>1</code> ("jump if <em>one</em>", not odd).</li>
+</ul>
+<p>All three jump instructions work with an <em>offset</em> relative to that instruction.  The offset is always written with a prefix <code>+</code> or <code>-</code> to indicate the direction of the jump (forward or backward, respectively).  For example, <code>jmp +1</code> would simply continue with the next instruction, while <code>jmp +0</code> would continuously jump back to itself forever.</p>
+<p>The program exits when it tries to run an instruction beyond the ones defined.</p>
+<p>For example, this program sets <code>a</code> to <code>2</code>, because the <code>jio</code> instruction causes it to skip the <code>tpl</code> instruction:</p>
+<pre><code>inc a
+jio a, +2
+tpl a
+inc a
+</code></pre>
+<p>What is <em>the value in register <code>b</code></em> when the program in your puzzle input is finished executing?</p>
+</article>
+<p>Your puzzle answer was <code>307</code>.</p><article class="day-desc"><h2>--- Part Two ---</h2><p>The unknown benefactor is <em>very</em> thankful for releasi-- er, helping little Jane Marie with her computer.  Definitely not to distract you, what is the value in register <code>b</code> after the program is finished executing if register <code>a</code> starts as <code>1</code> instead?</p>
+</article>
+<p>Your puzzle answer was <code>160</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
+<p>At this point, you should <a href="/">return to your advent calendar</a> and try another puzzle.</p>
+<p>If you still want to see it, you can <a href="/day/23/input" target="_blank">get your puzzle input</a>.</p>
+<p>You can also <span class="share">[Share<span class="share-content">on
+  <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Opening+the+Turing+Lock%22+%2D+Day+23+%2D+Advent+of+Code&amp;url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F23&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
+  <a href="https://plus.google.com/share?url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F23" target="_blank">Google+</a>
+  <a href="http://www.reddit.com/submit?url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F23&amp;title=I%27ve+completed+%22Opening+the+Turing+Lock%22+%2D+Day+23+%2D+Advent+of+Code" target="_blank">Reddit</a
+></span>]</span>
+ this puzzle.</p>
+</main>
+
+<!-- ga -->
+<script>
+(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+ga('create', 'UA-69522494-1', 'auto');
+ga('send', 'pageview');
+</script>
+<!-- /ga -->
+</body>
+</html>
index f08ab26105f2b12fac8caf66463dc4fe71c35dfc..874ce4d9e457fb5bed4943867288e895ca5935e7 100644 (file)
@@ -2,7 +2,7 @@
  "cells": [
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": 1,
    "metadata": {
     "collapsed": true
    },
@@ -13,7 +13,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
+   "execution_count": 2,
    "metadata": {
     "collapsed": false
    },
@@ -48,7 +48,7 @@
        "   {'mana': 101}]}]"
       ]
      },
-     "execution_count": 7,
+     "execution_count": 2,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -65,7 +65,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 153,
+   "execution_count": 3,
    "metadata": {
     "collapsed": false
    },
@@ -77,7 +77,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 154,
+   "execution_count": 4,
    "metadata": {
     "collapsed": true
    },
@@ -99,7 +99,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 155,
+   "execution_count": 5,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 53}"
       ]
      },
-     "execution_count": 155,
+     "execution_count": 5,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 156,
+   "execution_count": 6,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 0}"
       ]
      },
-     "execution_count": 156,
+     "execution_count": 6,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 157,
+   "execution_count": 7,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 286}"
       ]
      },
-     "execution_count": 157,
+     "execution_count": 7,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 97,
+   "execution_count": 37,
    "metadata": {
-    "collapsed": true
+    "collapsed": false
    },
    "outputs": [],
    "source": [
     "                for status in state['ongoing']:\n",
     "                    if s in status[0]:\n",
     "                        add_this_spell = False\n",
+    "        if spell['name'] == 'Magic missile' and state['cast'] and state['cast'][-1] == 'Drain':\n",
+    "            add_this_spell = False\n",
     "        if add_this_spell:\n",
     "            valid_spells += [spell]\n",
     "    return valid_spells"
   },
   {
    "cell_type": "code",
-   "execution_count": 98,
+   "execution_count": 57,
    "metadata": {
     "collapsed": false
    },
        "   {'mana': 101}]}]"
       ]
      },
-     "execution_count": 98,
+     "execution_count": 57,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 158,
+   "execution_count": 58,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 286}"
       ]
      },
-     "execution_count": 158,
+     "execution_count": 58,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 159,
+   "execution_count": 59,
    "metadata": {
     "collapsed": false
    },
        " {'boss_hp': -2, 'cost': 73, 'name': 'Drain', 'pc_hp': 2}]"
       ]
      },
-     "execution_count": 159,
+     "execution_count": 59,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 48,
+   "execution_count": 60,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[{'boss_hp': -2, 'cost': 73, 'name': 'Drain', 'pc_hp': 2}]"
+      ]
+     },
+     "execution_count": 60,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "s4 = cast_spell(spells[1], s3)\n",
+    "valid_spells(spells, s4)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 61,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[{'boss_hp': -4, 'cost': 53, 'name': 'Magic missile'},\n",
+       " {'boss_hp': -2, 'cost': 73, 'name': 'Drain', 'pc_hp': 2}]"
+      ]
+     },
+     "execution_count": 61,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "s4 = cast_spell(spells[0], s3)\n",
+    "valid_spells(spells, s4)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 12,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 67,
+   "execution_count": 13,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 160,
+   "execution_count": 44,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 286}"
       ]
      },
-     "execution_count": 160,
+     "execution_count": 44,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 161,
+   "execution_count": 45,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 173}"
       ]
      },
-     "execution_count": 161,
+     "execution_count": 45,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 162,
+   "execution_count": 46,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 226}"
       ]
      },
-     "execution_count": 162,
+     "execution_count": 46,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 163,
+   "execution_count": 47,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 229}"
       ]
      },
-     "execution_count": 163,
+     "execution_count": 47,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 164,
+   "execution_count": 48,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 342}"
       ]
      },
-     "execution_count": 164,
+     "execution_count": 48,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 165,
+   "execution_count": 49,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 415}"
       ]
      },
-     "execution_count": 165,
+     "execution_count": 49,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 166,
+   "execution_count": 50,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 588}"
       ]
      },
-     "execution_count": 166,
+     "execution_count": 50,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 167,
+   "execution_count": 51,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 641}"
       ]
      },
-     "execution_count": 167,
+     "execution_count": 51,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 90,
+   "execution_count": 22,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 125,
+   "execution_count": 23,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 186,
+   "execution_count": 30,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 1269}"
       ]
      },
-     "execution_count": 186,
+     "execution_count": 30,
      "metadata": {},
      "output_type": "execute_result"
     }
     "current_state"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": 31,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "(1587, 2908)"
+      ]
+     },
+     "execution_count": 31,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "len(agenda), len(closed)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 65,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "{'armour': 0,\n",
+       " 'boss_damage': 9,\n",
+       " 'boss_hp': 0,\n",
+       " 'cast': ['Poison',\n",
+       "  'Recharge',\n",
+       "  'Shield',\n",
+       "  'Poison',\n",
+       "  'Drain',\n",
+       "  'Recharge',\n",
+       "  'Poison',\n",
+       "  'Magic missile',\n",
+       "  'Magic missile'],\n",
+       " 'mana': 241,\n",
+       " 'ongoing': [[{'boss_hp': -3}, {'boss_hp': -3}]],\n",
+       " 'pc_hp': 1,\n",
+       " 'spent': 1269}"
+      ]
+     },
+     "execution_count": 65,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "agenda = [initial_state]\n",
+    "closed = []\n",
+    "while agenda:\n",
+    "        current_state = agenda[0]\n",
+    "        new_states = []\n",
+    "            # print(current_state)\n",
+    "        if victory(current_state):\n",
+    "                # return current_state\n",
+    "                break\n",
+    "        for spell in valid_spells(spells, current_state):\n",
+    "                s2 = cast_spell(spell, current_state)\n",
+    "                if victory(s2):\n",
+    "                    new_states += [s2]\n",
+    "                else:\n",
+    "                    s3 = boss_turn(s2)\n",
+    "                    if victory(s3):\n",
+    "                        new_states += [s3]\n",
+    "                    if not finished(s3):\n",
+    "                        new_states += [apply_ongoing(s3)]\n",
+    "        # print(new_states)\n",
+    "        states_to_add = [s for s in new_states \n",
+    "                         if len(s['cast']) <= 50]\n",
+    "        agenda = sorted(states_to_add + agenda[1:], key=lambda s: s['spent'])\n",
+    "        # agenda = new_states + agenda[1:]\n",
+    "current_state"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 66,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "(165616, 0)"
+      ]
+     },
+     "execution_count": 66,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "len(agenda), len(closed)"
+   ]
+  },
   {
    "cell_type": "markdown",
    "metadata": {},
   },
   {
    "cell_type": "code",
-   "execution_count": 182,
+   "execution_count": 25,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 185,
+   "execution_count": 32,
    "metadata": {
     "collapsed": false
    },
        " 'spent': 1309}"
       ]
      },
-     "execution_count": 185,
+     "execution_count": 32,
      "metadata": {},
      "output_type": "execute_result"
     }
     "current_state"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": 33,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "(872, 2354)"
+      ]
+     },
+     "execution_count": 33,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "len(agenda), len(closed)"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
diff --git a/advent23.ipynb b/advent23.ipynb
new file mode 100644 (file)
index 0000000..371f02e
--- /dev/null
@@ -0,0 +1,230 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 13,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[['jio', 'a, +18'],\n",
+       " ['inc', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['jmp', '+22'],\n",
+       " ['tpl', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['tpl', 'a'],\n",
+       " ['jio', 'a, +8'],\n",
+       " ['inc', 'b'],\n",
+       " ['jie', 'a, +4'],\n",
+       " ['tpl', 'a'],\n",
+       " ['inc', 'a'],\n",
+       " ['jmp', '+2'],\n",
+       " ['hlf', 'a'],\n",
+       " ['jmp', '-7']]"
+      ]
+     },
+     "execution_count": 13,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "program = [i.strip().split(' ', 1) for i in open('advent23.txt').readlines()]\n",
+    "program"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "registers = {'a': 0, 'b': 0, 'pc': 0}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 24,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def hlf(args):\n",
+    "    registers[args] >>= 1\n",
+    "    registers['pc'] += 1\n",
+    "    \n",
+    "def tpl(args):\n",
+    "    registers[args] *= 3\n",
+    "    registers['pc'] += 1\n",
+    "\n",
+    "def inc(args):\n",
+    "    registers[args] += 1\n",
+    "    registers['pc'] += 1\n",
+    "\n",
+    "def jmp(args):\n",
+    "    registers['pc'] += int(args)\n",
+    "\n",
+    "def jie(args):\n",
+    "    r, o = args.split(', ')\n",
+    "    if registers[r] % 2 == 0:\n",
+    "        registers['pc'] += int(o)\n",
+    "    else:\n",
+    "        registers['pc'] += 1\n",
+    "\n",
+    "def jio(args):\n",
+    "    r, o = args.split(', ')\n",
+    "    if registers[r] == 1:\n",
+    "        registers['pc'] += int(o)\n",
+    "    else:\n",
+    "        registers['pc'] += 1\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 25,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [],
+   "source": [
+    "instructions = {'hlf': hlf, 'tpl': tpl, 'inc': inc, 'jmp': jmp, 'jie': jie, 'jio': jio}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 26,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [],
+   "source": [
+    "while registers['pc'] < len(program):\n",
+    "    instructions[program[registers['pc']][0]](program[registers['pc']][1])\n",
+    "registers"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 27,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "{'a': 1, 'b': 307, 'pc': 47}"
+      ]
+     },
+     "execution_count": 27,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "registers"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Part 2"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 28,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "{'a': 1, 'b': 160, 'pc': 47}"
+      ]
+     },
+     "execution_count": 28,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "registers = {'a': 1, 'b': 0, 'pc': 0}\n",
+    "while registers['pc'] < len(program):\n",
+    "    instructions[program[registers['pc']][0]](program[registers['pc']][1])\n",
+    "registers"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.4.3"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/advent23.txt b/advent23.txt
new file mode 100644 (file)
index 0000000..cc94207
--- /dev/null
@@ -0,0 +1,47 @@
+jio a, +18
+inc a
+tpl a
+inc a
+tpl a
+tpl a
+tpl a
+inc a
+tpl a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+tpl a
+tpl a
+inc a
+jmp +22
+tpl a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+tpl a
+inc a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+jio a, +8
+inc b
+jie a, +4
+tpl a
+inc a
+jmp +2
+hlf a
+jmp -7
\ No newline at end of file