-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadv16.hs
1610 lines (1317 loc) · 48.8 KB
/
adv16.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env runhaskell
import Data.Bits
import Data.Char
import Data.List
import Data.Array
import Data.Maybe
import Debug.Trace
import Text.Printf
import Numeric
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.Sequence as Seq
import qualified Data.ByteString.Lazy as BS
import Data.Digest.Pure.MD5
import System.Environment (getArgs)
import Test.HUnit
import Data.Foldable
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
main =
getArgs >>= getInput >>= return . solve161_1 >>= print
where
getInput (l : _) = readFile l
getInput [] = getContents
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
-- readFile "inputs/1501.in" >>= return . solve151_1
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2016 DAY 25
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
newval2 vs ("cpy": x1: x2: [])
| isInteger x1 = M.insert x2 (read x1) vs
| isJust $ M.lookup x1 vs = M.insert x2 (fromJust $ M.lookup x1 vs) vs
| otherwise = vs
newval2 vs ("inc": x1: [])
| isJust $ M.lookup x1 vs = M.adjust (+1) x1 vs
| otherwise = vs
newval2 vs ("dec": x1: [])
| isJust $ M.lookup x1 vs = M.adjust (-1+) x1 vs
| otherwise = vs
newval2 vs ("mul": x1: x2: x3: []) =
M.insert x1 (v2 * v3) vs
where
v2 = M.findWithDefault 0 x2 vs
v3 = M.findWithDefault 0 x3 vs
newval2 vs ("jnz": x1: x2: []) = vs
newval2 vs ("nop": []) = vs
newval2 vs ("out": x1: []) = nvs
where
nvs = M.insert "o" new vs
old = M.findWithDefault 0 "o" vs
val = M.findWithDefault 0 x1 vs
new = old * 2 + val
newval2 vs x = error (show x)
newidx2 vs ("jnz": x1: x2: []) i
| isInteger x1 = res (read x1)
| otherwise = res (M.findWithDefault 0 x1 vs)
where
v2 = if (isInteger x2) then (read x2) else (M.findWithDefault 0 x2 vs)
res x = if x == 0 then (i + 1) else (i + v2)
newidx2 _ _ i = i + 1
newlst2 ls vs i ("tgl": x1: []) = nwl
where
off = M.findWithDefault 0 x1 vs
nwl = toggle ls (off + i)
toggle ls idx
| idx >= length ls = ls
| otherwise =
(take (idx) ls) ++ [toggli ((!!) ls idx)] ++ (drop (idx + 1) ls)
toggli ("inc": xs) = ("dec": xs)
toggli ("dec": xs) = ("inc": xs)
toggli ("cpy": xs) = ("jnz": xs)
toggli ("jnz": xs) = ("cpy": xs)
toggli ("tgl": xs) = ("inc": xs)
newlst2 ls _ _ _ = ls
-- 0b0101010101010101010101010101010101 5726623061
-- answer part one: 198
solve1625_1 s =
loop 0 insn
where
insn = map ( words ) $ lines s
loop x insn
| sigok == True = x
| otherwise = trace (show (x)) loop (x+1) insn
where sigok = (\l -> process l (M.fromList [("a", x)]) 0) $ insn
process ls vs i
| length ls <= i = False
| M.findWithDefault 0 "o" vs == 5726623061 = True
| M.findWithDefault 0 "o" vs > 5726623061 = False
| otherwise = process (newlst2 ls vs i ins) (newval2 vs ins) (newidx2 vs ins i)
where ins = (!!) ls i
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2016 DAY 24
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
combinations m xs =
combsBySize xs !! m
where
combsBySize = foldr f ([[]] : repeat [])
f x next = zipWith (++) (map (map (x:)) ([]:next)) next
possibleMoves grid (x, y) =
filter isPossible [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
where isPossible (x, y) = (/= '#') $ (grid !! y !! x)
visitG grid target cache (((x, y), l):vlist)
| (== target) $ grid !! y !! x = length l
| elem (x, y) cache = visitG grid target cache vlist
| otherwise =
visitG grid target ((x, y):cache)
(vlist ++ ([((nx, ny), ((x, y):l)) | (nx, ny) <- (possibleMoves grid (x, y))]))
-- answer part one: 502
solve1624_1 s =
trace (concat . map (++"\n") $ grid)
minimum $ map dstp pths
where
grid = lines s
xmax = (-1+) $ length $ (!! 0) $ grid
ymax = (-1+) $ length $ grid
gcrd = [((x, y), grid !! y !! x) | x <- [0..xmax], y <- [0..ymax]]
posx = M.fromList $ map (\(x, y) -> (y, x)) $ filter (isDigit . snd) gcrd
cmbs = combinations 2 $ sort $ M.keys posx
dsts = foldl (\acc cmb -> M.insert cmb (dstx grid cmb) acc) M.empty cmbs
dstx grid (x1:x2:[]) = visitG grid x2 [] [(fromJust $ M.lookup x1 posx, [])]
pths = map ('0':) $ permutations $ sort $ filter (/= '0') $ M.keys posx --
dstp (x1:x2:xs) = sum (catMaybes [M.lookup (x1:x2:[]) dsts, M.lookup (x2:x1:[]) dsts]) + dstp (x2:xs)
dstp _ = 0
-- -------------------------------------------------------------------
-- answer part two: 724
solve1624_2 s =
trace (concat . map (++"\n") $ grid)
minimum $ map dstp pths
where
grid = lines s
xmax = (-1+) $ length $ (!! 0) $ grid
ymax = (-1+) $ length $ grid
gcrd = [((x, y), grid !! y !! x) | x <- [0..xmax], y <- [0..ymax]]
posx = M.fromList $ map (\(x, y) -> (y, x)) $ filter (isDigit . snd) gcrd
cmbs = combinations 2 $ sort $ M.keys posx
dsts = foldl (\acc cmb -> M.insert cmb (dstx grid cmb) acc) M.empty cmbs
dstx grid (x1:x2:[]) = visitG grid x2 [] [(fromJust $ M.lookup x1 posx, [])]
pths = map (\x -> '0':x++"0") $ permutations $ sort $ filter (/= '0') $ M.keys posx --
dstp (x1:x2:xs) = sum (catMaybes [M.lookup (x1:x2:[]) dsts, M.lookup (x2:x1:[]) dsts]) + dstp (x2:xs)
dstp _ = 0
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2016 DAY 23
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
isInteger s = case reads s :: [(Integer, String)] of
[(_, "")] -> True
_ -> False
newval vs ("cpy": x1: x2: [])
| isInteger x1 = M.insert x2 (read x1) vs
| isJust $ M.lookup x1 vs = M.insert x2 (fromJust $ M.lookup x1 vs) vs
| otherwise = vs
newval vs ("inc": x1: [])
| isJust $ M.lookup x1 vs = M.adjust (+1) x1 vs
| otherwise = vs
newval vs ("dec": x1: [])
| isJust $ M.lookup x1 vs = M.adjust (-1+) x1 vs
| otherwise = vs
newval vs ("mul": x1: x2: x3: []) =
M.insert x1 (v2 * v3) vs
where
v2 = M.findWithDefault 0 x2 vs
v3 = M.findWithDefault 0 x3 vs
newval vs ("nop": []) = vs
newval vs _ = vs
newidx vs ("jnz": x1: x2: []) i
| isInteger x1 = res (read x1)
| otherwise = res (M.findWithDefault 0 x1 vs)
where
v2 = if (isInteger x2) then (read x2) else (M.findWithDefault 0 x2 vs)
res x = if x == 0 then (i + 1) else (i + v2)
newidx _ _ i = i + 1
newlst ls vs i ("tgl": x1: []) =
nwl
where
off = M.findWithDefault 0 x1 vs
nwl = toggle ls (off + i)
toggle ls idx
| idx >= length ls = ls
| otherwise =
(take (idx) ls) ++ [toggli ((!!) ls idx)] ++ (drop (idx + 1) ls)
toggli ("inc": xs) = ("dec": xs)
toggli ("dec": xs) = ("inc": xs)
toggli ("cpy": xs) = ("jnz": xs)
toggli ("jnz": xs) = ("cpy": xs)
toggli ("tgl": xs) = ("inc": xs)
newlst ls _ _ _ = ls
processL ls vs i
| length ls <= i = vs
| otherwise = processL (newlst ls vs i ins) (newval vs ins) (newidx vs ins i)
where ins = (!!) ls i
-- answer part one: 13685
solve1623_1 =
(\l -> processL l (M.fromList [("a", 7)]) 0) . map ( words ) . lines
--(\l -> processL l M.empty 0) . map ( words ) . lines
-- -------------------------------------------------------------------
--TODO
-- answer part two: 479010245
solve1623_2 =
(\l -> processL l (M.fromList [("a", 12)]) 0) . map ( words ) . lines
-- -->> a! + 8645
-- 479010245
-- cpy b c a= 0 c=11 <- |
-- inc a a= 1 <- | |
-- dec c c=10 | | |
-- jnz c -2 -- | |
-- -> a = a + b
-- cpy a d d=12
-- cpy 0 a a= 0
-- -> a = a + b <-
-- dec d d=11 | |
-- jnz d -5 -- |
-- cpy 0 a
-- cpy b c
-- inc a
-- dec c
-- jnz c -2
-- dec d
-- jnz d -5
-- -> a = b * d
-- a=12
-- cpy a b b=12
-- dec b b=11
--
-- cpy a d d=12 <-
-- cpy 0 a a= 0 |
-- |
-- cpy b c a= 0 c=11 <- |
-- | |
-- inc a a= 1 <- | |
-- dec c c=10 | | |
-- jnz c -2 -- | |
-- a= 2 c= 9 | |
-- a= 3 c= 8 | |
-- a= 4 c= 7 | |
-- a= 5 c= 6 | |
-- a= 6 c= 5 | |
-- a= 7 c= 4 | |
-- a= 8 c= 3 | |
-- a= 9 c= 2 | |
-- a=10 c= 1 | |
-- a=11 c= 0 | |
-- | |
-- dec d d=11 | |
-- jnz d -5 -- |
-- a=132 c= 0 d= 0 |
-- |
-- dec b b=10 |
-- cpy b c c=10 |
-- cpy c d d=10 |
-- dec d d= 9 <- |
-- inc c c=11 | |
-- jnz d -2 -- |
-- a=132 b=10 c=20 d=0 |
-- |
-- tgl c |
-- cpy -16 c c=-16 |
-- jnz 1 c --
-- cpy 95 c c=95
-- jnz 91 d
-- inc a
-- inc d
-- jnz d -2
-- inc c
-- jnz c -5
-- until jnz 1 c
-- 1 -> 1
-- 2 -> 2 prec * 2
-- 3 -> 6 prec * 3
-- 4 -> 24 prec * 4
-- 5 -> 120 prec * 5
-- 6 -> 720 ...
-- 7 -> 5040
-- 8 -> 40320
-- 9 -> 362680
-- to end
-- 6 ->
-- 7 -> 13685 ( + 8645 )
-- 8 -> 48965 ( + 8645 )
-- 9 -> 371525 ( + 8645 )
-- -->> a! + 8645
-- 479010245
-- cpy a b
-- dec b
-- cpy a d
-- mul a b d
-- nop
-- nop
-- nop
-- nop
-- nop
-- nop
-- dec b
-- cpy b c
-- cpy c d
-- dec d
-- inc c
-- jnz d -2
-- tgl c
-- cpy -16 c
-- jnz 1 c
-- cpy 95 c
-- jnz 91 d
-- inc a
-- inc d
-- jnz d -2
-- inc c
-- jnz c -5
-- cpy 0 a
-- cpy b c
-- inc a
-- dec c
-- jnz c -2
-- dec d
-- jnz d -5
-- a = b * d + 6 nops
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2016 DAY 22
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
processIn (x1:x2:x3:x4:x5:[]) =
(coord $ drop 15 x1,
read (init x2) :: Int,
read (init x3) :: Int,
read (init x4) :: Int,
read (init x5) :: Int)
where
coord c =
(x, y)
where
(x0, y0) = span (/='-') c
x = read (drop 1 x0) :: Int
y = read (drop 2 y0) :: Int
fldUsed (c, s, u, a, p) = u
fldCrd (c, s, u, a, p) = c
fldAvail (c, s, u, a, p) = a
-- answer part one: 985
solve1622_1 s =
sum $ map (\a -> length (filter
(\b -> ((a /= b) && fldUsed(a) <= fldAvail(b)))
l)) $ filter ((/= 0) . fldUsed) l
where l = map (processIn . words) . drop 2 $ lines s
-- -------------------------------------------------------------------
printGrid s =
map printRow [0..ymax]
where
ch (x, y) =
if (fldUsed rc == 0) then '_'
else if (reg rc) then '.' else '#'
where rc = (!! 0) $ filter (\a -> (fldCrd a) == (x, y)) l
printRow y = [ ch (x, y) | x <- [0..xmax] ]
xmax = maximum $ map (fst . fldCrd) l
ymax = maximum $ map (snd . fldCrd) l
l = map (processIn . words) . drop 2 $ lines s
reg a = (/= 0) $ length $ filter
(\b -> ((a /= b) && fldUsed(a) <= fldAvail(b))) l
-- TODO
-- answer part two: 179
solve1622_2 = printGrid
-- 28
-- "X..............................Y"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "..........................######"
-- "................................"
-- "............................_..."
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "................................"
-- "X...............X....X....X..._Y"
-- "................................"
-- 5 -> 21
-- 10 -> 46 +25
-- 15 -> 71 +25
-- 20 -> 96 +25
-- 25 -> 121 +25
-- 30 -> 146 +25
-- 31 -> 151 +5
-- 151 + 28
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2016 DAY 21
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
processOp pwd ("swap":"position":x1:"with":"position":x2:[]) =
map (swap pwd (read x1) (read x2)) [0..length pwd - 1]
where
swap pwd p1 p2 x
| x == p1 = pwd !! p2
| x == p2 = pwd !! p1
| otherwise = pwd !! x
processOp pwd ("swap":"letter":x1:"with":"letter":x2:[]) =
swap pwd (x1 !! 0) (x2 !! 0)
where
swap [] l1 l2 = []
swap (x:xs) l1 l2
| x == l1 = (l2:swap xs l1 l2)
| x == l2 = (l1:swap xs l1 l2)
| otherwise = (x:swap xs l1 l2)
processOp pwd ("rotate":x1:x2:x3:[]) =
take ln $ drop offset $ cycle pwd
where
offset = if x1 == "right" then (mod (-nb) ln) else nb
nb = read x2 :: Int
ln = length pwd
processOp pwd ("rotate":"based":"on":"position":"of":"letter":x1:[]) =
take ln $ drop offset $ cycle pwd
where
idx = elemIndex (x1 !! 0) pwd
nbr = case idx of
Just x -> x
otherwise -> 0
offset = (mod (-(1 + if nbr >= 4 then nbr + 1 else nbr)) ln)
ln = length pwd
processOp pwd ("reverse":"positions":x1:"through":x2:[]) =
hd ++ rv ++ tl
where
p1 = read x1 :: Int
p2 = read x2 :: Int
hd = take p1 pwd
tl = drop (p2 + 1) pwd
rv = reverse $ take (p2 - p1 + 1) $ drop p1 pwd
processOp pwd ("move":"position":x1:"to":"position":x2:[]) =
l2
where
p1 = read x1 :: Int
p2 = read x2 :: Int
l1 = take p1 pwd ++ drop (p1 + 1) pwd
l2 = take p2 l1 ++ [pwd !! p1] ++ drop (p2) l1
scramble p s = foldl processOp p . map words $ lines s
-- answer part one: agcebfdh
solve1621_1 = scramble p
where p = "abcdefgh"
-- -------------------------------------------------------------------
-- answer part two: afhdbegc
solve1621_2 input =
filter (\x -> scramble x input == p) $ permutations p
where p = "fbgdceah"
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2016 DAY 20
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
procblackl line =
(start, end)
where
start = toInt $ takeWhile (/= '-') line
end = toInt $ tail $ dropWhile (/= '-') line
toInt x = read x :: Int
-- answer part one: 17348574
solve1620_1 =
find1 0 . sortOn fst . map procblackl . lines
where
find1 x ((l, h):xs)
| l > x = x
| otherwise = find1 (max x (h + 1)) xs
-- -------------------------------------------------------------------
-- answer part two: 104
solve1620_2 =
findN 0 0 . sortOn fst . map procblackl . lines
where
findN _ n [] = n
findN x n ((l, h):xs)
| l > x = findN (h + 1) (n + l - x) xs
| otherwise = findN (max x (h + 1)) n xs
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2016 DAY 19
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
kill2 seq =
(Seq.><) (Seq.drop 1 nsk) (Seq.take 1 nsk)
where
imd = 2
nsk = (Seq.><) (Seq.take (imd - 1) seq) (Seq.drop imd seq)
killloop fk seq
| Seq.length seq == 1 = seq
| otherwise = killloop fk $ fk seq
-- 3017957
-- answer part one: 1841611
solve1619_1 _ =
killloop kill2 $ Seq.fromList $ [1..3017957]
-- -------------------------------------------------------------------
killH seq =
(Seq.><) (Seq.drop 1 nsk) (Seq.take 1 nsk)
where
imd = (+ 1) $ (`div` 2) $ Seq.length seq
nsk = (Seq.><) (Seq.take (imd - 1) seq) (Seq.drop imd seq)
-- answer part two: 1423634
solve1619_2 _ =
killloop killH $ Seq.fromList $ [1..3017957]
-- https://en.wikipedia.org/wiki/Josephus_problem
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2016 DAY 18
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
nbSafe nb row =
snd $ nrs nb row
where
nrs nb row = foldl (\(x, y) _ -> let nx = nr1 x in (nx, y + cnt nx))
(row, cnt row) [1..nb-1]
cnt = length . filter (=='.')
nr1 r = nr0 ("." ++ r ++ ".")
nr0 (x1:x2:x3:xs) = (next x1 x2 x3 : nr0 (x2:x3:xs))
nr0 _ = []
--
next '^' '^' '.' = '^'
next '.' '^' '^' = '^'
next '^' '.' '.' = '^'
next '.' '.' '^' = '^'
next _ _ _ = '.'
-- phyroximetful6adimpt
-- answer part one: 1974
solve1618_1 _ =
nbSafe 40 "^.....^.^^^^^.^..^^.^.......^^..^^^..^^^^..^.^^.^.^....^^...^^.^^.^...^^.^^^^..^^.....^.^...^.^.^^.^"
-- -------------------------------------------------------------------
-- answer part two: 19991126
solve1618_2 _ =
nbSafe 400000 "^.....^.^^^^^.^..^^.^.......^^..^^^..^^^^..^.^^.^.^....^^...^^.^^.^...^^.^^^^..^^.....^.^...^.^.^^.^"
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2016 DAY 17
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
possMoves c x y =
map (move c (x, y)) $ filter (open c (x, y)) $ filter (poss c (x, y)) ['U', 'D', 'R', 'L']
where
move c (x, y) 'U' = (c ++ "U", x, y - 1)
move c (x, y) 'D' = (c ++ "D", x, y + 1)
move c (x, y) 'L' = (c ++ "L", x - 1, y)
move c (x, y) 'R' = (c ++ "R", x + 1, y)
poss c (x, y) 'U' = (y > 0)
poss c (x, y) 'D' = (y < 3)
poss c (x, y) 'L' = (x > 0)
poss c (x, y) 'R' = (x < 3)
open c (x, y) d
| d == 'U' = (`elem` "bcdef") $ (md5sum c) !! 0
| d == 'D' = (`elem` "bcdef") $ (md5sum c) !! 1
| d == 'L' = (`elem` "bcdef") $ (md5sum c) !! 2
| d == 'R' = (`elem` "bcdef") $ (md5sum c) !! 3
-- answer part one: RDDRLDRURD
solve1617_1 _ =
visit [("qzthpkfp", 0, 0)] -- RDDRLDRURD
where
visit ((c, x, y):cs)
| (x, y) == (3, 3) = dropWhile isLower c
| otherwise = visit (cs ++ (possMoves c x y))
-- -------------------------------------------------------------------
-- answer part two: 448
solve1617_2 _ =
maximum $ visit2 [("qzthpkfp", 0, 0)] -- 448
where
visit2 [] = []
visit2 ((c, x, y):cs)
| (x, y) == (3, 3) = ((length $ dropWhile isLower c) : visit2 cs)
| otherwise = (visit2 (cs ++ (possMoves c x y)))
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2016 DAY 16
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
dragc n x
| length p >= n = p
| otherwise = dragc n p
where
p = x ++ "0" ++ (flipc $ reverse x)
flipc [] = []
flipc ('0':xs) = ('1':flipc xs)
flipc ('1':xs) = ('0':flipc xs)
checks n x
| even $ length c = checks n c
| otherwise = c
where
c = pairc $ take n x
pairc (x1:x2:xs)
| x1 == x2 = ('1':pairc xs)
| otherwise = ('0':pairc xs)
pairc _ = []
chkfill n x = checks n $ dragc n x
-- solve0 = chkfill 20 "10000"
-- answer part one: 01110011101111011
solve1616_1 _ =
chkfill 272 "11110010111001001"
-- -------------------------------------------------------------------
-- answer part two: 11001111011000111
solve1616_2 _ =
chkfill 35651584 "11110010111001001"
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2016 DAY 15
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
-- Disc #1 has 13 positions; at time=0, it is at position 1.
-- Disc #2 has 19 positions; at time=0, it is at position 10.
-- Disc #3 has 3 positions; at time=0, it is at position 2.
-- Disc #4 has 7 positions; at time=0, it is at position 1.
-- Disc #5 has 5 positions; at time=0, it is at position 3.
-- Disc #6 has 17 positions; at time=0, it is at position 5.
-- Disc #7 has 11 positions; at time=0, it is at position 0.
d1 = (== 0) . (`mod` 13) . (+ 1). (+ 1)
d2 = (== 0) . (`mod` 19) . (+ 10). (+ 2)
d3 = (== 0) . (`mod` 3) . (+ 2). (+ 3)
d4 = (== 0) . (`mod` 7) . (+ 1). (+ 4)
d5 = (== 0) . (`mod` 5) . (+ 3). (+ 5)
d6 = (== 0) . (`mod` 17) . (+ 5). (+ 6)
d7 = (== 0) . (`mod` 11) . (+ 0). (+ 7)
fall1 t = d1 t && d2 t && d3 t && d4 t && d5 t && d6 t
-- answer part one: 376777
solve1615_1 _ =
take 1 $ filter fall1 $ [0..]
-- -------------------------------------------------------------------
fall2 t = d1 t && d2 t && d3 t && d4 t && d5 t && d6 t && d7 t
-- answer part two: 3903937
solve1615_2 _ =
take 1 $ filter fall2 $ [0..]
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2016 DAY 14
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
isValidK ml i =
case search3x (ml !! i) of
Just x -> any id $ map (search5c x) $ map (ml !!) [i + 1..i +
1001]
_ -> False
where
search3x (x1:x2:x3:xs)
| x1 == x2 && x2 == x3 = Just x1
| otherwise = search3x (x2:x3:xs)
search3x (_) = Nothing
search5c c s =
(>= 5) . maximum . (0:) . map length . filter ((== c) . (!! 0)) $ group s
search1st fct salt n i
| n == 0 = (i - 1)
| isValidK ml 0 = trace (show (n, i)) search1st fct salt (n - 1) (i + 1)
| otherwise = search1st fct salt n (i + 1)
where ml = [ fct $ salt ++ show x | x <- [i..] ]
-- solve0 = search1st md5sum "abc" 64 0
-- answer part one: 16106
solve1614_1 _ =
search1st md5sum "zpqevtbw" 64 0
-- -------------------------------------------------------------------
md5Stretch x =
foldl (\acc _ -> md5sum acc) x [1..2017]
search2nd fct salt nb =
search nb 0
where
search n i
| n == 0 = i - 1
| isValidK ml i = trace (show (n, i)) search (n - 1) (i + 1)
| otherwise = search n (i + 1)
ml = [ fct $ salt ++ show x | x <- [1..] ]
searchAll fct salt n =
take n $ filter (isValidK ml) [1..]
where ml = [ fct $ salt ++ show x | x <- [1..] ]
-- solve2 = search2nd md5Stretch "zpqevtbw" 64
solve1614_2 _ = searchAll md5Stretch "zpqevtbw" 64
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2016 DAY 13
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
isWall c (x, y) = ((mod) n1 2 /= 0)
where
ns = c + x * x + 3 * x + 2 * x * y + y + y * y
n1 = length $ filter (=='1') $ showIntAtBase 2 intToDigit ns ""
showAreaC c l =
concat $ map ((++ "\n") . showRow) [0..rsize]
where
showRow y = map (\x -> showCell x y) [0..rsize]
showCell x y
| isWall c (x, y) = '#'
| elem (x, y) l = 'O'
| otherwise = '.'
rsize = 40
possibleCMoves c x y =
filter isPossible [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
where
isPossible (x, y) = (x >= 0) && (y >= 0) && (not $ isWall c (x, y))
-- answer part one: 90
solve1613_1 _ =
visit1 1352 (31, 39) [] [(1, 1, [])]
where
visit1 code target cache ((x, y, l):vlist)
| trace (showAreaC code l) False = undefined
visit1 code target cache ((x, y, l):vlist)
| (x, y) == target = length l
| elem (x, y) cache = visit1 code target cache vlist
| otherwise =
visit1 code target ((x, y):cache)
(vlist ++ ([(nx, ny, ((x, y):l)) | (nx, ny) <- (possibleCMoves code x y)]))
-- -------------------------------------------------------------------
-- answer part two: 135
solve1613_2 _ =
visit2 1352 50 [] [(1, 1, 0)]
where
visit2 code nmax cache ((x, y, n):vlist)
| trace (showAreaC code cache) False = undefined
visit2 code nmax cache ((x, y, n):vlist)
| n > nmax = visit2 code nmax cache vlist
| elem (x, y) cache = visit2 code nmax cache vlist
| otherwise =
visit2 code nmax ((x, y):cache) (vlist ++ ([(nx, ny, n + 1) | (nx, ny) <- (possibleCMoves code x y)]))
visit2 code nmax cache [] = length cache
-- -------------------------------------------------------------------
-- solve0 = visit 10 (7, 4) [] [(1, 1, [])]
-- .##.##............###.....#####.#####....
-- #o######.##.####...#.##....#..#.#..#.##.#
-- .ooo##.#..#.##.#.#.######..#.##..#.####..
-- ##.ooo###......#..#..#..####.###..#..#.#.
-- .####o####..###.#.##.#.#...#..#.#.##.##..
-- .....oo######.###..###..##.#..###..##.#..
-- ..##.#oo##..#.......###.#..###......#.###
-- ##.#...oooo.##.##.#.....#....#.##.#.###..
-- .##.######o#.#.##...###.##...##.#..#..##.
-- #.##....##oo##.#.###..#.###.#.##.#..#..##
-- ##.#..#.###o#..#...#.##...#..#.#..#......
-- .######..#.o#.######.#####.#..###..##.###
-- ...#.....##o#.##..#...##.#.##.####..#.###
-- ##.#.##...#oooooo.#......#.....#.###...##
-- #..#.###.#######o########.##.#.###.##...#
-- #..#..##.#...###o##ooo#.##.#..#.....##...
-- ###.#.##.#.....#oooo#oo#.##.#.####.#.#.##
-- #.###...###.##.#.###.#o##.###...##..##.#.
-- #.....#..##.#..##..##.o.#.....#..##.#..#.
-- #..#####....#...###.#.o.##.#####.#..#..##
-- ##....#.##.###.#..#.##o#.###..#..##.#####
-- .###..####..##..#.#.##oo#ooooo#...#.#..##
-- ##.#....#.#...#..##..##ooo###o##.##..#..#
-- ...###..##.##..#.###.#.####.#o##.###.#...
-- ####.##..#######..#..#.....##ooooo#..####
-- #.....###..#......#..###.#.#.###.o#.....#
-- #.##.#..##.#..########.###.###.##o##..#.#
-- #.##..#...####....#.......#...#.#o#####.#
-- #.#.#...#....#..#.#.##..#.###..##o##...##
-- ..##.############.#.#####....#.#.oooo#.##
-- .#.#.#..##..#....##..#....##.#.#..##o#...
-- ..##.#....#.##.#.#.#.#.###.#..######o#.##
-- #.#..##.#..#.#.#.##..##..##.#.....#oo###.
-- ..#.#.##.#...#.##.#...#.#.######..#o#..##
-- ..#..#.#..###...#.##.##.##..##.#..#oooo.#
-- #..#..###.#.#.#.####.##..#.....##..##.o.#
-- ##.##.###..##..#...#.....#..######..##o##
-- .#.....#.#.#.#..##.#.##.#####..#.##.##o##
-- #.####.##..#..#.#..##.#.##..#..####.##o..
-- ##....#.#.###...#...##......###.o##oooo#.
-- .#..#.#.#.####.###.#.#####.#...#oooo##.#.
-- .##.##..oooooooooo###.....#####.#####....
-- #o######.##o####ooo#.##....#..#.#..#.##.#
-- oooo##.#..#o##.#o#o######..#.##..#.####..
-- ##oooo###..o...#oo#..#oo####.###..#..#.#.
-- o####o####..###.#o##.#o#...#..#.#.##.##..
-- ooooooo######o###oo###oo##.#..###..##.#..
-- oo##o#oo##oo#ooooooo###o#..###......#.###
-- ##.#oooooooo##o##o#ooooo#....#.##.#.###..
-- .##.######o#.#o##ooo###o##...##.#..#..##.
-- #.##....##oo##o#.###..#o###.#.##.#..#..##
-- ##.#..#.###o#oo#...#.##ooo#..#.#..#......
-- .######..#oo#o######.#####.#..###..##.###
-- ...#.....##o#o##oo#...##.#.##.####..#.###
-- ##.#.##...#ooooooo#......#.....#.###...##
-- #..#.###.#######o########.##.#.###.##...#
-- #..#..##.#...###o##ooo#.##.#..#.....##...
-- ###.#.##.#.....#oooo#oo#.##.#.####.#.#.##
-- #.###...###.##.#o###.#o##.###...##..##.#.
-- #.....#..##.#..##..##ooo#.....#..##.#..#.
-- #..#####....#...###.#ooo##.#####.#..#..##
-- ##....#.##.###.#..#.##o#.###..#..##.#####
-- .###..####..##..#.#.##oo#ooo..#...#.#..##
-- ##.#....#.#...#..##..##ooo###.##.##..#..#
-- ...###..##.##..#.###.#.####.#.##.###.#...
-- ####.##..#######..#..#.....##.....#..####
-- #.....###..#......#..###.#.#.###..#.....#
-- #.##.#..##.#..########.###.###.##.##..#.#
-- #.##..#...####....#.......#...#.#.#####.#
-- #.#.#...#....#..#.#.##..#.###..##.##...##
-- ..##.############.#.#####....#.#.....#.##
-- .#.#.#..##..#....##..#....##.#.#..##.#...
-- ..##.#....#.##.#.#.#.#.###.#..######.#.##
-- #.#..##.#..#.#.#.##..##..##.#.....#..###.
-- ..#.#.##.#...#.##.#...#.#.######..#.#..##