9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}"
15 "execution_count": 57,
19 "import Data.List.Split (splitOn)\n",
20 "import qualified Data.Map.Strict as M\n",
21 "import Data.Map.Strict ((!))\n",
22 "import Data.List (sort)"
27 "execution_count": 62,
33 " text <- readFile \"../../data/advent04.txt\"\n",
34 " let passphrases = map parseLine $ lines text\n",
35 " print $ take 10 passphrases\n",
36 " print $ part1 passphrases\n",
37 " print $ part2 passphrases\n",
38 "-- print $ take 10 passphrases\n",
39 "-- part1 triangles\n",
45 "execution_count": 19,
49 "parseLine :: String -> [String]\n",
50 "parseLine = filter (not . null) . splitOn \" \""
55 "execution_count": 36,
59 "frequency :: (Ord a) => [a] -> M.Map a Int \n",
60 "frequency xs = M.fromListWith (+) [(x, 1) | x <- xs]"
65 "execution_count": 63,
71 "[[\"sayndz\",\"zfxlkl\",\"attjtww\",\"cti\",\"sokkmty\",\"brx\",\"fhh\",\"suelqbp\"],[\"xmuf\",\"znkhaes\",\"pggrlp\",\"zia\",\"znkhaes\",\"znkhaes\"],[\"nti\",\"rxr\",\"bogebb\",\"zdwrin\"],[\"sryookh\",\"unrudn\",\"zrkz\",\"jxhrdo\",\"gctlyz\"],[\"bssqn\",\"wbmdc\",\"rigc\",\"zketu\",\"ketichh\",\"enkixg\",\"bmdwc\",\"stnsdf\",\"jnz\",\"mqovwg\",\"ixgken\"],[\"flawt\",\"cpott\",\"xth\",\"ucwgg\",\"xce\",\"jcubx\",\"wvl\",\"qsysa\",\"nlg\"],[\"qovcqn\",\"zxcz\",\"vojsno\",\"nqoqvc\",\"hnf\",\"gqewlkd\",\"uevax\",\"vuna\",\"fxjkbll\",\"vfge\"],[\"qrzf\",\"phwuf\",\"ligf\",\"xgen\",\"vkig\",\"elptd\",\"njdm\",\"gvqiu\",\"epfzsvk\",\"urbltg\",\"dqg\"],[\"sfpku\",\"viwihi\",\"fje\",\"umdkwvi\",\"ejzhzj\",\"qrbl\",\"sfpku\",\"sad\",\"nawnow\",\"ksnku\"],[\"nzhj\",\"mfudick\",\"ueaa\",\"jnhz\",\"kpy\",\"pzk\"]]\n",
77 "output_type": "display_data"
86 "execution_count": 38,
92 "fromList [(\"eild\",1),(\"gcne\",1),(\"jxczomh\",1),(\"kebx\",1),(\"meja\",1),(\"nrskdr\",1)]"
96 "output_type": "display_data"
100 "frequency $ parseLine \"kebx eild nrskdr meja jxczomh gcne\""
105 "execution_count": 40,
109 "containsDuplicates passphrase = (not . M.null) $ M.filter (> 1) $ frequency passphrase"
114 "execution_count": 64,
118 "containsDuplicates = (not . M.null) . M.filter (> 1) . frequency"
123 "execution_count": 65,
133 "output_type": "display_data"
137 "containsDuplicates $ parseLine \"kebx eild nrskdr meja jxczomh gcne\""
142 "execution_count": 66,
152 "output_type": "display_data"
156 "containsDuplicates $ parseLine \"kebx eild nrskdr meja jxczomh gcne meja\""
161 "execution_count": 43,
165 "part1 passphrases = length $ filter (not . containsDuplicates) passphrases"
170 "execution_count": 46,
176 "[[\"sayndz\",\"zfxlkl\",\"attjtww\",\"cti\",\"sokkmty\",\"brx\",\"fhh\",\"suelqbp\"],[\"xmuf\",\"znkhaes\",\"pggrlp\",\"zia\",\"znkhaes\",\"znkhaes\"],[\"nti\",\"rxr\",\"bogebb\",\"zdwrin\"],[\"sryookh\",\"unrudn\",\"zrkz\",\"jxhrdo\",\"gctlyz\"],[\"bssqn\",\"wbmdc\",\"rigc\",\"zketu\",\"ketichh\",\"enkixg\",\"bmdwc\",\"stnsdf\",\"jnz\",\"mqovwg\",\"ixgken\"],[\"flawt\",\"cpott\",\"xth\",\"ucwgg\",\"xce\",\"jcubx\",\"wvl\",\"qsysa\",\"nlg\"],[\"qovcqn\",\"zxcz\",\"vojsno\",\"nqoqvc\",\"hnf\",\"gqewlkd\",\"uevax\",\"vuna\",\"fxjkbll\",\"vfge\"],[\"qrzf\",\"phwuf\",\"ligf\",\"xgen\",\"vkig\",\"elptd\",\"njdm\",\"gvqiu\",\"epfzsvk\",\"urbltg\",\"dqg\"],[\"sfpku\",\"viwihi\",\"fje\",\"umdkwvi\",\"ejzhzj\",\"qrbl\",\"sfpku\",\"sad\",\"nawnow\",\"ksnku\"],[\"nzhj\",\"mfudick\",\"ueaa\",\"jnhz\",\"kpy\",\"pzk\"]]\n",
181 "output_type": "display_data"
190 "execution_count": 47,
196 "fromList [('e',1),('h',1),('l',2),('o',1)]"
200 "output_type": "display_data"
204 "frequency \"hello\""
209 "execution_count": 48,
215 "fromList [('e',1),('h',1),('l',2),('o',1)]"
219 "output_type": "display_data"
223 "frequency \"olleh\""
228 "execution_count": 50,
238 "output_type": "display_data"
242 "M.difference (frequency \"hello\") (frequency \"olleh\")"
247 "execution_count": 53,
257 "output_type": "display_data"
261 "M.difference (frequency \"olleho\") (frequency \"hello\")"
266 "execution_count": 56,
272 "fromList [('e',1),('h',1),('l',2),('o',1)]"
276 "output_type": "display_data"
280 "M.intersection (frequency \"olleh\") (frequency \"oohello\")"
285 "execution_count": 58,
295 "output_type": "display_data"
304 "execution_count": 59,
314 "output_type": "display_data"
323 "execution_count": 60,
327 "containsAnagrams passphrase = containsDuplicates $ map sort passphrase"
332 "execution_count": 61,
336 "part2 passphrases = length $ filter (not . containsAnagrams) passphrases"
341 "execution_count": null,
349 "display_name": "Haskell",
350 "language": "haskell",
354 "codemirror_mode": "ihaskell",
355 "file_extension": ".hs",