9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}"
15 "execution_count": 113,
19 "import Data.List.Split (splitOn, chunksOf)\n",
20 "import Data.Char (ord, chr)\n",
22 "import Numeric (showHex)\n",
32 "extract items from len = take len $ drop from $ items ++ items"
37 "execution_count": 24,
41 "-- replace original replacement from \n",
42 "-- | from + length replacement <= length original = replacement ++ drop (length replacement) original\n",
43 "-- | otherwise = drop (length original) extended ++ \n",
44 "-- where extended = take from original ++ replacement ++ drop suflen original\n",
45 "-- suflen = from + length replacement - length original "
50 "execution_count": 51,
54 "replace original replacement from = take (length original) (start ++ replacement ++ remainder)\n",
55 " where excess = drop (length original - from) replacement\n",
56 " stub = drop (length excess) original\n",
57 " start = take from (excess ++ stub)\n",
58 " remainder = drop (length $ start ++ replacement) original "
63 "execution_count": 52,
73 "output_type": "display_data"
77 "extract [0, 1, 2, 3, 4] 0 3"
82 "execution_count": 53,
92 "output_type": "display_data"
101 "execution_count": 54,
110 "execution_count": 55,
120 "output_type": "display_data"
124 "replace l0 (reverse $ extract l0 0 3) 0"
129 "execution_count": 56,
139 "output_type": "display_data"
148 "execution_count": 57,
158 "output_type": "display_data"
162 "replace l0 (reverse $ extract l0 4 3) 4"
166 "cell_type": "markdown",
174 "0 1 2 3 4 | 0 1 2 3 4\n",
175 "0 1 2 3 [4] | 0 1 2 3 4\n",
176 "0 1 2 3 ([4] | 0 1) 2 3 4\n",
177 "0 1 2 3 ( 1 | 0 4) 2 3 4\n",
180 " 0 1 2 3 4 | 0 1 2 3 4\n",
181 " [0] 1 2 3 4 | 0 1 2 3 4\n",
182 "([0] 1 2) 3 4 | 0 1 2 3 4\n",
183 "( 2 1 0) 3 4 | 0 1 2 3 4"
188 "execution_count": 58,
192 "tie original start len = replace original replacement start\n",
193 " where replacement = reverse $ extract original start len"
198 "execution_count": 59,
204 "<style>/* Styles used for the Hoogle display in the pager */\n",
207 "padding-bottom: 1.3em;\n",
208 "padding-left: 0.4em;\n",
212 "font-family: monospace;\n",
213 "white-space: pre;\n",
220 "font-weight: bold;\n",
223 "font-weight: bold;\n",
227 "margin-left: 0.4em;\n",
229 ".hoogle-package {\n",
230 "font-weight: bold;\n",
231 "font-style: italic;\n",
233 ".hoogle-module {\n",
234 "font-weight: bold;\n",
237 "font-weight: bold;\n",
241 "font-weight: bold;\n",
242 "font-family: monospace;\n",
244 "white-space: pre-wrap;\n",
248 "font-weight: bold;\n",
249 "font-family: monospace;\n",
250 "margin-left: 1em;\n",
253 "font-family: monospace;\n",
258 "font-style: italic;\n",
259 "font-family: monospace;\n",
260 "white-space: pre;\n",
265 "font-weight: bold;\n",
267 ".err-msg.in.collapse {\n",
268 "padding-top: 0.7em;\n",
270 ".highlight-code {\n",
271 "white-space: pre;\n",
272 "font-family: monospace;\n",
274 ".suggestion-warning { \n",
275 "font-weight: bold;\n",
276 "color: rgb(200, 130, 0);\n",
278 ".suggestion-error { \n",
279 "font-weight: bold;\n",
282 ".suggestion-name {\n",
283 "font-weight: bold;\n",
285 "</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\">(start + len + skip) `mod` (length original)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">(start + len + skip) `mod` length original</div></div>"
288 "Line 3: Redundant bracket\n",
290 "(start + len + skip) `mod` (length original)\n",
292 "(start + len + skip) `mod` length original"
296 "output_type": "display_data"
300 "step (original, start, skip) len = (replaced, start', skip + 1)\n",
301 " where replaced = tie original start len\n",
302 " start' = (start + len + skip) `mod` (length original)"
307 "execution_count": 60,
317 "output_type": "display_data"
326 "execution_count": 61,
336 "output_type": "display_data"
340 "step ([0..4], 0, 0) 3"
345 "execution_count": 66,
355 "output_type": "display_data"
359 "foldl step ([0..4], 0, 0) [3, 4, 1, 5]"
364 "execution_count": 79,
368 "part1 :: [Int] -> Int\n",
369 "part1 lengths = (tied!!0) * (tied!!1)\n",
370 " where (tied, _, _) = foldl step ([0..255], 0, 0) lengths"
375 "execution_count": 101,
379 "part2 text = tied\n",
380 " where lengths = p2lengths text\n",
381 " (tied, _, _) = foldl step ([0..255], 0, 0) lengths"
386 "execution_count": 102,
392 " text <- readFile \"../../data/advent10.txt\"\n",
393 " let instrs = map read $ splitOn \",\" text\n",
394 " print $ part1 instrs\n",
395 " print $ part2 text"
400 "execution_count": 103,
407 "[166,221,219,74,89,118,226,182,77,185,188,87,238,181,229,173,45,68,84,147,10,194,196,201,55,80,171,119,186,54,154,46,52,161,9,149,135,255,180,210,129,214,127,18,51,236,56,78,24,187,97,25,131,70,143,106,211,218,189,224,179,220,41,141,240,233,76,26,253,207,159,172,86,241,136,215,12,197,112,62,23,138,0,169,99,50,235,205,122,103,116,71,251,160,206,22,4,202,252,40,234,242,193,39,32,191,63,217,152,117,33,163,61,213,164,133,6,16,110,58,102,43,126,67,31,30,95,199,2,183,1,208,239,96,20,13,57,66,38,132,177,195,44,203,247,65,14,227,100,82,151,3,28,150,98,146,140,37,42,120,168,91,83,27,209,148,176,250,101,162,145,79,115,15,228,192,142,104,155,29,19,53,49,35,128,5,184,248,222,123,124,216,178,64,113,198,244,245,246,167,47,11,170,212,109,114,237,94,144,69,243,254,121,231,59,108,17,153,249,34,72,7,130,21,200,175,81,134,105,137,75,165,156,174,232,125,92,88,8,93,157,223,225,48,111,85,107,204,36,158,90,139,190,230,60,73]"
411 "output_type": "display_data"
420 "execution_count": 85,
430 "output_type": "display_data"
439 "execution_count": 86,
449 "output_type": "display_data"
458 "execution_count": 88,
468 "output_type": "display_data"
477 "execution_count": 112,
487 "output_type": "display_data"
496 "execution_count": 116,
506 "output_type": "display_data"
510 "printf \"%02x\" 255"
515 "execution_count": 93,
519 "p2lengths text = take (length chunk * 64) $ cycle chunk\n",
520 " where chunk = map ord text ++ [17, 31, 73, 47, 23]"
525 "execution_count": 117,
529 "densify ns = concatMap (printf \"%02x\") codes\n",
530 " where chunks = chunksOf 16 ns\n",
531 " compress = foldl1 xor\n",
532 " codes = map compress chunks"
537 "execution_count": 126,
541 "part2 :: String -> String\n",
542 "part2 text = densify tied\n",
543 " where lengths = p2lengths text\n",
544 " (tied, _, _) = foldl step ([0..255], 0, 0) lengths"
549 "execution_count": 121,
555 "a2582a3a0e66e6e86e3812dcb672a272"
559 "output_type": "display_data"
563 "putStrLn $ part2 \"\""
568 "execution_count": 122,
574 "33efeb34ea91902bb2f59c9920caa6cd"
578 "output_type": "display_data"
582 "putStrLn $ part2 \"AoC 2017\""
587 "execution_count": 123,
593 "3efbe78a8d82f29979031a4aa0b16a9d"
597 "output_type": "display_data"
601 "putStrLn $ part2 \"1,2,3\""
606 "execution_count": 124,
612 "63960835bcdc130f0b66d7ff4f6a5a8e"
616 "output_type": "display_data"
620 "putStrLn $ part2 \"1,2,4\""
625 "execution_count": 129,
631 " text <- readFile \"../../data/advent10.txt\"\n",
632 " let instrs = map read $ splitOn \",\" text\n",
633 " print $ part1 instrs\n",
634 " putStrLn $ part2 text"
639 "execution_count": 130,
646 "9de8846431eef262be78f590e39a4848"
650 "output_type": "display_data"
659 "execution_count": null,
667 "display_name": "Haskell",
668 "language": "haskell",
672 "codemirror_mode": "ihaskell",
673 "file_extension": ".hs",