Fixed typo
[advent-of-code-15.git] / advent16.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {
7 "collapsed": false
8 },
9 "outputs": [
10 {
11 "data": {
12 "text/plain": [
13 "['Sue 1: goldfish: 9, cars: 0, samoyeds: 9',\n",
14 " 'Sue 2: perfumes: 5, trees: 8, goldfish: 8',\n",
15 " 'Sue 3: pomeranians: 2, akitas: 1, trees: 5',\n",
16 " 'Sue 4: goldfish: 10, akitas: 2, perfumes: 9',\n",
17 " 'Sue 5: cars: 5, perfumes: 6, akitas: 9']"
18 ]
19 },
20 "execution_count": 1,
21 "metadata": {},
22 "output_type": "execute_result"
23 }
24 ],
25 "source": [
26 "pi16 = [l.strip() for l in open('advent16.txt').readlines()]\n",
27 "pi16[:5]"
28 ]
29 },
30 {
31 "cell_type": "code",
32 "execution_count": 3,
33 "metadata": {
34 "collapsed": false
35 },
36 "outputs": [
37 {
38 "data": {
39 "text/plain": [
40 "{'cats': 3, 'goldfish': 6, 'pomeranians': 8}"
41 ]
42 },
43 "execution_count": 3,
44 "metadata": {},
45 "output_type": "execute_result"
46 }
47 ],
48 "source": [
49 "aunts = {}\n",
50 "for l in pi16:\n",
51 " ll = l.split(': ', 1)\n",
52 " n = int(ll[0].split(' ')[1])\n",
53 " items = ll[1].split(', ')\n",
54 " aunts[n] = {i.split(': ')[0].strip(): int(i.split(': ')[1]) for i in items}\n",
55 "aunts[406]"
56 ]
57 },
58 {
59 "cell_type": "code",
60 "execution_count": 4,
61 "metadata": {
62 "collapsed": true
63 },
64 "outputs": [],
65 "source": [
66 "target = {'children': 3,\n",
67 "'cats': 7,\n",
68 "'samoyeds': 2,\n",
69 "'pomeranians': 3,\n",
70 "'akitas': 0,\n",
71 "'vizslas': 0,\n",
72 "'goldfish': 5,\n",
73 "'trees': 3,\n",
74 "'cars': 2,\n",
75 "'perfumes': 1}"
76 ]
77 },
78 {
79 "cell_type": "code",
80 "execution_count": 5,
81 "metadata": {
82 "collapsed": true
83 },
84 "outputs": [],
85 "source": [
86 "def possible(aunt):\n",
87 " return all(aunts[aunt][item] == target[item] for item in aunts[aunt])"
88 ]
89 },
90 {
91 "cell_type": "code",
92 "execution_count": 6,
93 "metadata": {
94 "collapsed": false
95 },
96 "outputs": [
97 {
98 "data": {
99 "text/plain": [
100 "[40]"
101 ]
102 },
103 "execution_count": 6,
104 "metadata": {},
105 "output_type": "execute_result"
106 }
107 ],
108 "source": [
109 "list(aunt for aunt in aunts if possible(aunt))"
110 ]
111 },
112 {
113 "cell_type": "code",
114 "execution_count": 7,
115 "metadata": {
116 "collapsed": true
117 },
118 "outputs": [],
119 "source": [
120 "def possible2(aunt):\n",
121 " possible = True\n",
122 " for item in aunts[aunt]:\n",
123 " if item == 'cats' or item == 'trees':\n",
124 " if aunts[aunt][item] <= target[item]:\n",
125 " possible = False\n",
126 " elif item == 'pomeranians' or item == 'goldfish':\n",
127 " if aunts[aunt][item] >= target[item]:\n",
128 " possible = False\n",
129 " else:\n",
130 " if aunts[aunt][item] != target[item]:\n",
131 " possible = False\n",
132 " return possible"
133 ]
134 },
135 {
136 "cell_type": "code",
137 "execution_count": 8,
138 "metadata": {
139 "collapsed": false
140 },
141 "outputs": [
142 {
143 "data": {
144 "text/plain": [
145 "False"
146 ]
147 },
148 "execution_count": 8,
149 "metadata": {},
150 "output_type": "execute_result"
151 }
152 ],
153 "source": [
154 "possible2(40)"
155 ]
156 },
157 {
158 "cell_type": "code",
159 "execution_count": 9,
160 "metadata": {
161 "collapsed": false
162 },
163 "outputs": [
164 {
165 "data": {
166 "text/plain": [
167 "[241]"
168 ]
169 },
170 "execution_count": 9,
171 "metadata": {},
172 "output_type": "execute_result"
173 }
174 ],
175 "source": [
176 "list(aunt for aunt in aunts if possible2(aunt))"
177 ]
178 },
179 {
180 "cell_type": "code",
181 "execution_count": null,
182 "metadata": {
183 "collapsed": true
184 },
185 "outputs": [],
186 "source": []
187 }
188 ],
189 "metadata": {
190 "kernelspec": {
191 "display_name": "Python 3",
192 "language": "python",
193 "name": "python3"
194 },
195 "language_info": {
196 "codemirror_mode": {
197 "name": "ipython",
198 "version": 3
199 },
200 "file_extension": ".py",
201 "mimetype": "text/x-python",
202 "name": "python",
203 "nbconvert_exporter": "python",
204 "pygments_lexer": "ipython3",
205 "version": "3.4.3"
206 }
207 },
208 "nbformat": 4,
209 "nbformat_minor": 0
210 }