Done puzzle 64
[project-euler.git] / euler59.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 33,
6 "metadata": {
7 "scrolled": true
8 },
9 "outputs": [
10 {
11 "data": {
12 "text/plain": [
13 "[79, 59, 12, 2, 79, 35, 8, 28, 20, 2, 3, 68, 8, 9, 68, 45, 0, 12, 9, 67, 68, 4, 7, 5, 23, 27, 1, 21, 79, 85, 78, 79, 85, 71, 38, 10, 71, 27, 12, 2, 79, 6, 2, 8, 13, 9, 1, 13, 9, 8, 68, 19, 7, 1, 71, 56, 11, 21, 11, 68, 6, 3, 22, 2, 14, 0, 30, 79, 1, 31, 6, 23, 19, 10, 0, 73, 79, 44, 2, 79, 19, 6, 28, 68, 16, 6, 16, 15, 79, 35, 8, 11, 72, 71, 14, 10, 3, 79, 12, 2, 79, 19, 6, 28, 68, 32, 0, 0, 73, 79, 86, 71, 39, 1, 71, 24, 5, 20, 79, 13, 9, 79, 16, 15, 10, 68, 5, 10, 3, 14, 1, 10, 14, 1, 3, 71, 24, 13, 19, 7, 68, 32, 0, 0, 73, 79, 87, 71, 39, 1, 71, 12, 22, 2, 14, 16, 2, 11, 68, 2, 25, 1, 21, 22, 16, 15, 6, 10, 0, 79, 16, 15, 10, 22, 2, 79, 13, 20, 65, 68, 41, 0, 16, 15, 6, 10, 0, 79, 1, 31, 6, 23, 19, 28, 68, 19, 7, 5, 19, 79, 12, 2, 79, 0, 14, 11, 10, 64, 27, 68, 10, 14, 15, 2, 65, 68, 83, 79, 40, 14, 9, 1, 71, 6, 16, 20, 10, 8, 1, 79, 19, 6, 28, 68, 14, 1, 68, 15, 6, 9, 75, 79, 5, 9, 11, 68, 19, 7, 13, 20, 79, 8, 14, 9, 1, 71, 8, 13, 17, 10, 23, 71, 3, 13, 0, 7, 16, 71, 27, 11, 71, 10, 18, 2, 29, 29, 8, 1, 1, 73, 79, 81, 71, 59, 12, 2, 79, 8, 14, 8, 12, 19, 79, 23, 15, 6, 10, 2, 28, 68, 19, 7, 22, 8, 26, 3, 15, 79, 16, 15, 10, 68, 3, 14, 22, 12, 1, 1, 20, 28, 72, 71, 14, 10, 3, 79, 16, 15, 10, 68, 3, 14, 22, 12, 1, 1, 20, 28, 68, 4, 14, 10, 71, 1, 1, 17, 10, 22, 71, 10, 28, 19, 6, 10, 0, 26, 13, 20, 7, 68, 14, 27, 74, 71, 89, 68, 32, 0, 0, 71, 28, 1, 9, 27, 68, 45, 0, 12, 9, 79, 16, 15, 10, 68, 37, 14, 20, 19, 6, 23, 19, 79, 83, 71, 27, 11, 71, 27, 1, 11, 3, 68, 2, 25, 1, 21, 22, 11, 9, 10, 68, 6, 13, 11, 18, 27, 68, 19, 7, 1, 71, 3, 13, 0, 7, 16, 71, 28, 11, 71, 27, 12, 6, 27, 68, 2, 25, 1, 21, 22, 11, 9, 10, 68, 10, 6, 3, 15, 27, 68, 5, 10, 8, 14, 10, 18, 2, 79, 6, 2, 12, 5, 18, 28, 1, 71, 0, 2, 71, 7, 13, 20, 79, 16, 2, 28, 16, 14, 2, 11, 9, 22, 74, 71, 87, 68, 45, 0, 12, 9, 79, 12, 14, 2, 23, 2, 3, 2, 71, 24, 5, 20, 79, 10, 8, 27, 68, 19, 7, 1, 71, 3, 13, 0, 7, 16, 92, 79, 12, 2, 79, 19, 6, 28, 68, 8, 1, 8, 30, 79, 5, 71, 24, 13, 19, 1, 1, 20, 28, 68, 19, 0, 68, 19, 7, 1, 71, 3, 13, 0, 7, 16, 73, 79, 93, 71, 59, 12, 2, 79, 11, 9, 10, 68, 16, 7, 11, 71, 6, 23, 71, 27, 12, 2, 79, 16, 21, 26, 1, 71, 3, 13, 0, 7, 16, 75, 79, 19, 15, 0, 68, 0, 6, 18, 2, 28, 68, 11, 6, 3, 15, 27, 68, 19, 0, 68, 2, 25, 1, 21, 22, 11, 9, 10, 72, 71, 24, 5, 20, 79, 3, 8, 6, 10, 0, 79, 16, 8, 79, 7, 8, 2, 1, 71, 6, 10, 19, 0, 68, 19, 7, 1, 71, 24, 11, 21, 3, 0, 73, 79, 85, 87, 79, 38, 18, 27, 68, 6, 3, 16, 15, 0, 17, 0, 7, 68, 19, 7, 1, 71, 24, 11, 21, 3, 0, 71, 24, 5, 20, 79, 9, 6, 11, 1, 71, 27, 12, 21, 0, 17, 0, 7, 68, 15, 6, 9, 75, 79, 16, 15, 10, 68, 16, 0, 22, 11, 11, 68, 3, 6, 0, 9, 72, 16, 71, 29, 1, 4, 0, 3, 9, 6, 30, 2, 79, 12, 14, 2, 68, 16, 7, 1, 9, 79, 12, 2, 79, 7, 6, 2, 1, 73, 79, 85, 86, 79, 33, 17, 10, 10, 71, 6, 10, 71, 7, 13, 20, 79, 11, 16, 1, 68, 11, 14, 10, 3, 79, 5, 9, 11, 68, 6, 2, 11, 9, 8, 68, 15, 6, 23, 71, 0, 19, 9, 79, 20, 2, 0, 20, 11, 10, 72, 71, 7, 1, 71, 24, 5, 20, 79, 10, 8, 27, 68, 6, 12, 7, 2, 31, 16, 2, 11, 74, 71, 94, 86, 71, 45, 17, 19, 79, 16, 8, 79, 5, 11, 3, 68, 16, 7, 11, 71, 13, 1, 11, 6, 1, 17, 10, 0, 71, 7, 13, 10, 79, 5, 9, 11, 68, 6, 12, 7, 2, 31, 16, 2, 11, 68, 15, 6, 9, 75, 79, 12, 2, 79, 3, 6, 25, 1, 71, 27, 12, 2, 79, 22, 14, 8, 12, 19, 79, 16, 8, 79, 6, 2, 12, 11, 10, 10, 68, 4, 7, 13, 11, 11, 22, 2, 1, 68, 8, 9, 68, 32, 0, 0, 73, 79, 85, 84, 79, 48, 15, 10, 29, 71, 14, 22, 2, 79, 22, 2, 13, 11, 21, 1, 69, 71, 59, 12, 14, 28, 68, 14, 28, 68, 9, 0, 16, 71, 14, 68, 23, 7, 29, 20, 6, 7, 6, 3, 68, 5, 6, 22, 19, 7, 68, 21, 10, 23, 18, 3, 16, 14, 1, 3, 71, 9, 22, 8, 2, 68, 15, 26, 9, 6, 1, 68, 23, 14, 23, 20, 6, 11, 9, 79, 11, 21, 79, 20, 11, 14, 10, 75, 79, 16, 15, 6, 23, 71, 29, 1, 5, 6, 22, 19, 7, 68, 4, 0, 9, 2, 28, 68, 1, 29, 11, 10, 79, 35, 8, 11, 74, 86, 91, 68, 52, 0, 68, 19, 7, 1, 71, 56, 11, 21, 11, 68, 5, 10, 7, 6, 2, 1, 71, 7, 17, 10, 14, 10, 71, 14, 10, 3, 79, 8, 14, 25, 1, 3, 79, 12, 2, 29, 1, 71, 0, 10, 71, 10, 5, 21, 27, 12, 71, 14, 9, 8, 1, 3, 71, 26, 23, 73, 79, 44, 2, 79, 19, 6, 28, 68, 1, 26, 8, 11, 79, 11, 1, 79, 17, 9, 9, 5, 14, 3, 13, 9, 8, 68, 11, 0, 18, 2, 79, 5, 9, 11, 68, 1, 14, 13, 19, 7, 2, 18, 3, 10, 2, 28, 23, 73, 79, 37, 9, 11, 68, 16, 10, 68, 15, 14, 18, 2, 79, 23, 2, 10, 10, 71, 7, 13, 20, 79, 3, 11, 0, 22, 30, 67, 68, 19, 7, 1, 71, 8, 8, 8, 29, 29, 71, 0, 2, 71, 27, 12, 2, 79, 11, 9, 3, 29, 71, 60, 11, 9, 79, 11, 1, 79, 16, 15, 10, 68, 33, 14, 16, 15, 10, 22, 73]"
14 ]
15 },
16 "execution_count": 33,
17 "metadata": {},
18 "output_type": "execute_result"
19 }
20 ],
21 "source": [
22 "ciphertext = File.read('p059_cipher.txt').chomp.split(',').map {|c| c.to_i}"
23 ]
24 },
25 {
26 "cell_type": "code",
27 "execution_count": 30,
28 "metadata": {},
29 "outputs": [
30 {
31 "data": {
32 "text/plain": [
33 "2"
34 ]
35 },
36 "execution_count": 30,
37 "metadata": {},
38 "output_type": "execute_result"
39 }
40 ],
41 "source": [
42 "3 ^ 1"
43 ]
44 },
45 {
46 "cell_type": "code",
47 "execution_count": 35,
48 "metadata": {},
49 "outputs": [
50 {
51 "data": {
52 "text/plain": [
53 "17576"
54 ]
55 },
56 "execution_count": 35,
57 "metadata": {},
58 "output_type": "execute_result"
59 }
60 ],
61 "source": [
62 "letters = ('a'..'z').to_a\n",
63 "keys = letters.product letters, letters \n",
64 "keys.length"
65 ]
66 },
67 {
68 "cell_type": "code",
69 "execution_count": 37,
70 "metadata": {},
71 "outputs": [
72 {
73 "data": {
74 "text/plain": [
75 "[[\"a\", \"a\", \"a\"], [\"a\", \"a\", \"b\"], [\"a\", \"a\", \"c\"], [\"a\", \"a\", \"d\"], [\"a\", \"a\", \"e\"], [\"a\", \"a\", \"f\"], [\"a\", \"a\", \"g\"], [\"a\", \"a\", \"h\"], [\"a\", \"a\", \"i\"], [\"a\", \"a\", \"j\"], [\"a\", \"a\", \"k\"]]"
76 ]
77 },
78 "execution_count": 37,
79 "metadata": {},
80 "output_type": "execute_result"
81 }
82 ],
83 "source": [
84 "keys[0..10]"
85 ]
86 },
87 {
88 "cell_type": "code",
89 "execution_count": 38,
90 "metadata": {},
91 "outputs": [
92 {
93 "data": {
94 "text/plain": [
95 ":decipher_char"
96 ]
97 },
98 "execution_count": 38,
99 "metadata": {},
100 "output_type": "execute_result"
101 }
102 ],
103 "source": [
104 "def decipher_char(char, key)\n",
105 " (char ^ key.ord).chr\n",
106 "end"
107 ]
108 },
109 {
110 "cell_type": "code",
111 "execution_count": 44,
112 "metadata": {},
113 "outputs": [
114 {
115 "data": {
116 "text/plain": [
117 "1"
118 ]
119 },
120 "execution_count": 44,
121 "metadata": {},
122 "output_type": "execute_result"
123 }
124 ],
125 "source": [
126 "decipher_char(79, 78.chr).ord"
127 ]
128 },
129 {
130 "cell_type": "code",
131 "execution_count": 52,
132 "metadata": {},
133 "outputs": [
134 {
135 "data": {
136 "text/plain": [
137 ":pad_key"
138 ]
139 },
140 "execution_count": 52,
141 "metadata": {},
142 "output_type": "execute_result"
143 }
144 ],
145 "source": [
146 "def pad_key(key, target_length)\n",
147 " repeats = (target_length.to_f / key.length).ceil\n",
148 " (key * repeats)[0...target_length]\n",
149 "end"
150 ]
151 },
152 {
153 "cell_type": "code",
154 "execution_count": 53,
155 "metadata": {},
156 "outputs": [
157 {
158 "data": {
159 "text/plain": [
160 "100"
161 ]
162 },
163 "execution_count": 53,
164 "metadata": {},
165 "output_type": "execute_result"
166 }
167 ],
168 "source": [
169 "pad_key([1,2,3], 100).length"
170 ]
171 },
172 {
173 "cell_type": "code",
174 "execution_count": 78,
175 "metadata": {},
176 "outputs": [
177 {
178 "data": {
179 "text/plain": [
180 "-1200.7343581516182"
181 ]
182 },
183 "execution_count": 78,
184 "metadata": {},
185 "output_type": "execute_result"
186 }
187 ],
188 "source": [
189 "k = [\"a\", \"b\", \"c\"]\n",
190 "\n",
191 "pletters(ciphertext.zip(pad_key(k, ciphertext.length)).map {|c, k| decipher_char c, k})"
192 ]
193 },
194 {
195 "cell_type": "code",
196 "execution_count": 89,
197 "metadata": {},
198 "outputs": [
199 {
200 "data": {
201 "text/plain": [
202 ":decipher"
203 ]
204 },
205 "execution_count": 89,
206 "metadata": {},
207 "output_type": "execute_result"
208 }
209 ],
210 "source": [
211 "def decipher(ciphertext, key)\n",
212 " ciphertext.zip(pad_key(key, ciphertext.length)).map {|c, k| decipher_char c, k}\n",
213 "end"
214 ]
215 },
216 {
217 "cell_type": "markdown",
218 "metadata": {},
219 "source": [
220 " Score a potential plaintext by the number of letters in it."
221 ]
222 },
223 {
224 "cell_type": "code",
225 "execution_count": 105,
226 "metadata": {},
227 "outputs": [
228 {
229 "data": {
230 "text/plain": [
231 ":score"
232 ]
233 },
234 "execution_count": 105,
235 "metadata": {},
236 "output_type": "execute_result"
237 }
238 ],
239 "source": [
240 "def score(ciphertext, key)\n",
241 " decipher(ciphertext, key).select {|c| /[[:alpha:]]/ =~ c}.length\n",
242 "end"
243 ]
244 },
245 {
246 "cell_type": "code",
247 "execution_count": 108,
248 "metadata": {},
249 "outputs": [
250 {
251 "data": {
252 "text/plain": [
253 "[\"a\", \"a\", \"d\"]"
254 ]
255 },
256 "execution_count": 108,
257 "metadata": {},
258 "output_type": "execute_result"
259 }
260 ],
261 "source": [
262 "keys[0..10].map {|k| [score(ciphertext, k), k]}.sort.last.last"
263 ]
264 },
265 {
266 "cell_type": "code",
267 "execution_count": 109,
268 "metadata": {},
269 "outputs": [
270 {
271 "data": {
272 "text/plain": [
273 "[\"g\", \"o\", \"d\"]"
274 ]
275 },
276 "execution_count": 109,
277 "metadata": {},
278 "output_type": "execute_result"
279 }
280 ],
281 "source": [
282 "keys.map {|k| [score(ciphertext, k), k]}.sort.last.last"
283 ]
284 },
285 {
286 "cell_type": "code",
287 "execution_count": 100,
288 "metadata": {},
289 "outputs": [
290 {
291 "data": {
292 "text/plain": [
293 "\"(The Gospel of John, chapter 1) 1 In the beginning the Word already existed. He was with God, and he was God. 2 He was in the beginning with God. 3 He created everything there is. Nothing exists that he didn't make. 4 Life itself was in him, and this life gives light to everyone. 5 The light shines through the darkness, and the darkness can never extinguish it. 6 God sent John the Baptist 7 to tell everyone about the light so that everyone might believe because of his testimony. 8 John himself was not the light; he was only a witness to the light. 9 The one who is the true light, who gives light to everyone, was going to come into the world. 10 But although the world was made through him, the world didn't recognize him when he came. 11 Even in his own land and among his own people, he was not accepted. 12 But to all who believed him and accepted him, he gave the right to become children of God. 13 They are reborn! This is not a physical birth resulting from human passion or plan, this rebirth comes from God.14 So the Word became human and lived here on earth among us. He was full of unfailing love and faithfulness. And we have seen his glory, the glory of the only Son of the Father.\""
294 ]
295 },
296 "execution_count": 100,
297 "metadata": {},
298 "output_type": "execute_result"
299 }
300 ],
301 "source": [
302 "decipher(ciphertext, [\"g\", \"o\", \"d\"]).join"
303 ]
304 },
305 {
306 "cell_type": "code",
307 "execution_count": 101,
308 "metadata": {},
309 "outputs": [
310 {
311 "data": {
312 "text/plain": [
313 "107359"
314 ]
315 },
316 "execution_count": 101,
317 "metadata": {},
318 "output_type": "execute_result"
319 }
320 ],
321 "source": [
322 "decipher(ciphertext, [\"g\", \"o\", \"d\"]).map {|c| c.ord}.sum"
323 ]
324 },
325 {
326 "cell_type": "code",
327 "execution_count": null,
328 "metadata": {
329 "collapsed": true
330 },
331 "outputs": [],
332 "source": []
333 }
334 ],
335 "metadata": {
336 "kernelspec": {
337 "display_name": "Ruby 2.4.0",
338 "language": "ruby",
339 "name": "ruby"
340 },
341 "language_info": {
342 "file_extension": ".rb",
343 "mimetype": "application/x-ruby",
344 "name": "ruby",
345 "version": "2.4.0"
346 }
347 },
348 "nbformat": 4,
349 "nbformat_minor": 2
350 }