Day 21
authorNeil Smith <neil.git@njae.me.uk>
Mon, 21 Dec 2015 16:13:04 +0000 (16:13 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Mon, 21 Dec 2015 16:13:04 +0000 (16:13 +0000)
SIGNED.md
advent-of-code-21.html [new file with mode: 0644]
advent21.ipynb [new file with mode: 0644]
advent21.txt [new file with mode: 0644]

index 7c34f1956dcda9288b846e5313f544cd50166a57..851166dfa45e76e8728f59c317b76203f0839ae4 100644 (file)
--- a/SIGNED.md
+++ b/SIGNED.md
@@ -3,19 +3,19 @@
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v2
 
-iQIcBAABCAAGBQJWduQGAAoJEJPB2e07Pgbqz2IQAJ9DTWMKZeYcafZgy0q36wUR
-X0hB4ocr0fqOYu0DSnMiw/dTO8iHhqavngTDrO+wcIXsm2zd3ImY6pABY6r111iA
-ZCSBMZV5XJ5LtpDQRaTSbHqpNzj3DKxYAhxUH5Hc29oOt3tn900oCupdY6g+BUb9
-xHZQoGTJEhSVB8UuljCtTOBMOufusjjpuTa7d/bEHOkSPDryHysZJheWs3DKMU5W
-yxQvVckyVV2Ke5qkpkGysQguyAbZxkyVey+gjEdtZVqnU+A8PNb099eOVMDzm5n8
-R8wc8wT+NuSwOmARrEcZjOYWM9CXDa7H3vYRXQpiiScGYoidB+clwpTq7SjmF5ik
-krfGsmEFT95X5t9X3tVwFVuMdhDUGzNzuC4fgq79NJxqzXYQ5uv++I8EWPeTP5gd
-r2YLMohJzHBT2odh5Xp6Vefcmrer0qwRz2GTj/mcrz4G9IqmOkzpK6tu0nXMMenM
-M0oWAESeAYDJ/5PRJcEOQcQxhhCCfwYbEWklRiAhTZ0AMGoWklmz3Ru6B1bxUOIU
-gm8g1RahzlXFfWSOq/+1x8yO1xAiGf0A9XsibDTatKex7DHQAjLySXeVxIof5J1X
-9HEsd4dRtK6T4sRBYt90dFNb+HwBL0c3KmE+rmay+l+glVYYvaGOrXdRrDmV7J0o
-LzHCVehOsol3p4Dyp3CO
-=zX10
+iQIcBAABCAAGBQJWeCUWAAoJEJPB2e07PgbqroEP/jpfpm2kXw0ZV1GzzKJK/c7P
+og/ruaCLWUn08iPv325GfjA8KCycxsFL6slGN4c66+zqveTIa+dHpabgepQHSdlJ
+FfRDR227eJMLHYF5LrsRQBXj0nD5HMEHXCcdkQPBzUpVTEO6YSsh9TRR1xS/FGf4
+zsbmd7FEGxW9A2VfX9CQtCLLrkIgJrvmceYxSXFRj5XTi3PbTuoba5zq6YP/CJh+
+XtKQirOUnsXqQ5oXJy/P6bGBsmeIZqkuVo4GRVQwo1G+MnX+j6sp/tY/Ft4I5vHd
+6K5G7lPTHYKYyUKNSelJQWZf50PdylTHYKmnkgPxcMepbESPhoAjIBUJOgT+xXq+
+KIMIcPSapAq6fxUWqz8Hi/Q8v0AubvENSjMoI+jwtOAIrKjqHbT1MfWkkdw/dwCw
+7PQQoU92JWgzptVmdr2OebD1vtUJvoxewHTx4LJrbEcYnE8HrLGfsUxGb+3BdSnQ
+Wpsr2iQkbkTMoTrK4fL5ZxtpeV6WJbhVrLFW2EJBTURz0TxrLdFPEUhReXDMNZi0
+LMT+170uRfx4OYZJsvUX7MO2XVgA0+UcBG1MdSz6vGmU9aCIwIscMs8enpjcCSVT
+XMoYU9HEQ6gjEM93zpRefyFYE3z9FzEQJFHVG2gHtkwRYSYCqtHocv8/MZlItz7V
+NW0resJihpsHglTpz43r
+=wiY6
 -----END PGP SIGNATURE-----
 
 ```
@@ -54,6 +54,7 @@ size    exec  file                        contents
 7545            advent-of-code-18.html    d41f47bada37cd3fb1be795a00a5ef7b6d78a8246f436441e37f94d64d18f677
 7580            advent-of-code-19.html    9cd3b0a1a5a1dc658c09b46ba9624312622ac5cf46fffcaccbeea661b11c577e
 6387            advent-of-code-20.html    3ec26a7cfb58a168b62781f097ebdb71ae3224cadde49588463370faaa3d33f9
+7780            advent-of-code-21.html    877675e47121bd4812ab461fd7cd6124c397669a5e6f35c3f19b023728976970
 109395          advent-of-code.ipynb      7292eeb9a8019f6931037c70105ab96bf691074dbe963736b6429cb4d4373f51
 25607           advent01.ipynb            c33ad39a77803a6870dd74998da98e3bb9c2c2db37c34167b330a01d663717e7
 7001            advent01.txt              79312922877bdedd09ce0886a42b3d7f7ed092e2218579fb7d6ac1cb38cedebe
@@ -92,6 +93,8 @@ size    exec  file                        contents
 968             advent19.txt              eb333fc1d3ec41e3b9b22096e838fe860fbc6667068129996cae3cd3483dd9e7
 11722           advent20-slow.ipynb       780a5bb1e729e8fdf8b4c75f6fbad5aa53975dcc9a6d12ee699a2d488c7b7dc3
 3377            advent20.ipynb            be9db2beee6805ac9db9f2dc145abba3275964e36e21580122bacd8436777a30
+9373            advent21.ipynb            70f49b67c911f9316c6be1404b57c76bd0b8aca94e16dd6fc1e5b39d3442b2b4
+35              advent21.txt              6c4c3bcec0bf45ee485c7595a6a5eb2ad7722bdecf275780e1cdb9993eb931d8
 ```
 
 #### Ignore
diff --git a/advent-of-code-21.html b/advent-of-code-21.html
new file mode 100644 (file)
index 0000000..f1b599b
--- /dev/null
@@ -0,0 +1,172 @@
+<!DOCTYPE html>
+<html lang="en-us">
+<head>
+<meta charset="utf-8"/>
+<title>Day 21 - 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">42*</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 21: RPG Simulator 20XX ---</h2><p>Little <span title="The sky above the battle is the color of television, tuned to a dead channel.">Henry Case</span> got a new video game for Christmas.  It's an <a href="https://en.wikipedia.org/wiki/Role-playing_video_game">RPG</a>, and he's stuck on a boss.  He needs to know what equipment to buy at the shop.  He hands you the <a href="https://en.wikipedia.org/wiki/Game_controller">controller</a>.</p>
+<p>In this game, the player (you) and the enemy (the boss) take turns attacking.  The player always goes first.  Each attack reduces the opponent's hit points by at least <code>1</code>.  The first character at or below <code>0</code> hit points loses.</p>
+<p>Damage dealt by an attacker each turn is equal to the attacker's damage score minus the defender's armor score.  An attacker always does at least <code>1</code> damage.  So, if the attacker has a damage score of <code>8</code>, and the defender has an armor score of <code>3</code>, the defender loses <code>5</code> hit points.  If the defender had an armor score of <code>300</code>, the defender would still lose <code>1</code> hit point.</p>
+<p>Your damage score and armor score both start at zero.  They can be increased by buying items in exchange for gold.  You start with no items and have as much gold as you need.  Your total damage or armor is equal to the sum of those stats from all of your items.  You have <em>100 hit points</em>.</p>
+<p>Here is what the item shop is selling:</p>
+<pre><code>Weapons:    Cost  Damage  Armor
+Dagger        8     4       0
+Shortsword   10     5       0
+Warhammer    25     6       0
+Longsword    40     7       0
+Greataxe     74     8       0
+
+Armor:      Cost  Damage  Armor
+Leather      13     0       1
+Chainmail    31     0       2
+Splintmail   53     0       3
+Bandedmail   75     0       4
+Platemail   102     0       5
+
+Rings:      Cost  Damage  Armor
+Damage +1    25     1       0
+Damage +2    50     2       0
+Damage +3   100     3       0
+Defense +1   20     0       1
+Defense +2   40     0       2
+Defense +3   80     0       3
+</code></pre>
+<p>You must buy exactly one weapon; no dual-wielding.  Armor is optional, but you can't use more than one.  You can buy 0-2 rings (at most one for each hand).  You must use any items you buy.  The shop only has one of each item, so you can't buy, for example, two rings of Damage +3.</p>
+<p>For example, suppose you have <code>8</code> hit points, <code>5</code> damage, and <code>5</code> armor, and that the boss has <code>12</code> hit points, <code>7</code> damage, and <code>2</code> armor:</p>
+<ul>
+<li>The player deals <code>5-2 = 3</code> damage; the boss goes down to 9 hit points.</li>
+<li>The boss deals <code>7-5 = 2</code> damage; the player goes down to 6 hit points.</li>
+<li>The player deals <code>5-2 = 3</code> damage; the boss goes down to 6 hit points.</li>
+<li>The boss deals <code>7-5 = 2</code> damage; the player goes down to 4 hit points.</li>
+<li>The player deals <code>5-2 = 3</code> damage; the boss goes down to 3 hit points.</li>
+<li>The boss deals <code>7-5 = 2</code> damage; the player goes down to 2 hit points.</li>
+<li>The player deals <code>5-2 = 3</code> damage; the boss goes down to 0 hit points.</li>
+</ul>
+<p>In this scenario, the player wins!  (Barely.)</p>
+<p>You have <em>100 hit points</em>.  The boss's actual stats are in your puzzle input.  What is <em>the least amount of gold you can spend</em> and still win the fight?</p>
+</article>
+<p>Your puzzle answer was <code>91</code>.</p><article class="day-desc"><h2>--- Part Two ---</h2><p>Turns out the shopkeeper is working with the boss, and can persuade you to buy whatever items he wants. The other rules still apply, and he still only has one of each item.</p>
+<p>What is the <em>most</em> amount of gold you can spend and still <em>lose</em> the fight?</p>
+</article>
+<p>Your puzzle answer was <code>158</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/21/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+%22RPG+Simulator+20XX%22+%2D+Day+21+%2D+Advent+of+Code&amp;url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F21&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
+  <a href="https://plus.google.com/share?url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F21" target="_blank">Google+</a>
+  <a href="http://www.reddit.com/submit?url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F21&amp;title=I%27ve+completed+%22RPG+Simulator+20XX%22+%2D+Day+21+%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>
diff --git a/advent21.ipynb b/advent21.ipynb
new file mode 100644 (file)
index 0000000..6fbd087
--- /dev/null
@@ -0,0 +1,389 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 133,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "import collections\n",
+    "import itertools"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 134,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def player_wins_fight(player, boss, verbose=False):\n",
+    "    boss_hp = boss['hp']\n",
+    "    pc_hp = player['hp']\n",
+    "    while pc_hp > 0 and boss_hp > 0:\n",
+    "        boss_hp -= max(player['damage'] - boss['armour'], 1)\n",
+    "        pc_hp -= max(boss['damage'] - player['armour'], 1)\n",
+    "        if verbose:\n",
+    "            print('PC:', player['hp'], ' . Boss:', boss_hp)\n",
+    "    if boss_hp <= 0:\n",
+    "        return True\n",
+    "    else:\n",
+    "        return False"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 135,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "True"
+      ]
+     },
+     "execution_count": 135,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "pc = {'hp': 8, 'damage': 5, 'armour': 5}\n",
+    "boss =  {'hp': 12, 'damage': 7, 'armour': 2}\n",
+    "player_wins_fight(pc, boss)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 136,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "({'armour': 5, 'damage': 5, 'hp': 8}, {'armour': 2, 'damage': 7, 'hp': 12})"
+      ]
+     },
+     "execution_count": 136,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "pc, boss"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 137,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "{'armour': 2, 'damage': 8, 'hp': 100}"
+      ]
+     },
+     "execution_count": 137,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "boss = {}\n",
+    "for l in open('advent21.txt').readlines():\n",
+    "    t, v = l.strip().split(': ')\n",
+    "    if t == 'Hit Points': \n",
+    "        boss['hp'] = int(v)\n",
+    "    elif t == 'Armor': \n",
+    "        boss['armour'] = int(v)\n",
+    "    else:\n",
+    "        boss[t.lower()] = int(v)\n",
+    "boss"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 138,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "weapons_s = \"\"\"Dagger        8     4       0\n",
+    "Shortsword   10     5       0\n",
+    "Warhammer    25     6       0\n",
+    "Longsword    40     7       0\n",
+    "Greataxe     74     8       0\"\"\"\n",
+    "\n",
+    "armour_s = \"\"\"Leather      13     0       1\n",
+    "Chainmail    31     0       2\n",
+    "Splintmail   53     0       3\n",
+    "Bandedmail   75     0       4\n",
+    "Platemail   102     0       5\"\"\"\n",
+    "\n",
+    "rings_s = \"\"\"Damage+1    25     1       0\n",
+    "Damage+2    50     2       0\n",
+    "Damage+3   100     3       0\n",
+    "Defense+1   20     0       1\n",
+    "Defense+2   40     0       2\n",
+    "Defense+3   80     0       3\"\"\""
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 140,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [],
+   "source": [
+    "def parse_table(table):\n",
+    "    d = []\n",
+    "    for l in table.split('\\n'):\n",
+    "        ls = l.split()\n",
+    "        d += [{'name': ls[0].strip(),\n",
+    "               'cost': int(ls[1].strip()), \n",
+    "               'damage': int(ls[2].strip()), \n",
+    "               'armour': int(ls[3].strip()) }]\n",
+    "    return d"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 141,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "([{'armour': 0, 'cost': 8, 'damage': 4, 'name': 'Dagger'},\n",
+       "  {'armour': 0, 'cost': 10, 'damage': 5, 'name': 'Shortsword'},\n",
+       "  {'armour': 0, 'cost': 25, 'damage': 6, 'name': 'Warhammer'},\n",
+       "  {'armour': 0, 'cost': 40, 'damage': 7, 'name': 'Longsword'},\n",
+       "  {'armour': 0, 'cost': 74, 'damage': 8, 'name': 'Greataxe'}],\n",
+       " [{'armour': 1, 'cost': 13, 'damage': 0, 'name': 'Leather'},\n",
+       "  {'armour': 2, 'cost': 31, 'damage': 0, 'name': 'Chainmail'},\n",
+       "  {'armour': 3, 'cost': 53, 'damage': 0, 'name': 'Splintmail'},\n",
+       "  {'armour': 4, 'cost': 75, 'damage': 0, 'name': 'Bandedmail'},\n",
+       "  {'armour': 5, 'cost': 102, 'damage': 0, 'name': 'Platemail'},\n",
+       "  {}],\n",
+       " [{'armour': 0, 'cost': 25, 'damage': 1, 'name': 'Damage+1'},\n",
+       "  {'armour': 0, 'cost': 50, 'damage': 2, 'name': 'Damage+2'},\n",
+       "  {'armour': 0, 'cost': 100, 'damage': 3, 'name': 'Damage+3'},\n",
+       "  {'armour': 1, 'cost': 20, 'damage': 0, 'name': 'Defense+1'},\n",
+       "  {'armour': 2, 'cost': 40, 'damage': 0, 'name': 'Defense+2'},\n",
+       "  {'armour': 3, 'cost': 80, 'damage': 0, 'name': 'Defense+3'},\n",
+       "  {},\n",
+       "  {}])"
+      ]
+     },
+     "execution_count": 141,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "weapons = parse_table(weapons_s)\n",
+    "armour = parse_table(armour_s)\n",
+    "armour += [{}]\n",
+    "rings = parse_table(rings_s)\n",
+    "rings += [{}, {}]\n",
+    "weapons, armour, rings"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 142,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def player(equipment):\n",
+    "    player = {'hp': 100, 'damage': 0, 'armour': 0, 'cost': 0, 'name': ''}\n",
+    "    for e in equipment:\n",
+    "        for s in e:\n",
+    "            player[s] += e[s]\n",
+    "    return player"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 143,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "{'armour': 4, 'cost': 217, 'damage': 10, 'hp': 100, 'name': ''}"
+      ]
+     },
+     "execution_count": 143,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "player([{'armour': 0, 'cost': 74, 'damage': 8}, {'armour': 1, 'cost': 13, 'damage': 0},\n",
+    "        {'armour': 0, 'cost': 50, 'damage': 2}, {'armour': 3, 'cost': 80, 'damage': 0}])"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 144,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "840"
+      ]
+     },
+     "execution_count": 144,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "equipment = itertools.chain(\n",
+    "    itertools.product(\n",
+    "        itertools.combinations(weapons, 1),\n",
+    "        itertools.combinations(armour, 1),\n",
+    "        itertools.combinations(rings, 2)))\n",
+    "\n",
+    "players = [player(j for i in e for j in i) for e in equipment]\n",
+    "len(players)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 146,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "91"
+      ]
+     },
+     "execution_count": 146,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "winner = [p for p in players if not player_wins_fight(p, boss)]\n",
+    "# sorted(winners, key=lambda w: w['cost'])\n",
+    "min(winners, key=lambda w: w['cost'])['cost']"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "#Part 2"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 149,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "840"
+      ]
+     },
+     "execution_count": 149,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "equipment = itertools.chain(\n",
+    "    itertools.product(\n",
+    "        itertools.combinations(weapons, 1),\n",
+    "        itertools.combinations(armour, 1),\n",
+    "        itertools.combinations(rings, 2)))\n",
+    "\n",
+    "players = [player(j for i in e for j in i) for e in equipment]\n",
+    "len(players)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 151,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "158"
+      ]
+     },
+     "execution_count": 151,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "losers = [p for p in players if not player_wins_fight(p, boss)]\n",
+    "# sorted(losers, key=lambda w: w['cost'], reverse=True)\n",
+    "max(losers, key=lambda w: w['cost'])['cost']"
+   ]
+  },
+  {
+   "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/advent21.txt b/advent21.txt
new file mode 100644 (file)
index 0000000..db43742
--- /dev/null
@@ -0,0 +1,3 @@
+Hit Points: 100
+Damage: 8
+Armor: 2