diff options
author | Akshay <[email protected]> | 2020-12-08 07:03:22 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-08 07:03:22 +0000 |
commit | 06b14ed84c0a8ec11462f0a6f7397c3e3a52654d (patch) | |
tree | 5d28d13ede49b48f63b42fd31816f0e98a68aca6 | |
parent | 90ee9892e2e0fd188bac09c50987c967cf282333 (diff) |
add initial day08 solution
-rw-r--r-- | aoc.cabal | 6 | ||||
-rw-r--r-- | execs/Day08.hs | 45 | ||||
-rw-r--r-- | input/08 | 626 |
3 files changed, 677 insertions, 0 deletions
@@ -63,3 +63,9 @@ executable Day07 | |||
63 | build-depends: base, aoc, containers, parsec | 63 | build-depends: base, aoc, containers, parsec |
64 | default-language: Haskell2010 | 64 | default-language: Haskell2010 |
65 | hs-source-dirs: execs | 65 | hs-source-dirs: execs |
66 | |||
67 | executable Day08 | ||
68 | main-is: Day08.hs | ||
69 | build-depends: base, aoc, containers | ||
70 | default-language: Haskell2010 | ||
71 | hs-source-dirs: execs | ||
diff --git a/execs/Day08.hs b/execs/Day08.hs new file mode 100644 index 0000000..7d3fce8 --- /dev/null +++ b/execs/Day08.hs | |||
@@ -0,0 +1,45 @@ | |||
1 | module Main where | ||
2 | |||
3 | import Data.Set (Set) | ||
4 | import qualified Data.Set as Set | ||
5 | import Data.Map (Map) | ||
6 | import qualified Data.Map as Map | ||
7 | import Utils | ||
8 | |||
9 | type Op = (String, Int) | ||
10 | |||
11 | parseLine :: [String] -> Op | ||
12 | parseLine [s, j] = (s, read (dropWhile (== '+') j)) | ||
13 | |||
14 | run :: Int -> Int -> Set Int -> Map Int Op -> Int | ||
15 | run acc pc visited operations = if Set.member pc visited then acc else handleCase | ||
16 | where visited' = Set.insert pc visited | ||
17 | handleCase = | ||
18 | case Map.lookup pc operations of | ||
19 | Just ("acc", v) -> run (acc + v) (pc + 1) visited' operations | ||
20 | Just ("nop", _) -> run acc (pc + 1) visited' operations | ||
21 | Just ("jmp", j) -> run acc (pc + j) visited' operations | ||
22 | _ -> acc | ||
23 | |||
24 | doesEnd :: Int -> Int -> Set Int -> Map Int Op -> Bool | ||
25 | doesEnd acc pc visited operations = not (Set.member pc visited) && handleCase | ||
26 | where visited' = Set.insert pc visited | ||
27 | handleCase = | ||
28 | case Map.lookup pc operations of | ||
29 | Just ("acc", v) -> doesEnd (acc + v) (pc + 1) visited' operations | ||
30 | Just ("nop", _) -> doesEnd acc (pc + 1) visited' operations | ||
31 | Just ("jmp", j) -> doesEnd acc (pc + j) visited' operations | ||
32 | _ -> True -- pc has crossed the end! | ||
33 | |||
34 | genAll :: [Op] -> [[Op]] | ||
35 | genAll [] = [] | ||
36 | genAll (n@("nop",v):rest) = (("jmp",v):rest) : map (n:) (genAll rest) | ||
37 | genAll (j@("jmp",v):rest) = (("nop",v):rest) : map (j:) (genAll rest) | ||
38 | genAll (acc:rest) = map (acc:) $ genAll rest | ||
39 | |||
40 | main :: IO () | ||
41 | main = do | ||
42 | n <- map (parseLine . words) . lines <$> readFile "input/08" | ||
43 | let solve1 = run 0 0 Set.empty . Map.fromList . zip [0..] | ||
44 | print $ solve1 n | ||
45 | print $ solve1 $ head $ filter (doesEnd 0 0 Set.empty . Map.fromList . zip [0..]) $ genAll n | ||
diff --git a/input/08 b/input/08 new file mode 100644 index 0000000..4d50e16 --- /dev/null +++ b/input/08 | |||
@@ -0,0 +1,626 @@ | |||
1 | nop +346 | ||
2 | acc +44 | ||
3 | acc +15 | ||
4 | jmp +473 | ||
5 | acc +29 | ||
6 | acc -13 | ||
7 | jmp +525 | ||
8 | acc +22 | ||
9 | acc +13 | ||
10 | nop +265 | ||
11 | jmp +397 | ||
12 | jmp +39 | ||
13 | acc +39 | ||
14 | nop -1 | ||
15 | acc +36 | ||
16 | acc +25 | ||
17 | jmp +153 | ||
18 | nop +374 | ||
19 | jmp +27 | ||
20 | jmp +282 | ||
21 | jmp +1 | ||
22 | jmp +1 | ||
23 | jmp +15 | ||
24 | acc +33 | ||
25 | acc -3 | ||
26 | jmp +533 | ||
27 | acc +25 | ||
28 | acc -14 | ||
29 | acc -16 | ||
30 | jmp +245 | ||
31 | nop +567 | ||
32 | acc +7 | ||
33 | jmp +147 | ||
34 | acc +26 | ||
35 | acc +40 | ||
36 | acc -9 | ||
37 | jmp +295 | ||
38 | acc +14 | ||
39 | nop +388 | ||
40 | acc -1 | ||
41 | jmp -21 | ||
42 | nop +524 | ||
43 | nop +166 | ||
44 | nop +515 | ||
45 | jmp +18 | ||
46 | jmp +214 | ||
47 | acc +42 | ||
48 | nop -35 | ||
49 | acc +7 | ||
50 | jmp +492 | ||
51 | acc +14 | ||
52 | acc +48 | ||
53 | jmp +326 | ||
54 | acc +48 | ||
55 | acc -18 | ||
56 | acc -5 | ||
57 | jmp +343 | ||
58 | jmp +81 | ||
59 | acc +18 | ||
60 | acc +16 | ||
61 | acc +21 | ||
62 | jmp +355 | ||
63 | acc +48 | ||
64 | nop +358 | ||
65 | acc +49 | ||
66 | acc -2 | ||
67 | jmp +89 | ||
68 | acc +4 | ||
69 | jmp +171 | ||
70 | acc +6 | ||
71 | jmp +299 | ||
72 | acc -8 | ||
73 | jmp +150 | ||
74 | acc +41 | ||
75 | acc -9 | ||
76 | acc +48 | ||
77 | jmp +73 | ||
78 | jmp +523 | ||
79 | nop +471 | ||
80 | jmp +493 | ||
81 | acc -16 | ||
82 | nop +440 | ||
83 | acc +2 | ||
84 | acc +33 | ||
85 | jmp +117 | ||
86 | acc +3 | ||
87 | acc +34 | ||
88 | jmp +310 | ||
89 | acc +8 | ||
90 | jmp +197 | ||
91 | acc +26 | ||
92 | acc +47 | ||
93 | jmp +194 | ||
94 | nop +115 | ||
95 | jmp +259 | ||
96 | nop +456 | ||
97 | jmp +420 | ||
98 | nop +398 | ||
99 | jmp +235 | ||
100 | acc +44 | ||
101 | acc +47 | ||
102 | jmp +218 | ||
103 | jmp +1 | ||
104 | jmp +275 | ||
105 | acc +12 | ||
106 | jmp +434 | ||
107 | acc +50 | ||
108 | acc +10 | ||
109 | nop +361 | ||
110 | jmp +367 | ||
111 | acc -16 | ||
112 | acc +44 | ||
113 | jmp +96 | ||
114 | acc +9 | ||
115 | acc +38 | ||
116 | acc -15 | ||
117 | nop -31 | ||
118 | jmp -55 | ||
119 | nop +421 | ||
120 | acc +50 | ||
121 | acc +12 | ||
122 | jmp -64 | ||
123 | acc +33 | ||
124 | acc +25 | ||
125 | jmp +382 | ||
126 | acc +7 | ||
127 | jmp -22 | ||
128 | jmp +95 | ||
129 | acc +44 | ||
130 | acc +32 | ||
131 | acc +23 | ||
132 | jmp +1 | ||
133 | jmp +456 | ||
134 | acc +49 | ||
135 | jmp +15 | ||
136 | jmp +312 | ||
137 | acc +6 | ||
138 | jmp +216 | ||
139 | acc +7 | ||
140 | jmp +458 | ||
141 | jmp +465 | ||
142 | nop +372 | ||
143 | acc +35 | ||
144 | acc +32 | ||
145 | acc +13 | ||
146 | jmp -35 | ||
147 | acc +50 | ||
148 | nop +32 | ||
149 | jmp +143 | ||
150 | jmp +327 | ||
151 | acc +0 | ||
152 | nop -82 | ||
153 | nop -62 | ||
154 | acc +41 | ||
155 | jmp -81 | ||
156 | acc -10 | ||
157 | nop -106 | ||
158 | jmp +82 | ||
159 | acc +1 | ||
160 | acc +11 | ||
161 | jmp +124 | ||
162 | acc +25 | ||
163 | acc +17 | ||
164 | jmp -73 | ||
165 | nop +8 | ||
166 | acc +29 | ||
167 | acc +33 | ||
168 | acc +10 | ||
169 | jmp +123 | ||
170 | jmp +236 | ||
171 | acc +41 | ||
172 | jmp +370 | ||
173 | acc +17 | ||
174 | acc -13 | ||
175 | acc +35 | ||
176 | jmp -47 | ||
177 | nop +287 | ||
178 | acc +22 | ||
179 | jmp +38 | ||
180 | jmp +1 | ||
181 | nop -52 | ||
182 | nop -9 | ||
183 | acc +22 | ||
184 | jmp +253 | ||
185 | acc +12 | ||
186 | acc -18 | ||
187 | acc +21 | ||
188 | nop -69 | ||
189 | jmp +28 | ||
190 | acc +16 | ||
191 | jmp +392 | ||
192 | jmp +325 | ||
193 | jmp -74 | ||
194 | acc +34 | ||
195 | acc +47 | ||
196 | acc +41 | ||
197 | jmp +201 | ||
198 | nop +361 | ||
199 | acc +50 | ||
200 | jmp +30 | ||
201 | jmp -127 | ||
202 | nop -171 | ||
203 | jmp +349 | ||
204 | acc +11 | ||
205 | nop +156 | ||
206 | acc +1 | ||
207 | acc -18 | ||
208 | jmp +393 | ||
209 | acc -8 | ||
210 | jmp +1 | ||
211 | acc -17 | ||
212 | nop +188 | ||
213 | jmp +134 | ||
214 | acc -9 | ||
215 | acc -14 | ||
216 | jmp +206 | ||
217 | jmp -209 | ||
218 | jmp +1 | ||
219 | acc +49 | ||
220 | nop +112 | ||
221 | acc -4 | ||
222 | jmp -20 | ||
223 | acc +41 | ||
224 | jmp -145 | ||
225 | acc +8 | ||
226 | jmp +276 | ||
227 | acc +48 | ||
228 | jmp -5 | ||
229 | jmp -143 | ||
230 | acc +0 | ||
231 | jmp -161 | ||
232 | jmp +238 | ||
233 | acc +8 | ||
234 | jmp -134 | ||
235 | acc +34 | ||
236 | acc +10 | ||
237 | jmp +1 | ||
238 | nop +109 | ||
239 | jmp -100 | ||
240 | acc +41 | ||
241 | acc -4 | ||
242 | jmp -12 | ||
243 | acc +42 | ||
244 | acc +46 | ||
245 | acc -7 | ||
246 | acc +28 | ||
247 | jmp +85 | ||
248 | jmp +216 | ||
249 | jmp +364 | ||
250 | acc +0 | ||
251 | acc -6 | ||
252 | nop +331 | ||
253 | acc +33 | ||
254 | jmp +163 | ||
255 | acc +37 | ||
256 | acc +20 | ||
257 | acc +33 | ||
258 | acc +45 | ||
259 | jmp -159 | ||
260 | acc +34 | ||
261 | acc +10 | ||
262 | acc +48 | ||
263 | acc +10 | ||
264 | jmp +358 | ||
265 | acc -9 | ||
266 | jmp +276 | ||
267 | acc +27 | ||
268 | acc +45 | ||
269 | nop +129 | ||
270 | acc +32 | ||
271 | jmp +243 | ||
272 | acc +0 | ||
273 | acc -5 | ||
274 | jmp -24 | ||
275 | acc +44 | ||
276 | nop +307 | ||
277 | acc -18 | ||
278 | acc +13 | ||
279 | jmp +37 | ||
280 | acc +5 | ||
281 | nop -125 | ||
282 | nop -126 | ||
283 | acc -18 | ||
284 | jmp +186 | ||
285 | jmp -87 | ||
286 | jmp -262 | ||
287 | nop -20 | ||
288 | jmp -108 | ||
289 | acc +26 | ||
290 | acc +20 | ||
291 | jmp +193 | ||
292 | nop +185 | ||
293 | jmp +129 | ||
294 | acc +26 | ||
295 | jmp +122 | ||
296 | acc -8 | ||
297 | nop +143 | ||
298 | nop +166 | ||
299 | jmp -236 | ||
300 | acc +33 | ||
301 | jmp -139 | ||
302 | acc +38 | ||
303 | jmp +1 | ||
304 | acc +21 | ||
305 | acc +31 | ||
306 | jmp -79 | ||
307 | acc -13 | ||
308 | jmp -78 | ||
309 | acc +29 | ||
310 | jmp +160 | ||
311 | acc +48 | ||
312 | acc -8 | ||
313 | acc +28 | ||
314 | acc +15 | ||
315 | jmp -284 | ||
316 | jmp +25 | ||
317 | acc +24 | ||
318 | jmp +1 | ||
319 | jmp -92 | ||
320 | acc +22 | ||
321 | jmp +169 | ||
322 | acc -15 | ||
323 | acc +16 | ||
324 | acc +4 | ||
325 | jmp -85 | ||
326 | acc -18 | ||
327 | acc -19 | ||
328 | acc -2 | ||
329 | jmp -278 | ||
330 | acc +48 | ||
331 | jmp -195 | ||
332 | jmp -40 | ||
333 | jmp -110 | ||
334 | acc +47 | ||
335 | jmp -26 | ||
336 | acc +26 | ||
337 | nop -187 | ||
338 | acc +40 | ||
339 | acc +42 | ||
340 | jmp +167 | ||
341 | acc +50 | ||
342 | acc +36 | ||
343 | acc -14 | ||
344 | jmp -313 | ||
345 | nop -203 | ||
346 | jmp +227 | ||
347 | acc -15 | ||
348 | acc +22 | ||
349 | jmp -23 | ||
350 | acc +6 | ||
351 | acc +30 | ||
352 | acc +12 | ||
353 | jmp +69 | ||
354 | nop -212 | ||
355 | nop -105 | ||
356 | acc +12 | ||
357 | jmp -155 | ||
358 | nop +69 | ||
359 | acc -16 | ||
360 | jmp -68 | ||
361 | acc -18 | ||
362 | acc +35 | ||
363 | acc +34 | ||
364 | acc +6 | ||
365 | jmp -80 | ||
366 | acc +7 | ||
367 | acc +19 | ||
368 | acc -8 | ||
369 | jmp -94 | ||
370 | acc +12 | ||
371 | nop -148 | ||
372 | acc +33 | ||
373 | nop -41 | ||
374 | jmp -107 | ||
375 | acc +25 | ||
376 | acc +9 | ||
377 | nop +107 | ||
378 | jmp -44 | ||
379 | jmp +1 | ||
380 | jmp -254 | ||
381 | acc +10 | ||
382 | acc +0 | ||
383 | acc +37 | ||
384 | acc +33 | ||
385 | jmp +137 | ||
386 | nop +136 | ||
387 | jmp -225 | ||
388 | acc -4 | ||
389 | acc -17 | ||
390 | acc +39 | ||
391 | jmp -286 | ||
392 | jmp +150 | ||
393 | nop -380 | ||
394 | acc +34 | ||
395 | acc +16 | ||
396 | nop +146 | ||
397 | jmp +105 | ||
398 | jmp +119 | ||
399 | jmp -190 | ||
400 | acc +0 | ||
401 | nop +205 | ||
402 | nop -302 | ||
403 | jmp -17 | ||
404 | acc -4 | ||
405 | jmp -7 | ||
406 | jmp -14 | ||
407 | jmp -394 | ||
408 | acc +34 | ||
409 | acc -1 | ||
410 | acc +37 | ||
411 | acc -17 | ||
412 | jmp -312 | ||
413 | nop -180 | ||
414 | nop -139 | ||
415 | acc +21 | ||
416 | jmp -378 | ||
417 | acc +24 | ||
418 | acc +38 | ||
419 | jmp +129 | ||
420 | acc +26 | ||
421 | jmp +19 | ||
422 | acc +31 | ||
423 | jmp -190 | ||
424 | acc +29 | ||
425 | acc -5 | ||
426 | jmp +14 | ||
427 | nop +186 | ||
428 | acc +12 | ||
429 | acc +9 | ||
430 | acc -16 | ||
431 | jmp +9 | ||
432 | acc +2 | ||
433 | nop -382 | ||
434 | nop -284 | ||
435 | nop -377 | ||
436 | jmp -169 | ||
437 | jmp +129 | ||
438 | acc +49 | ||
439 | jmp -297 | ||
440 | acc +48 | ||
441 | acc +18 | ||
442 | acc +8 | ||
443 | jmp +170 | ||
444 | acc +12 | ||
445 | acc -4 | ||
446 | acc +28 | ||
447 | jmp -20 | ||
448 | acc -11 | ||
449 | jmp -363 | ||
450 | jmp +1 | ||
451 | acc +9 | ||
452 | acc +31 | ||
453 | jmp +31 | ||
454 | acc +36 | ||
455 | acc +42 | ||
456 | acc +2 | ||
457 | nop -131 | ||
458 | jmp -322 | ||
459 | acc +35 | ||
460 | acc +44 | ||
461 | acc +11 | ||
462 | acc +14 | ||
463 | jmp -213 | ||
464 | acc -16 | ||
465 | acc -15 | ||
466 | acc -5 | ||
467 | jmp -277 | ||
468 | acc -17 | ||
469 | jmp -252 | ||
470 | acc -19 | ||
471 | acc +31 | ||
472 | acc -16 | ||
473 | acc -5 | ||
474 | jmp +48 | ||
475 | jmp +1 | ||
476 | jmp -97 | ||
477 | acc +5 | ||
478 | jmp -382 | ||
479 | acc +26 | ||
480 | acc +41 | ||
481 | acc +31 | ||
482 | acc -2 | ||
483 | jmp -392 | ||
484 | acc +41 | ||
485 | jmp -124 | ||
486 | acc +45 | ||
487 | acc +24 | ||
488 | acc +10 | ||
489 | jmp -339 | ||
490 | acc +29 | ||
491 | acc -10 | ||
492 | acc -10 | ||
493 | acc +3 | ||
494 | jmp -456 | ||
495 | jmp -25 | ||
496 | acc +37 | ||
497 | acc +39 | ||
498 | acc -11 | ||
499 | acc -1 | ||
500 | jmp +106 | ||
501 | jmp -328 | ||
502 | jmp -489 | ||
503 | nop -111 | ||
504 | nop -458 | ||
505 | acc +31 | ||
506 | jmp -100 | ||
507 | acc -18 | ||
508 | jmp -258 | ||
509 | acc -17 | ||
510 | nop -46 | ||
511 | acc +43 | ||
512 | acc +45 | ||
513 | jmp -127 | ||
514 | jmp +34 | ||
515 | acc +33 | ||
516 | jmp -200 | ||
517 | nop -90 | ||
518 | acc +20 | ||
519 | jmp -271 | ||
520 | acc +41 | ||
521 | jmp -189 | ||
522 | acc -16 | ||
523 | nop -321 | ||
524 | acc +25 | ||
525 | acc -12 | ||
526 | jmp -62 | ||
527 | acc -1 | ||
528 | jmp +1 | ||
529 | acc +35 | ||
530 | acc +39 | ||
531 | jmp -184 | ||
532 | jmp -236 | ||
533 | nop -331 | ||
534 | acc +12 | ||
535 | jmp +78 | ||
536 | acc +15 | ||
537 | jmp -30 | ||
538 | acc -11 | ||
539 | jmp -117 | ||
540 | jmp -8 | ||
541 | jmp +9 | ||
542 | acc +2 | ||
543 | jmp -497 | ||
544 | acc +10 | ||
545 | acc +0 | ||
546 | acc -6 | ||
547 | jmp -155 | ||
548 | jmp -148 | ||
549 | jmp -95 | ||
550 | jmp -96 | ||
551 | jmp -249 | ||
552 | nop -277 | ||
553 | nop -411 | ||
554 | acc -13 | ||
555 | acc -2 | ||
556 | jmp -383 | ||
557 | acc -13 | ||
558 | jmp -110 | ||
559 | acc +13 | ||
560 | acc +16 | ||
561 | acc +5 | ||
562 | acc -2 | ||
563 | jmp -67 | ||
564 | acc +37 | ||
565 | jmp -491 | ||
566 | acc +35 | ||
567 | acc +34 | ||
568 | acc +32 | ||
569 | acc -2 | ||
570 | jmp -546 | ||
571 | acc -19 | ||
572 | jmp -322 | ||
573 | acc +48 | ||
574 | acc +18 | ||
575 | acc +35 | ||
576 | acc +8 | ||
577 | jmp -448 | ||
578 | acc +41 | ||
579 | acc -15 | ||
580 | acc +34 | ||
581 | acc +46 | ||
582 | jmp -50 | ||
583 | acc +12 | ||
584 | nop -184 | ||
585 | acc +14 | ||
586 | acc +38 | ||
587 | jmp -370 | ||
588 | jmp +10 | ||
589 | acc -14 | ||
590 | acc -16 | ||
591 | nop -259 | ||
592 | nop -300 | ||
593 | jmp -400 | ||
594 | acc +38 | ||
595 | acc +29 | ||
596 | acc +27 | ||
597 | jmp -175 | ||
598 | jmp -456 | ||
599 | acc +30 | ||
600 | jmp -308 | ||
601 | jmp -538 | ||
602 | jmp +1 | ||
603 | acc -16 | ||
604 | nop -127 | ||
605 | jmp -407 | ||
606 | acc +5 | ||
607 | jmp -57 | ||
608 | acc +21 | ||
609 | acc +3 | ||
610 | acc +42 | ||
611 | acc +43 | ||
612 | jmp -521 | ||
613 | acc -9 | ||
614 | acc +20 | ||
615 | nop -217 | ||
616 | jmp -15 | ||
617 | acc +37 | ||
618 | acc -12 | ||
619 | acc -18 | ||
620 | jmp +1 | ||
621 | jmp -465 | ||
622 | acc +37 | ||
623 | nop -577 | ||
624 | acc +8 | ||
625 | acc +43 | ||
626 | jmp +1 | ||