Fixed typo
[advent-of-code-15.git] / advent11.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 3,
6 "metadata": {
7 "collapsed": false
8 },
9 "outputs": [],
10 "source": [
11 "import itertools\n",
12 "import string"
13 ]
14 },
15 {
16 "cell_type": "code",
17 "execution_count": 4,
18 "metadata": {
19 "collapsed": true
20 },
21 "outputs": [],
22 "source": [
23 "def increment(password):\n",
24 " if password:\n",
25 " if password[-1] < 'z':\n",
26 " return password[:-1] + chr(ord(password[-1]) + 1)\n",
27 " else:\n",
28 " return increment(password[:-1]) + 'a'\n",
29 " else:\n",
30 " raise StopIterations\n"
31 ]
32 },
33 {
34 "cell_type": "code",
35 "execution_count": 7,
36 "metadata": {
37 "collapsed": false
38 },
39 "outputs": [
40 {
41 "data": {
42 "text/plain": [
43 "'aca'"
44 ]
45 },
46 "execution_count": 7,
47 "metadata": {},
48 "output_type": "execute_result"
49 }
50 ],
51 "source": [
52 "increment('abz')"
53 ]
54 },
55 {
56 "cell_type": "code",
57 "execution_count": 8,
58 "metadata": {
59 "collapsed": false
60 },
61 "outputs": [
62 {
63 "data": {
64 "text/plain": [
65 "'aabaaaaa'"
66 ]
67 },
68 "execution_count": 8,
69 "metadata": {},
70 "output_type": "execute_result"
71 }
72 ],
73 "source": [
74 "increment('aaazzzzz')"
75 ]
76 },
77 {
78 "cell_type": "code",
79 "execution_count": 9,
80 "metadata": {
81 "collapsed": true
82 },
83 "outputs": [],
84 "source": [
85 "def valid(password):\n",
86 " if 'i' in password:\n",
87 " return False\n",
88 " if 'o' in password:\n",
89 " return False\n",
90 " if 'l' in password:\n",
91 " return False\n",
92 " if not has_run(password):\n",
93 " return False\n",
94 " if len(doubles(password)) < 2:\n",
95 " return False\n",
96 " return True"
97 ]
98 },
99 {
100 "cell_type": "code",
101 "execution_count": 21,
102 "metadata": {
103 "collapsed": false
104 },
105 "outputs": [],
106 "source": [
107 "def has_run(password):\n",
108 " for i in range(len(password) - 3):\n",
109 " if ord(password[i]) + 1 == ord(password[i+1]) and ord(password[i+1]) + 1 == ord(password[i+2]):\n",
110 " return True\n",
111 " return False"
112 ]
113 },
114 {
115 "cell_type": "code",
116 "execution_count": 20,
117 "metadata": {
118 "collapsed": false
119 },
120 "outputs": [
121 {
122 "data": {
123 "text/plain": [
124 "False"
125 ]
126 },
127 "execution_count": 20,
128 "metadata": {},
129 "output_type": "execute_result"
130 }
131 ],
132 "source": [
133 "has_run('hiajkblammnpo')"
134 ]
135 },
136 {
137 "cell_type": "code",
138 "execution_count": 24,
139 "metadata": {
140 "collapsed": false
141 },
142 "outputs": [],
143 "source": [
144 "def doubles(password):\n",
145 " ds = []\n",
146 " for i in range(len(password)-1):\n",
147 " if password[i] == password[i+1]:\n",
148 " ds += [password[i]]\n",
149 " return set(ds)"
150 ]
151 },
152 {
153 "cell_type": "code",
154 "execution_count": 27,
155 "metadata": {
156 "collapsed": false
157 },
158 "outputs": [
159 {
160 "data": {
161 "text/plain": [
162 "{'k', 'm'}"
163 ]
164 },
165 "execution_count": 27,
166 "metadata": {},
167 "output_type": "execute_result"
168 }
169 ],
170 "source": [
171 "doubles('hiajkkblammmnpo')"
172 ]
173 },
174 {
175 "cell_type": "code",
176 "execution_count": 30,
177 "metadata": {
178 "collapsed": false
179 },
180 "outputs": [
181 {
182 "data": {
183 "text/plain": [
184 "False"
185 ]
186 },
187 "execution_count": 30,
188 "metadata": {},
189 "output_type": "execute_result"
190 }
191 ],
192 "source": [
193 "valid('abbcegjk')"
194 ]
195 },
196 {
197 "cell_type": "code",
198 "execution_count": 31,
199 "metadata": {
200 "collapsed": true
201 },
202 "outputs": [],
203 "source": [
204 "def next_valid(password):\n",
205 " p = increment(password)\n",
206 " while not valid(p):\n",
207 " p = increment(p)\n",
208 " return p"
209 ]
210 },
211 {
212 "cell_type": "code",
213 "execution_count": 32,
214 "metadata": {
215 "collapsed": false
216 },
217 "outputs": [
218 {
219 "data": {
220 "text/plain": [
221 "'abcdffaa'"
222 ]
223 },
224 "execution_count": 32,
225 "metadata": {},
226 "output_type": "execute_result"
227 }
228 ],
229 "source": [
230 "next_valid('abcdefgh')"
231 ]
232 },
233 {
234 "cell_type": "code",
235 "execution_count": 33,
236 "metadata": {
237 "collapsed": false
238 },
239 "outputs": [
240 {
241 "data": {
242 "text/plain": [
243 "'ghjaabcc'"
244 ]
245 },
246 "execution_count": 33,
247 "metadata": {},
248 "output_type": "execute_result"
249 }
250 ],
251 "source": [
252 "next_valid('ghijklmn')"
253 ]
254 },
255 {
256 "cell_type": "code",
257 "execution_count": 34,
258 "metadata": {
259 "collapsed": false
260 },
261 "outputs": [
262 {
263 "data": {
264 "text/plain": [
265 "'hxbxxyzz'"
266 ]
267 },
268 "execution_count": 34,
269 "metadata": {},
270 "output_type": "execute_result"
271 }
272 ],
273 "source": [
274 "next_valid('hxbxwxba')"
275 ]
276 },
277 {
278 "cell_type": "code",
279 "execution_count": 35,
280 "metadata": {
281 "collapsed": false
282 },
283 "outputs": [
284 {
285 "data": {
286 "text/plain": [
287 "'hxcaabcc'"
288 ]
289 },
290 "execution_count": 35,
291 "metadata": {},
292 "output_type": "execute_result"
293 }
294 ],
295 "source": [
296 "next_valid('hxbxxyzz')"
297 ]
298 },
299 {
300 "cell_type": "code",
301 "execution_count": null,
302 "metadata": {
303 "collapsed": true
304 },
305 "outputs": [],
306 "source": []
307 }
308 ],
309 "metadata": {
310 "kernelspec": {
311 "display_name": "Python 3",
312 "language": "python",
313 "name": "python3"
314 },
315 "language_info": {
316 "codemirror_mode": {
317 "name": "ipython",
318 "version": 3
319 },
320 "file_extension": ".py",
321 "mimetype": "text/x-python",
322 "name": "python",
323 "nbconvert_exporter": "python",
324 "pygments_lexer": "ipython3",
325 "version": "3.4.3"
326 }
327 },
328 "nbformat": 4,
329 "nbformat_minor": 0
330 }