9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}"
19 "import Data.List.Split (splitOn)\n",
20 "import Data.List (mapAccumL)"
29 "hexStep (Int, Int) -> String -> (Int, Int)\n",
30 "hexStep (n, ne) s = case s of \n",
31 " \"n\" -> (n + 1, ne)\n",
32 " \"ne\" -> (n, ne + 1)\n",
33 " \"nw\" -> (n + 1, ne - 1)\n",
34 " \"s\" -> (n - 1, ne)\n",
35 " \"se\" -> (n - 1, ne + 1)\n",
36 " \"sw\" -> (n, ne - 1)"
51 "output_type": "display_data"
55 "mapAccumL (\\a b -> (a + 1, a + b)) 0 [1,2,3,4]"
60 "execution_count": 23,
64 "hexPath = foldl hexStep (0, 0)"
69 "execution_count": 24,
79 "output_type": "display_data"
83 "distance $ hexPath $ splitOn \",\" \"ne,ne,ne\""
88 "execution_count": 25,
98 "output_type": "display_data"
102 "distance $ hexPath $ splitOn \",\" \"ne,ne,sw,sw\""
107 "execution_count": 26,
117 "output_type": "display_data"
121 "distance $ hexPath $ splitOn \",\" \"ne,ne,s,s\""
126 "execution_count": 27,
136 "output_type": "display_data"
140 "distance $ hexPath $ splitOn \",\" \"se,sw,se,sw,sw\""
145 "execution_count": 28,
151 "<style>/* Styles used for the Hoogle display in the pager */\n",
154 "padding-bottom: 1.3em;\n",
155 "padding-left: 0.4em;\n",
159 "font-family: monospace;\n",
160 "white-space: pre;\n",
167 "font-weight: bold;\n",
170 "font-weight: bold;\n",
174 "margin-left: 0.4em;\n",
176 ".hoogle-package {\n",
177 "font-weight: bold;\n",
178 "font-style: italic;\n",
180 ".hoogle-module {\n",
181 "font-weight: bold;\n",
184 "font-weight: bold;\n",
188 "font-weight: bold;\n",
189 "font-family: monospace;\n",
191 "white-space: pre-wrap;\n",
195 "font-weight: bold;\n",
196 "font-family: monospace;\n",
197 "margin-left: 1em;\n",
200 "font-family: monospace;\n",
205 "font-style: italic;\n",
206 "font-family: monospace;\n",
207 "white-space: pre;\n",
212 "font-weight: bold;\n",
214 ".err-msg.in.collapse {\n",
215 "padding-top: 0.7em;\n",
217 ".highlight-code {\n",
218 "white-space: pre;\n",
219 "font-family: monospace;\n",
221 ".suggestion-warning { \n",
222 "font-weight: bold;\n",
223 "color: rgb(200, 130, 0);\n",
225 ".suggestion-error { \n",
226 "font-weight: bold;\n",
229 ".suggestion-name {\n",
230 "font-weight: bold;\n",
232 "</style><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(abs n) + (abs ne)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">abs n + (abs ne)</div></div><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(abs n) + (abs ne)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">(abs n) + abs ne</div></div><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(abs n) - smallest</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">abs n - smallest</div></div><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(abs ne) - smallest</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">abs ne - smallest</div></div>"
235 "Line 2: Redundant bracket\n",
237 "(abs n) + (abs ne)\n",
239 "abs n + (abs ne)Line 2: Redundant bracket\n",
241 "(abs n) + (abs ne)\n",
243 "(abs n) + abs neLine 5: Redundant bracket\n",
245 "(abs n) - smallest\n",
247 "abs n - smallestLine 5: Redundant bracket\n",
249 "(abs ne) - smallest\n",
255 "output_type": "display_data"
259 "distance (n, ne) = if n * ne > 0 \n",
260 " then (abs n) + (abs ne)\n",
261 " else smallest + remainder\n",
262 " where smallest = min (abs n) (abs ne)\n",
263 " remainder = max ((abs n) - smallest) ((abs ne) - smallest)"
268 "execution_count": 29,
272 "part1 = distance . hexPath . splitOn \",\""
277 "execution_count": 30,
283 " text <- readFile \"../../data/advent11.txt\"\n",
284 "-- let instrs = map read $ splitOn \",\" text\n",
285 " print $ part1 text\n",
286 "-- print $ part2 text"
291 "execution_count": 31,
301 "output_type": "display_data"
310 "execution_count": 32,
314 "hexPathB = scanl hexStep (0, 0)"
319 "execution_count": 36,
329 "output_type": "display_data"
333 "maximum $ map distance $ hexPathB $ splitOn \",\" \"ne,ne,ne\""
338 "execution_count": 37,
344 "[(0,0),(0,1),(0,2),(0,3)]"
348 "output_type": "display_data"
352 "hexPathB $ splitOn \",\" \"ne,ne,ne\""
357 "execution_count": 38,
367 "output_type": "display_data"
371 "map distance $ hexPathB $ splitOn \",\" \"ne,ne,ne\""
376 "execution_count": 39,
382 "([(0,0),(0,1),(0,2),(0,3)],[0,1,2,3],3)"
386 "output_type": "display_data"
390 "pth = splitOn \",\" \"ne,ne,ne\"\n",
391 "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)"
396 "execution_count": 40,
402 "([(0,0),(0,1),(0,2),(0,1),(0,0)],[0,1,2,1,0],2)"
406 "output_type": "display_data"
410 "pth = splitOn \",\" \"ne,ne,sw,sw\"\n",
411 "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)"
416 "execution_count": 41,
422 "([(0,0),(0,1),(0,2),(-1,2),(-2,2)],[0,1,2,2,2],2)"
426 "output_type": "display_data"
430 "pth = splitOn \",\" \"ne,ne,s,s\"\n",
431 "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)"
436 "execution_count": 42,
442 "([(0,0),(-1,1),(-1,0),(-2,1),(-2,0),(-2,-1)],[0,1,1,2,2,3],3)"
446 "output_type": "display_data"
450 "pth = splitOn \",\" \"se,sw,se,sw,sw\"\n",
451 "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)"
456 "execution_count": 43,
460 "part2 = maximum . map distance . hexPathB . splitOn \",\""
465 "execution_count": 44,
471 " text <- readFile \"../../data/advent11.txt\"\n",
472 "-- let instrs = map read $ splitOn \",\" text\n",
473 " print $ part1 text\n",
474 " print $ part2 text"
479 "execution_count": 45,
490 "output_type": "display_data"
499 "execution_count": null,
507 "display_name": "Haskell",
508 "language": "haskell",
512 "codemirror_mode": "ihaskell",
513 "file_extension": ".hs",