Rearranged file locations
[advent-of-code-17.git] / src / advent02 / advent02.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}"
11 ]
12 },
13 {
14 "cell_type": "code",
15 "execution_count": 2,
16 "metadata": {},
17 "outputs": [],
18 "source": [
19 "import Text.Parsec \n",
20 "import Text.ParserCombinators.Parsec.Number"
21 ]
22 },
23 {
24 "cell_type": "code",
25 "execution_count": 3,
26 "metadata": {},
27 "outputs": [],
28 "source": [
29 "onlySpaces = many (oneOf \" \\t\")"
30 ]
31 },
32 {
33 "cell_type": "code",
34 "execution_count": 4,
35 "metadata": {},
36 "outputs": [],
37 "source": [
38 "sFile = sLine `sepEndBy` newline \n",
39 "sLine = int `sepBy` onlySpaces"
40 ]
41 },
42 {
43 "cell_type": "code",
44 "execution_count": 5,
45 "metadata": {},
46 "outputs": [],
47 "source": [
48 "parseFile :: String -> Either ParseError [[Int]]\n",
49 "parseFile input = parse sFile \"(unknown)\" input\n",
50 "\n",
51 "parseLine :: String -> Either ParseError [Int]\n",
52 "parseLine input = parse sLine \"(unknown)\" input\n",
53 "\n",
54 "successfulParse :: Either ParseError [a] -> [a]\n",
55 "successfulParse (Left _) = []\n",
56 "successfulParse (Right a) = a"
57 ]
58 },
59 {
60 "cell_type": "code",
61 "execution_count": 6,
62 "metadata": {},
63 "outputs": [],
64 "source": [
65 "main :: IO ()\n",
66 "main = do \n",
67 " text <- readFile \"../../data/advent02.txt\"\n",
68 " let sheet = successfulParse $ parseFile text\n",
69 " print sheet\n",
70 "-- print $ part1 sheet\n",
71 "-- print $ part2 sheet"
72 ]
73 },
74 {
75 "cell_type": "code",
76 "execution_count": 7,
77 "metadata": {},
78 "outputs": [
79 {
80 "data": {
81 "text/plain": [
82 "[[179,2358,5197,867,163,4418,3135,5049,187,166,4682,5080,5541,172,4294,1397],[2637,136,3222,591,2593,1982,4506,195,4396,3741,2373,157,4533,3864,4159,142],[1049,1163,1128,193,1008,142,169,168,165,310,1054,104,1100,761,406,173],[200,53,222,227,218,51,188,45,98,194,189,42,50,105,46,176],[299,2521,216,2080,2068,2681,2376,220,1339,244,605,1598,2161,822,387,268],[1043,1409,637,1560,970,69,832,87,78,1391,1558,75,1643,655,1398,1193],[90,649,858,2496,1555,2618,2302,119,2675,131,1816,2356,2480,603,65,128],[2461,5099,168,4468,5371,2076,223,1178,194,5639,890,5575,1258,5591,6125,226],[204,205,2797,2452,2568,2777,1542,1586,241,836,3202,2495,197,2960,240,2880],[560,96,336,627,546,241,191,94,368,528,298,78,76,123,240,563],[818,973,1422,244,1263,200,1220,208,1143,627,609,274,130,961,685,1318],[1680,1174,1803,169,450,134,3799,161,2101,3675,133,4117,3574,4328,3630,4186],[1870,3494,837,115,1864,3626,24,116,2548,1225,3545,676,128,1869,3161,109],[890,53,778,68,65,784,261,682,563,781,360,382,790,313,785,71],[125,454,110,103,615,141,562,199,340,80,500,473,221,573,108,536],[1311,64,77,1328,1344,1248,1522,51,978,1535,1142,390,81,409,68,352]]"
83 ]
84 },
85 "metadata": {},
86 "output_type": "display_data"
87 }
88 ],
89 "source": [
90 "main"
91 ]
92 },
93 {
94 "cell_type": "code",
95 "execution_count": 8,
96 "metadata": {},
97 "outputs": [
98 {
99 "data": {
100 "text/plain": [
101 "Right [179,2358,5197,867,163,4418,3135,5049,187,166,4682,5080,5541,172,4294,1397]"
102 ]
103 },
104 "metadata": {},
105 "output_type": "display_data"
106 }
107 ],
108 "source": [
109 "parseLine \"179 2358 5197 867 163 4418 3135 5049 187 166 4682 5080 5541 172 4294 1397\""
110 ]
111 },
112 {
113 "cell_type": "code",
114 "execution_count": 9,
115 "metadata": {},
116 "outputs": [
117 {
118 "data": {
119 "text/plain": [
120 "Right [[1,2],[8,9]]"
121 ]
122 },
123 "metadata": {},
124 "output_type": "display_data"
125 }
126 ],
127 "source": [
128 "parseFile \"1 2\\n8 9\""
129 ]
130 },
131 {
132 "cell_type": "code",
133 "execution_count": 10,
134 "metadata": {},
135 "outputs": [
136 {
137 "data": {
138 "text/plain": [
139 "[[1,2],[8,9]]"
140 ]
141 },
142 "metadata": {},
143 "output_type": "display_data"
144 }
145 ],
146 "source": [
147 "successfulParse $ parseFile \"1 2\\n8 9\""
148 ]
149 },
150 {
151 "cell_type": "code",
152 "execution_count": 11,
153 "metadata": {},
154 "outputs": [],
155 "source": [
156 "cSum :: [Int] -> Int\n",
157 "cSum row = (maximum row) - (minimum row)"
158 ]
159 },
160 {
161 "cell_type": "code",
162 "execution_count": 12,
163 "metadata": {},
164 "outputs": [],
165 "source": [
166 "check :: [[Int]] -> Int\n",
167 "check = sum . (map cSum)"
168 ]
169 },
170 {
171 "cell_type": "code",
172 "execution_count": 13,
173 "metadata": {},
174 "outputs": [
175 {
176 "data": {
177 "text/plain": [
178 "2"
179 ]
180 },
181 "metadata": {},
182 "output_type": "display_data"
183 }
184 ],
185 "source": [
186 "check $ successfulParse $ parseFile \"1 2\\n8 9\""
187 ]
188 },
189 {
190 "cell_type": "code",
191 "execution_count": 14,
192 "metadata": {},
193 "outputs": [],
194 "source": [
195 "part1 = check"
196 ]
197 },
198 {
199 "cell_type": "code",
200 "execution_count": 15,
201 "metadata": {},
202 "outputs": [
203 {
204 "data": {
205 "text/plain": [
206 "[(2,1),(3,1),(4,1),(4,2)]"
207 ]
208 },
209 "metadata": {},
210 "output_type": "display_data"
211 }
212 ],
213 "source": [
214 "digits = [1,2,3,4]\n",
215 "[(a, b) | a <- digits, b <- digits, a /= b, a `mod` b == 0]"
216 ]
217 },
218 {
219 "cell_type": "code",
220 "execution_count": 16,
221 "metadata": {},
222 "outputs": [
223 {
224 "data": {
225 "text/plain": [
226 "[(1,1),(2,2),(3,3),(4,4)]"
227 ]
228 },
229 "metadata": {},
230 "output_type": "display_data"
231 }
232 ],
233 "source": [
234 "zip digits digits"
235 ]
236 },
237 {
238 "cell_type": "code",
239 "execution_count": 17,
240 "metadata": {},
241 "outputs": [
242 {
243 "data": {
244 "text/plain": [
245 "[179,2358,5197,867,163,4418,3135,5049,187,166,4682,5080,5541,172,4294,1397]"
246 ]
247 },
248 "metadata": {},
249 "output_type": "display_data"
250 }
251 ],
252 "source": [
253 "Right digits = parseLine \"179 2358 5197 867 163 4418 3135 5049 187 166 4682 5080 5541 172 4294 1397\"\n",
254 "digits"
255 ]
256 },
257 {
258 "cell_type": "code",
259 "execution_count": 18,
260 "metadata": {},
261 "outputs": [
262 {
263 "data": {
264 "text/plain": [
265 "27"
266 ]
267 },
268 "metadata": {},
269 "output_type": "display_data"
270 }
271 ],
272 "source": [
273 "sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0]"
274 ]
275 },
276 {
277 "cell_type": "code",
278 "execution_count": 19,
279 "metadata": {},
280 "outputs": [],
281 "source": [
282 "p2cSum digits = sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0]"
283 ]
284 },
285 {
286 "cell_type": "code",
287 "execution_count": 20,
288 "metadata": {},
289 "outputs": [],
290 "source": [
291 "part2 = sum . map p2cSum"
292 ]
293 },
294 {
295 "cell_type": "code",
296 "execution_count": 23,
297 "metadata": {},
298 "outputs": [],
299 "source": [
300 "main :: IO ()\n",
301 "main = do \n",
302 " text <- readFile \"../../data/advent02.txt\"\n",
303 " let sheet = successfulParse $ parseFile text\n",
304 "-- print sheet\n",
305 " print $ part1 sheet\n",
306 " print $ part2 sheet"
307 ]
308 },
309 {
310 "cell_type": "code",
311 "execution_count": 24,
312 "metadata": {},
313 "outputs": [
314 {
315 "data": {
316 "text/plain": [
317 "39126\n",
318 "258"
319 ]
320 },
321 "metadata": {},
322 "output_type": "display_data"
323 }
324 ],
325 "source": [
326 "main"
327 ]
328 },
329 {
330 "cell_type": "code",
331 "execution_count": null,
332 "metadata": {},
333 "outputs": [],
334 "source": []
335 }
336 ],
337 "metadata": {
338 "kernelspec": {
339 "display_name": "Haskell",
340 "language": "haskell",
341 "name": "haskell"
342 },
343 "language_info": {
344 "codemirror_mode": "ihaskell",
345 "file_extension": ".hs",
346 "name": "haskell",
347 "version": "8.0.2"
348 }
349 },
350 "nbformat": 4,
351 "nbformat_minor": 2
352 }