Done puzzle 47
[project-euler.git] / euler42.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 "[\"A\", \"ABILITY\", \"ABLE\", \"ABOUT\", \"ABOVE\", \"ABSENCE\", \"ABSOLUTELY\", \"ACADEMIC\", \"ACCEPT\", \"ACCESS\", \"ACCIDENT\"]"
14 ]
15 },
16 "execution_count": 1,
17 "metadata": {},
18 "output_type": "execute_result"
19 }
20 ],
21 "source": [
22 "words = File.read(\"p042_words.txt\").split(\",\").map {|w| w[1..-2]}\n",
23 "words[0..10]"
24 ]
25 },
26 {
27 "cell_type": "code",
28 "execution_count": 2,
29 "metadata": {
30 "collapsed": false
31 },
32 "outputs": [
33 {
34 "data": {
35 "text/plain": [
36 ":grow"
37 ]
38 },
39 "execution_count": 2,
40 "metadata": {},
41 "output_type": "execute_result"
42 }
43 ],
44 "source": [
45 "require 'singleton'\n",
46 "\n",
47 "class TriangleN\n",
48 " include Enumerable\n",
49 " include Singleton\n",
50 "\n",
51 " def initialize\n",
52 " @tns = [1]\n",
53 " end\n",
54 " \n",
55 " def is_triangle?(n)\n",
56 " self.grow(n)\n",
57 " @tns.include?(n)\n",
58 " end\n",
59 " \n",
60 " def grow(n)\n",
61 " while n > @tns[-1]\n",
62 " @tns += [(@tns.length+1) * (@tns.length+2) / 2]\n",
63 " end\n",
64 " end\n",
65 "end"
66 ]
67 },
68 {
69 "cell_type": "code",
70 "execution_count": 3,
71 "metadata": {
72 "collapsed": false
73 },
74 "outputs": [
75 {
76 "data": {
77 "text/plain": [
78 "#<TriangleN:0x00563e39321180 @tns=[1]>"
79 ]
80 },
81 "execution_count": 3,
82 "metadata": {},
83 "output_type": "execute_result"
84 }
85 ],
86 "source": [
87 "trins = TriangleN.instance"
88 ]
89 },
90 {
91 "cell_type": "code",
92 "execution_count": 4,
93 "metadata": {
94 "collapsed": false
95 },
96 "outputs": [
97 {
98 "data": {
99 "text/plain": [
100 "true"
101 ]
102 },
103 "execution_count": 4,
104 "metadata": {},
105 "output_type": "execute_result"
106 }
107 ],
108 "source": [
109 "trins.is_triangle? 10"
110 ]
111 },
112 {
113 "cell_type": "code",
114 "execution_count": 5,
115 "metadata": {
116 "collapsed": false
117 },
118 "outputs": [
119 {
120 "data": {
121 "text/plain": [
122 "false"
123 ]
124 },
125 "execution_count": 5,
126 "metadata": {},
127 "output_type": "execute_result"
128 }
129 ],
130 "source": [
131 "trins.is_triangle? 11"
132 ]
133 },
134 {
135 "cell_type": "code",
136 "execution_count": 6,
137 "metadata": {
138 "collapsed": false
139 },
140 "outputs": [
141 {
142 "data": {
143 "text/plain": [
144 ":triangle_sum?"
145 ]
146 },
147 "execution_count": 6,
148 "metadata": {},
149 "output_type": "execute_result"
150 }
151 ],
152 "source": [
153 "class String\n",
154 " \n",
155 " @@triangles = TriangleN.instance\n",
156 " \n",
157 " def to_nums\n",
158 " self.chars.map {|c| c.ord - 'A'.ord + 1}\n",
159 " end\n",
160 " \n",
161 " def num_sum\n",
162 " self.to_nums.sum\n",
163 " end\n",
164 " \n",
165 " def triangle_sum?\n",
166 " @@triangles.is_triangle?(self.num_sum)\n",
167 " end\n",
168 "end"
169 ]
170 },
171 {
172 "cell_type": "code",
173 "execution_count": 7,
174 "metadata": {
175 "collapsed": false
176 },
177 "outputs": [
178 {
179 "data": {
180 "text/plain": [
181 "[19, 11, 25]"
182 ]
183 },
184 "execution_count": 7,
185 "metadata": {},
186 "output_type": "execute_result"
187 }
188 ],
189 "source": [
190 "\"SKY\".to_nums"
191 ]
192 },
193 {
194 "cell_type": "code",
195 "execution_count": 8,
196 "metadata": {
197 "collapsed": false
198 },
199 "outputs": [
200 {
201 "data": {
202 "text/plain": [
203 "55"
204 ]
205 },
206 "execution_count": 8,
207 "metadata": {},
208 "output_type": "execute_result"
209 }
210 ],
211 "source": [
212 "\"SKY\".num_sum"
213 ]
214 },
215 {
216 "cell_type": "code",
217 "execution_count": 9,
218 "metadata": {
219 "collapsed": false
220 },
221 "outputs": [
222 {
223 "data": {
224 "text/plain": [
225 "true"
226 ]
227 },
228 "execution_count": 9,
229 "metadata": {},
230 "output_type": "execute_result"
231 }
232 ],
233 "source": [
234 "trins.is_triangle? \"SKY\".num_sum"
235 ]
236 },
237 {
238 "cell_type": "code",
239 "execution_count": 10,
240 "metadata": {
241 "collapsed": false
242 },
243 "outputs": [
244 {
245 "data": {
246 "text/plain": [
247 "true"
248 ]
249 },
250 "execution_count": 10,
251 "metadata": {},
252 "output_type": "execute_result"
253 }
254 ],
255 "source": [
256 "\"SKY\".triangle_sum?"
257 ]
258 },
259 {
260 "cell_type": "code",
261 "execution_count": 11,
262 "metadata": {
263 "collapsed": false
264 },
265 "outputs": [
266 {
267 "data": {
268 "text/plain": [
269 "162"
270 ]
271 },
272 "execution_count": 11,
273 "metadata": {},
274 "output_type": "execute_result"
275 }
276 ],
277 "source": [
278 "words.select {|w| w.triangle_sum?}.length"
279 ]
280 },
281 {
282 "cell_type": "code",
283 "execution_count": 12,
284 "metadata": {
285 "collapsed": false
286 },
287 "outputs": [
288 {
289 "data": {
290 "text/plain": [
291 "[\"A\", \"ABILITY\", \"ABOVE\", \"ACCOMPANY\", \"ACHIEVEMENT\", \"AGENCY\", \"AGREE\", \"AIR\", \"ALREADY\", \"AN\", \"ANCIENT\", \"APPARENT\", \"APPOINT\", \"APPROACH\", \"ASSUME\", \"AT\", \"ATMOSPHERE\", \"BAG\", \"BAND\", \"BANK\", \"BAR\", \"BEAT\", \"BELONG\", \"BENEATH\", \"BONE\", \"BOTH\", \"BRIDGE\", \"BUILDING\", \"BURN\", \"CALL\", \"CAPACITY\", \"CAREFUL\", \"CASE\", \"CHILD\", \"CIVIL\", \"CLOSELY\", \"COME\", \"CONFIDENCE\", \"CONFIRM\", \"CONSERVATIVE\", \"CONSTRUCTION\", \"CONTENT\", \"COULD\", \"CURRENTLY\", \"DECISION\", \"DEFINITION\", \"DEMOCRATIC\", \"DEPUTY\", \"DESPITE\", \"DISTINCTION\", \"EAST\", \"EDGE\", \"EDUCATIONAL\", \"EFFECT\", \"EQUIPMENT\", \"EVENT\", \"FACE\", \"FAIL\", \"FAMILY\", \"FEEL\", \"FIELD\", \"FIGURE\", \"FLOOR\", \"FREEDOM\", \"FUND\", \"FUTURE\", \"GENTLEMAN\", \"GREY\", \"GROWTH\", \"HAIR\", \"HAPPY\", \"HAVE\", \"HERE\", \"HIS\", \"IF\", \"INCIDENT\", \"INCREASED\", \"INCREASINGLY\", \"INDIVIDUAL\", \"INSTRUMENT\", \"INTEND\", \"INTENTION\", \"IS\", \"LAW\", \"LEADER\", \"LEAVE\", \"LENGTH\", \"LESS\", \"LITTLE\", \"LOVELY\", \"MAN\", \"MATCH\", \"MERELY\", \"MILK\", \"MISTAKE\", \"MOVE\", \"MUCH\", \"NEED\", \"NOTICE\", \"OBJECT\", \"OBJECTIVE\", \"OF\", \"OIL\", \"ONLY\", \"OTHER\", \"OURSELVES\", \"PART\", \"PASS\", \"PATH\", \"PERFORM\", \"PRISON\", \"PRIVATE\", \"PROBABLY\", \"PROCEDURE\", \"QUALITY\", \"QUESTION\", \"RANGE\", \"READ\", \"REAL\", \"RELIEF\", \"REMOVE\", \"REPRESENT\", \"REQUEST\", \"RESPOND\", \"RIDE\", \"SAMPLE\", \"SAY\", \"SEAT\", \"SECURITY\", \"SINGLE\", \"SKY\", \"SOIL\", \"SOLICITOR\", \"SONG\", \"SOUTHERN\", \"SPIRIT\", \"START\", \"SUGGESTION\", \"TALL\", \"TAX\", \"THEORY\", \"THREATEN\", \"THROUGHOUT\", \"TITLE\", \"TOOTH\", \"TOTALLY\", \"TRAVEL\", \"TYPE\", \"UNABLE\", \"UNDERSTAND\", \"UPON\", \"USE\", \"VARIOUS\", \"VARY\", \"VIDEO\", \"WAGE\", \"WARM\", \"WATCH\", \"WE\", \"WHILST\", \"WIDELY\", \"WOMAN\"]"
292 ]
293 },
294 "execution_count": 12,
295 "metadata": {},
296 "output_type": "execute_result"
297 }
298 ],
299 "source": [
300 "words.select {|w| w.triangle_sum?}"
301 ]
302 },
303 {
304 "cell_type": "code",
305 "execution_count": 13,
306 "metadata": {
307 "collapsed": false
308 },
309 "outputs": [
310 {
311 "data": {
312 "text/plain": [
313 "\"#<TriangleN:0x00563e39321180 @tns=[1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210]>\""
314 ]
315 },
316 "execution_count": 13,
317 "metadata": {},
318 "output_type": "execute_result"
319 }
320 ],
321 "source": [
322 "trins.inspect"
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": 0
350 }