{ "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": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'cats': 3, 'goldfish': 6, 'pomeranians': 8}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aunts = {}\n", "for l in pi16:\n", " ll = l.split(': ', 1)\n", " n = int(ll[0].split(' ')[1])\n", " items = ll[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": 4, "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": 5, "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": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[40]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(aunt for aunt in aunts if possible(aunt))" ] }, { "cell_type": "code", "execution_count": 7, "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": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "possible2(40)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[241]" ] }, "execution_count": 9, "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 }