9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}"
19 "import Data.List (tails)"
33 "execution_count": null,
44 "sum_valid_pairs :: [String] -> Integer\n",
45 "sum_valid_pairs possibles = sum $ map (read . take 1) \n",
46 " $ filter (\\(x:y:_) -> x == y) \n",
47 " $ filter (\\p -> length p == 2) possibles"
56 "part1_extract :: String -> [String] \n",
57 "part1_extract digits = map (take 2) $ tails (digits ++ [head digits])"
66 "part1 :: String -> Integer \n",
67 "part1 = sum_valid_pairs . part1_extract"
82 "output_type": "display_data"
101 "output_type": "display_data"
110 "execution_count": 9,
120 "output_type": "display_data"
129 "execution_count": 10,
139 "output_type": "display_data"
148 "execution_count": 11,
152 "part2_extract :: String -> [String]\n",
153 "part2_extract digits = map (\\ds -> (take 1 ds) ++ (take 1 $ drop offset ds)) \n",
154 " $ take (length digits) \n",
155 " $ tails (digits ++ digits)\n",
156 " where offset = length digits `div` 2"
161 "execution_count": 12,
165 "part2 :: String -> Integer \n",
166 "part2 = sum_valid_pairs . part2_extract"
171 "execution_count": 13,
177 "[\"12\",\"12\",\"21\",\"21\"]"
181 "output_type": "display_data"
185 "part2_extract \"1122\""
190 "execution_count": 14,
200 "output_type": "display_data"
209 "execution_count": 15,
215 "[\"11\",\"22\",\"11\",\"22\"]"
219 "output_type": "display_data"
223 "part2_extract \"1212\""
228 "execution_count": 16,
238 "output_type": "display_data"
247 "execution_count": 17,
253 "[\"12\",\"21\",\"21\",\"12\"]"
257 "output_type": "display_data"
261 "part2_extract \"1221\""
266 "execution_count": 18,
276 "output_type": "display_data"
285 "execution_count": 19,
291 "[\"14\",\"22\",\"35\",\"41\",\"22\",\"53\"]"
295 "output_type": "display_data"
299 "part2_extract \"123425\""
304 "execution_count": 20,
314 "output_type": "display_data"
323 "execution_count": 21,
329 "[\"11\",\"22\",\"33\",\"11\",\"22\",\"33\"]"
333 "output_type": "display_data"
337 "part2_extract \"123123\""
342 "execution_count": 22,
352 "output_type": "display_data"
361 "execution_count": 23,
367 "[\"11\",\"24\",\"11\",\"35\",\"11\",\"42\",\"11\",\"53\"]"
371 "output_type": "display_data"
375 "part2_extract \"12131415\""
380 "execution_count": 24,
390 "output_type": "display_data"
399 "execution_count": 25,
405 " digits <- readFile \"../../data/advent01.txt\"\n",
406 " print $ part1 digits\n",
407 " print $ part2 digits"
412 "execution_count": 26,
423 "output_type": "display_data"
432 "execution_count": null,
440 "display_name": "Haskell",
441 "language": "haskell",
445 "codemirror_mode": "ihaskell",
446 "file_extension": ".hs",