From: Neil Smith Date: Mon, 21 Dec 2015 16:13:04 +0000 (+0000) Subject: Day 21 X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-15.git;a=commitdiff_plain;h=1dbbda35a8b4d52d61bb12cafd5cbc1eb360206f Day 21 --- diff --git a/SIGNED.md b/SIGNED.md index 7c34f19..851166d 100644 --- 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 index 0000000..f1b599b --- /dev/null +++ b/advent-of-code-21.html @@ -0,0 +1,172 @@ + + + + +Day 21 - Advent of Code + + + + + + +

Advent of Code

Neil Smith 42*
+ + + +
+

--- Day 21: RPG Simulator 20XX ---

Little Henry Case got a new video game for Christmas. It's an RPG, and he's stuck on a boss. He needs to know what equipment to buy at the shop. He hands you the controller.

+

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 1. The first character at or below 0 hit points loses.

+

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 1 damage. So, if the attacker has a damage score of 8, and the defender has an armor score of 3, the defender loses 5 hit points. If the defender had an armor score of 300, the defender would still lose 1 hit point.

+

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 100 hit points.

+

Here is what the item shop is selling:

+
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
+
+

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.

+

For example, suppose you have 8 hit points, 5 damage, and 5 armor, and that the boss has 12 hit points, 7 damage, and 2 armor:

+
    +
  • The player deals 5-2 = 3 damage; the boss goes down to 9 hit points.
  • +
  • The boss deals 7-5 = 2 damage; the player goes down to 6 hit points.
  • +
  • The player deals 5-2 = 3 damage; the boss goes down to 6 hit points.
  • +
  • The boss deals 7-5 = 2 damage; the player goes down to 4 hit points.
  • +
  • The player deals 5-2 = 3 damage; the boss goes down to 3 hit points.
  • +
  • The boss deals 7-5 = 2 damage; the player goes down to 2 hit points.
  • +
  • The player deals 5-2 = 3 damage; the boss goes down to 0 hit points.
  • +
+

In this scenario, the player wins! (Barely.)

+

You have 100 hit points. The boss's actual stats are in your puzzle input. What is the least amount of gold you can spend and still win the fight?

+
+

Your puzzle answer was 91.

--- Part Two ---

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.

+

What is the most amount of gold you can spend and still lose the fight?

+
+

Your puzzle answer was 158.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also + this puzzle.

+
+ + + + + + diff --git a/advent21.ipynb b/advent21.ipynb new file mode 100644 index 0000000..6fbd087 --- /dev/null +++ b/advent21.ipynb @@ -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 index 0000000..db43742 --- /dev/null +++ b/advent21.txt @@ -0,0 +1,3 @@ +Hit Points: 100 +Damage: 8 +Armor: 2