X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-15.git;a=blobdiff_plain;f=advent16.ipynb;fp=advent16.ipynb;h=7e183c65f109610afc9f3da6d60d69750d2d750c;hp=0000000000000000000000000000000000000000;hb=b9628120d445b262f151412426637ed6029d0a5d;hpb=56e1be9ad16bcdf72f063083e7955f0145df4c2e diff --git a/advent16.ipynb b/advent16.ipynb new file mode 100644 index 0000000..7e183c6 --- /dev/null +++ b/advent16.ipynb @@ -0,0 +1,210 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['Sue 1: goldfish: 9, cars: 0, samoyeds: 9',\n", + " 'Sue 2: perfumes: 5, trees: 8, goldfish: 8',\n", + " 'Sue 3: pomeranians: 2, akitas: 1, trees: 5',\n", + " 'Sue 4: goldfish: 10, akitas: 2, perfumes: 9',\n", + " 'Sue 5: cars: 5, perfumes: 6, akitas: 9']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pi16 = [l.strip() for l in open('advent16.txt').readlines()]\n", + "pi16[:5]" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'cats': 3, 'goldfish': 6, 'pomeranians': 8}" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aunts = {}\n", + "for l in pi16:\n", + " s = l.find(':')\n", + " n = int(l[:s].split(' ')[1])\n", + " items = l[s+1:].split(', ')\n", + " aunts[n] = {i.split(': ')[0].strip(): int(i.split(': ')[1]) for i in items}\n", + "aunts[406]" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "target = {'children': 3,\n", + "'cats': 7,\n", + "'samoyeds': 2,\n", + "'pomeranians': 3,\n", + "'akitas': 0,\n", + "'vizslas': 0,\n", + "'goldfish': 5,\n", + "'trees': 3,\n", + "'cars': 2,\n", + "'perfumes': 1}" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def possible(aunt):\n", + " return all(aunts[aunt][item] == target[item] for item in aunts[aunt])" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[40]" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(aunt for aunt in aunts if possible(aunt))" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def possible2(aunt):\n", + " possible = True\n", + " for item in aunts[aunt]:\n", + " if item == 'cats' or item == 'trees':\n", + " if aunts[aunt][item] <= target[item]:\n", + " possible = False\n", + " elif item == 'pomeranians' or item == 'goldfish':\n", + " if aunts[aunt][item] >= target[item]:\n", + " possible = False\n", + " else:\n", + " if aunts[aunt][item] != target[item]:\n", + " possible = False\n", + " return possible" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "possible2(40)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[241]" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(aunt for aunt in aunts if possible2(aunt))" + ] + }, + { + "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 +}