66a1ac908026510654c37193b4653becb4b43f6f
[advent-of-code-17.git] / adventofcode1702 / app / 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": 21,
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": 22,
26 "metadata": {},
27 "outputs": [],
28 "source": [
29 "sFile = sLine `sepEndBy` newline \n",
30 "sLine = int `sepBy` onlySpaces"
31 ]
32 },
33 {
34 "cell_type": "code",
35 "execution_count": 23,
36 "metadata": {},
37 "outputs": [],
38 "source": [
39 "onlySpaces = many (oneOf \" \\t\")"
40 ]
41 },
42 {
43 "cell_type": "code",
44 "execution_count": 24,
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": 65,
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": 66,
77 "metadata": {},
78 "outputs": [
79 {
80 "data": {
81 "text/plain": [
82 "39126\n",
83 "258"
84 ]
85 },
86 "metadata": {},
87 "output_type": "display_data"
88 }
89 ],
90 "source": [
91 "main"
92 ]
93 },
94 {
95 "cell_type": "code",
96 "execution_count": 27,
97 "metadata": {},
98 "outputs": [
99 {
100 "data": {
101 "text/plain": [
102 "Right [179,2358,5197,867,163,4418,3135,5049,187,166,4682,5080,5541,172,4294,1397]"
103 ]
104 },
105 "metadata": {},
106 "output_type": "display_data"
107 }
108 ],
109 "source": [
110 "parseLine \"179 2358 5197 867 163 4418 3135 5049 187 166 4682 5080 5541 172 4294 1397\""
111 ]
112 },
113 {
114 "cell_type": "code",
115 "execution_count": 28,
116 "metadata": {},
117 "outputs": [
118 {
119 "data": {
120 "text/plain": [
121 "Right [[1,2],[8,9]]"
122 ]
123 },
124 "metadata": {},
125 "output_type": "display_data"
126 }
127 ],
128 "source": [
129 "parseFile \"1 2\\n8 9\""
130 ]
131 },
132 {
133 "cell_type": "code",
134 "execution_count": 32,
135 "metadata": {},
136 "outputs": [
137 {
138 "data": {
139 "text/plain": [
140 "[[1,2],[8,9]]"
141 ]
142 },
143 "metadata": {},
144 "output_type": "display_data"
145 }
146 ],
147 "source": [
148 "successfulParse $ parseFile \"1 2\\n8 9\""
149 ]
150 },
151 {
152 "cell_type": "code",
153 "execution_count": 37,
154 "metadata": {},
155 "outputs": [],
156 "source": [
157 "cSum :: [Int] -> Int\n",
158 "cSum row = (maximum row) - (minimum row)"
159 ]
160 },
161 {
162 "cell_type": "code",
163 "execution_count": 42,
164 "metadata": {},
165 "outputs": [],
166 "source": [
167 "check :: [[Int]] -> Int\n",
168 "check = sum . (map cSum)"
169 ]
170 },
171 {
172 "cell_type": "code",
173 "execution_count": 43,
174 "metadata": {},
175 "outputs": [
176 {
177 "data": {
178 "text/plain": [
179 "2"
180 ]
181 },
182 "metadata": {},
183 "output_type": "display_data"
184 }
185 ],
186 "source": [
187 "check $ successfulParse $ parseFile \"1 2\\n8 9\""
188 ]
189 },
190 {
191 "cell_type": "code",
192 "execution_count": 45,
193 "metadata": {},
194 "outputs": [],
195 "source": [
196 "part1 = check"
197 ]
198 },
199 {
200 "cell_type": "code",
201 "execution_count": 54,
202 "metadata": {},
203 "outputs": [
204 {
205 "data": {
206 "text/plain": [
207 "[(2,1),(3,1),(4,1),(4,2)]"
208 ]
209 },
210 "metadata": {},
211 "output_type": "display_data"
212 }
213 ],
214 "source": [
215 "digits = [1,2,3,4]\n",
216 "[(a, b) | a <- digits, b <- digits, a /= b, a `mod` b == 0]"
217 ]
218 },
219 {
220 "cell_type": "code",
221 "execution_count": 51,
222 "metadata": {},
223 "outputs": [
224 {
225 "data": {
226 "text/plain": [
227 "[(1,1),(2,2),(3,3),(4,4)]"
228 ]
229 },
230 "metadata": {},
231 "output_type": "display_data"
232 }
233 ],
234 "source": [
235 "zip digits digits"
236 ]
237 },
238 {
239 "cell_type": "code",
240 "execution_count": null,
241 "metadata": {},
242 "outputs": [],
243 "source": []
244 },
245 {
246 "cell_type": "code",
247 "execution_count": 55,
248 "metadata": {},
249 "outputs": [
250 {
251 "data": {
252 "text/plain": [
253 "[179,2358,5197,867,163,4418,3135,5049,187,166,4682,5080,5541,172,4294,1397]"
254 ]
255 },
256 "metadata": {},
257 "output_type": "display_data"
258 }
259 ],
260 "source": [
261 "Right digits = parseLine \"179 2358 5197 867 163 4418 3135 5049 187 166 4682 5080 5541 172 4294 1397\"\n",
262 "digits"
263 ]
264 },
265 {
266 "cell_type": "code",
267 "execution_count": 60,
268 "metadata": {},
269 "outputs": [
270 {
271 "data": {
272 "text/plain": [
273 "27"
274 ]
275 },
276 "metadata": {},
277 "output_type": "display_data"
278 }
279 ],
280 "source": [
281 "sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0]"
282 ]
283 },
284 {
285 "cell_type": "code",
286 "execution_count": 61,
287 "metadata": {},
288 "outputs": [],
289 "source": [
290 "p2cSum digits = sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0]"
291 ]
292 },
293 {
294 "cell_type": "code",
295 "execution_count": 64,
296 "metadata": {},
297 "outputs": [],
298 "source": [
299 "part2 = sum . map p2cSum"
300 ]
301 },
302 {
303 "cell_type": "code",
304 "execution_count": null,
305 "metadata": {},
306 "outputs": [],
307 "source": []
308 }
309 ],
310 "metadata": {
311 "kernelspec": {
312 "display_name": "Haskell",
313 "language": "haskell",
314 "name": "haskell"
315 },
316 "language_info": {
317 "codemirror_mode": "ihaskell",
318 "file_extension": ".hs",
319 "name": "haskell",
320 "version": "8.0.2"
321 }
322 },
323 "nbformat": 4,
324 "nbformat_minor": 2
325 }