Add files via upload

This commit is contained in:
Peter Norvig 2017-12-02 09:14:12 -08:00 committed by GitHub
parent 6b9063551c
commit c8c4dee589
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,18 +1,30 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# December 2017: Advent of Code Solutions\n",
"\n",
"<div align=right>Peter Norvig, December 2017</div>\n",
"\n",
"I'm doing the [Advent of Code](https://adventofcode.com) puzzles, just like [last year](https://github.com/norvig/pytudes/blob/master/ipynb/Advent%20of%20Code.ipynb). But this time, I won't repeat the write up my version of the puzzle description each time; you'll have to follow the links in the section headers (e.g. **[Day 1](https://adventofcode.com/2017/day/1)**) to read those. I just show my solutions.\n",
"\n",
"First, a set of imports and utility functions that might prove useful:"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {
"collapsed": false
},
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Python 3.x\n",
"# Python 3.x Utility Functions\n",
"\n",
"import re\n",
"import numpy as np\n",
"import math\n",
"import random\n",
"import urllib.request\n",
"\n",
"from collections import Counter, defaultdict, namedtuple, deque\n",
@ -36,6 +48,20 @@
" return open(filename)\n",
" except FileNotFoundError:\n",
" return urllib.request.urlopen(\"http://norvig.com/ipython/\" + filename)\n",
" \n",
"def array(lines):\n",
" \"Convert an iterable of lines into a 2-D array. If lines is a str, do splitlines.\"\n",
" if isinstance(lines, str): lines = lines.splitlines()\n",
" return [mapt(atom, line.split()) for line in lines]\n",
"\n",
"def atom(token):\n",
" try:\n",
" return int(token)\n",
" except ValueError:\n",
" try:\n",
" return float(token)\n",
" except ValueError:\n",
" return token\n",
"\n",
"################ Functions on Iterables\n",
"\n",
@ -123,6 +149,10 @@
" \"Canonicalize these order-independent items into a hashable canonical form.\"\n",
" typ = (typ or (cat if isinstance(items, str) else tuple))\n",
" return typ(sorted(items))\n",
"\n",
"def mapt(fn, *args): \n",
" \"Do a map, and make the results into a tuple.\"\n",
" return tuple(map(fn, *args))\n",
" \n",
"################ Math\n",
" \n",
@ -218,14 +248,158 @@
" return Astar(start, moves_func, lambda s: (0 if goal_func(s) else 1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# [Day 1](https://adventofcode.com/2017/day/1): Inverse Captcha\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": []
"source": [
"raw = '3294199471327195994824832197564859876682638188889768298894243832665654681412886862234525991553276578641265589959178414218389329361496673991614673626344552179413995562266818138372393213966143124914469397692587251112663217862879233226763533911128893354536353213847122251463857894159819828724827969576432191847787772732881266875469721189331882228146576832921314638221317393256471998598117289632684663355273845983933845721713497811766995367795857965222183668765517454263354111134841334631345111596131682726196574763165187889337599583345634413436165539744188866156771585647718555182529936669683581662398618765391487164715724849894563314426959348119286955144439452731762666568741612153254469131724137699832984728937865956711925592628456617133695259554548719328229938621332325125972547181236812263887375866231118312954369432937359357266467383318326239572877314765121844831126178173988799765218913178825966268816476559792947359956859989228917136267178571776316345292573489873792149646548747995389669692188457724414468727192819919448275922166321158141365237545222633688372891451842434458527698774342111482498999383831492577615154591278719656798277377363284379468757998373193231795767644654155432692988651312845433511879457921638934877557575241394363721667237778962455961493559848522582413748218971212486373232795878362964873855994697149692824917183375545192119453587398199912564474614219929345185468661129966379693813498542474732198176496694746111576925715493967296487258237854152382365579876894391815759815373319159213475555251488754279888245492373595471189191353244684697662848376529881512529221627313527441221459672786923145165989611223372241149929436247374818467481641931872972582295425936998535194423916544367799522276914445231582272368388831834437562752119325286474352863554693373718848649568451797751926315617575295381964426843625282819524747119726872193569785611959896776143539915299968276374712996485367853494734376257511273443736433464496287219615697341973131715166768916149828396454638596713572963686159214116763'\n",
"\n",
"digits = mapt(int, raw)\n",
"N = len(digits)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1158"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(digits[i] \n",
" for i in range(N) \n",
" if digits[i] == digits[i - 1])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1132"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(digits[i] \n",
" for i in range(N) \n",
" if digits[i] == digits[i - N//2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I was able to do this warmup puzzle very quickly; too bad I started a few hours too late to make in on the leader board. I created a recurring calendar reminder so I'll be less likely to be late in the future."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# [Day 2](https://adventofcode.com/2017/day/2): Corruption Checksum\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"rows = array('''790\t99\t345\t1080\t32\t143\t1085\t984\t553\t98\t123\t97\t197\t886\t125\t947\n",
"302\t463\t59\t58\t55\t87\t508\t54\t472\t63\t469\t419\t424\t331\t337\t72\n",
"899\t962\t77\t1127\t62\t530\t78\t880\t129\t1014\t93\t148\t239\t288\t357\t424\n",
"2417\t2755\t254\t3886\t5336\t3655\t5798\t3273\t5016\t178\t270\t6511\t223\t5391\t1342\t2377\n",
"68\t3002\t3307\t166\t275\t1989\t1611\t364\t157\t144\t3771\t1267\t3188\t3149\t156\t3454\n",
"1088\t1261\t21\t1063\t1173\t278\t1164\t207\t237\t1230\t1185\t431\t232\t660\t195\t1246\n",
"49\t1100\t136\t1491\t647\t1486\t112\t1278\t53\t1564\t1147\t1068\t809\t1638\t138\t117\n",
"158\t3216\t1972\t2646\t3181\t785\t2937\t365\t611\t1977\t1199\t2972\t201\t2432\t186\t160\n",
"244\t86\t61\t38\t58\t71\t243\t52\t245\t264\t209\t265\t308\t80\t126\t129\n",
"1317\t792\t74\t111\t1721\t252\t1082\t1881\t1349\t94\t891\t1458\t331\t1691\t89\t1724\n",
"3798\t202\t3140\t3468\t1486\t2073\t3872\t3190\t3481\t3760\t2876\t182\t2772\t226\t3753\t188\n",
"2272\t6876\t6759\t218\t272\t4095\t4712\t6244\t4889\t2037\t234\t223\t6858\t3499\t2358\t439\n",
"792\t230\t886\t824\t762\t895\t99\t799\t94\t110\t747\t635\t91\t406\t89\t157\n",
"2074\t237\t1668\t1961\t170\t2292\t2079\t1371\t1909\t221\t2039\t1022\t193\t2195\t1395\t2123\n",
"8447\t203\t1806\t6777\t278\t2850\t1232\t6369\t398\t235\t212\t992\t7520\t7304\t7852\t520\n",
"3928\t107\t3406\t123\t2111\t2749\t223\t125\t134\t146\t3875\t1357\t508\t1534\t4002\t4417''')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"46402"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(abs(max(row) - min(row)) for row in rows)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"265"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def evendiv(row): \n",
" return first(a // b for a in row for b in row if a > b and a // b == a / b)\n",
"\n",
"sum(map(evendiv, rows))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Today I remembered to start on time, but I was too slow to score any points; the best I could do was 112th place on Part Two. \n",
"\n",
"In Part One, my big mistake was typing `\"=\"` instead of `\"-\"` in `\"max(row) - min(row)\"`. I was confused by Python's misleading error message, which said `\"SyntaxError: keyword can't be an expression\"` and pointed at `\"max\"`."
]
}
],
"metadata": {
@ -244,7 +418,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
"version": "3.5.3"
}
},
"nbformat": 4,