63a54e506894e3bac4e28e788478a951c5875aa5
[advent-of-code-17.git] / src / advent04 / advent04.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": 57,
16 "metadata": {},
17 "outputs": [],
18 "source": [
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)"
23 ]
24 },
25 {
26 "cell_type": "code",
27 "execution_count": 62,
28 "metadata": {},
29 "outputs": [],
30 "source": [
31 "main :: IO ()\n",
32 "main = do \n",
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",
40 "-- part2 triangles"
41 ]
42 },
43 {
44 "cell_type": "code",
45 "execution_count": 19,
46 "metadata": {},
47 "outputs": [],
48 "source": [
49 "parseLine :: String -> [String]\n",
50 "parseLine = filter (not . null) . splitOn \" \""
51 ]
52 },
53 {
54 "cell_type": "code",
55 "execution_count": 36,
56 "metadata": {},
57 "outputs": [],
58 "source": [
59 "frequency :: (Ord a) => [a] -> M.Map a Int \n",
60 "frequency xs = M.fromListWith (+) [(x, 1) | x <- xs]"
61 ]
62 },
63 {
64 "cell_type": "code",
65 "execution_count": 63,
66 "metadata": {},
67 "outputs": [
68 {
69 "data": {
70 "text/plain": [
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",
72 "383\n",
73 "265"
74 ]
75 },
76 "metadata": {},
77 "output_type": "display_data"
78 }
79 ],
80 "source": [
81 "main"
82 ]
83 },
84 {
85 "cell_type": "code",
86 "execution_count": 38,
87 "metadata": {},
88 "outputs": [
89 {
90 "data": {
91 "text/plain": [
92 "fromList [(\"eild\",1),(\"gcne\",1),(\"jxczomh\",1),(\"kebx\",1),(\"meja\",1),(\"nrskdr\",1)]"
93 ]
94 },
95 "metadata": {},
96 "output_type": "display_data"
97 }
98 ],
99 "source": [
100 "frequency $ parseLine \"kebx eild nrskdr meja jxczomh gcne\""
101 ]
102 },
103 {
104 "cell_type": "code",
105 "execution_count": 40,
106 "metadata": {},
107 "outputs": [],
108 "source": [
109 "containsDuplicates passphrase = (not . M.null) $ M.filter (> 1) $ frequency passphrase"
110 ]
111 },
112 {
113 "cell_type": "code",
114 "execution_count": 64,
115 "metadata": {},
116 "outputs": [],
117 "source": [
118 "containsDuplicates = (not . M.null) . M.filter (> 1) . frequency"
119 ]
120 },
121 {
122 "cell_type": "code",
123 "execution_count": 65,
124 "metadata": {},
125 "outputs": [
126 {
127 "data": {
128 "text/plain": [
129 "False"
130 ]
131 },
132 "metadata": {},
133 "output_type": "display_data"
134 }
135 ],
136 "source": [
137 "containsDuplicates $ parseLine \"kebx eild nrskdr meja jxczomh gcne\""
138 ]
139 },
140 {
141 "cell_type": "code",
142 "execution_count": 66,
143 "metadata": {},
144 "outputs": [
145 {
146 "data": {
147 "text/plain": [
148 "True"
149 ]
150 },
151 "metadata": {},
152 "output_type": "display_data"
153 }
154 ],
155 "source": [
156 "containsDuplicates $ parseLine \"kebx eild nrskdr meja jxczomh gcne meja\""
157 ]
158 },
159 {
160 "cell_type": "code",
161 "execution_count": 43,
162 "metadata": {},
163 "outputs": [],
164 "source": [
165 "part1 passphrases = length $ filter (not . containsDuplicates) passphrases"
166 ]
167 },
168 {
169 "cell_type": "code",
170 "execution_count": 46,
171 "metadata": {},
172 "outputs": [
173 {
174 "data": {
175 "text/plain": [
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",
177 "383"
178 ]
179 },
180 "metadata": {},
181 "output_type": "display_data"
182 }
183 ],
184 "source": [
185 "main"
186 ]
187 },
188 {
189 "cell_type": "code",
190 "execution_count": 47,
191 "metadata": {},
192 "outputs": [
193 {
194 "data": {
195 "text/plain": [
196 "fromList [('e',1),('h',1),('l',2),('o',1)]"
197 ]
198 },
199 "metadata": {},
200 "output_type": "display_data"
201 }
202 ],
203 "source": [
204 "frequency \"hello\""
205 ]
206 },
207 {
208 "cell_type": "code",
209 "execution_count": 48,
210 "metadata": {},
211 "outputs": [
212 {
213 "data": {
214 "text/plain": [
215 "fromList [('e',1),('h',1),('l',2),('o',1)]"
216 ]
217 },
218 "metadata": {},
219 "output_type": "display_data"
220 }
221 ],
222 "source": [
223 "frequency \"olleh\""
224 ]
225 },
226 {
227 "cell_type": "code",
228 "execution_count": 50,
229 "metadata": {},
230 "outputs": [
231 {
232 "data": {
233 "text/plain": [
234 "fromList []"
235 ]
236 },
237 "metadata": {},
238 "output_type": "display_data"
239 }
240 ],
241 "source": [
242 "M.difference (frequency \"hello\") (frequency \"olleh\")"
243 ]
244 },
245 {
246 "cell_type": "code",
247 "execution_count": 53,
248 "metadata": {},
249 "outputs": [
250 {
251 "data": {
252 "text/plain": [
253 "fromList []"
254 ]
255 },
256 "metadata": {},
257 "output_type": "display_data"
258 }
259 ],
260 "source": [
261 "M.difference (frequency \"olleho\") (frequency \"hello\")"
262 ]
263 },
264 {
265 "cell_type": "code",
266 "execution_count": 56,
267 "metadata": {},
268 "outputs": [
269 {
270 "data": {
271 "text/plain": [
272 "fromList [('e',1),('h',1),('l',2),('o',1)]"
273 ]
274 },
275 "metadata": {},
276 "output_type": "display_data"
277 }
278 ],
279 "source": [
280 "M.intersection (frequency \"olleh\") (frequency \"oohello\")"
281 ]
282 },
283 {
284 "cell_type": "code",
285 "execution_count": 58,
286 "metadata": {},
287 "outputs": [
288 {
289 "data": {
290 "text/plain": [
291 "\"ehllo\""
292 ]
293 },
294 "metadata": {},
295 "output_type": "display_data"
296 }
297 ],
298 "source": [
299 "sort \"hello\""
300 ]
301 },
302 {
303 "cell_type": "code",
304 "execution_count": 59,
305 "metadata": {},
306 "outputs": [
307 {
308 "data": {
309 "text/plain": [
310 "\"ehllo\""
311 ]
312 },
313 "metadata": {},
314 "output_type": "display_data"
315 }
316 ],
317 "source": [
318 "sort \"olleh\""
319 ]
320 },
321 {
322 "cell_type": "code",
323 "execution_count": 60,
324 "metadata": {},
325 "outputs": [],
326 "source": [
327 "containsAnagrams passphrase = containsDuplicates $ map sort passphrase"
328 ]
329 },
330 {
331 "cell_type": "code",
332 "execution_count": 61,
333 "metadata": {},
334 "outputs": [],
335 "source": [
336 "part2 passphrases = length $ filter (not . containsAnagrams) passphrases"
337 ]
338 },
339 {
340 "cell_type": "code",
341 "execution_count": null,
342 "metadata": {},
343 "outputs": [],
344 "source": []
345 }
346 ],
347 "metadata": {
348 "kernelspec": {
349 "display_name": "Haskell",
350 "language": "haskell",
351 "name": "haskell"
352 },
353 "language_info": {
354 "codemirror_mode": "ihaskell",
355 "file_extension": ".hs",
356 "name": "haskell",
357 "version": "8.0.2"
358 }
359 },
360 "nbformat": 4,
361 "nbformat_minor": 2
362 }