aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--aoc.cabal6
-rw-r--r--default.nix3
-rw-r--r--execs/Day08.hs4
-rw-r--r--execs/Day09.hs35
-rw-r--r--input/091000
-rw-r--r--input/09sample20
-rw-r--r--lib/Utils.hs5
7 files changed, 1070 insertions, 3 deletions
diff --git a/aoc.cabal b/aoc.cabal
index 01cebc9..0957baa 100644
--- a/aoc.cabal
+++ b/aoc.cabal
@@ -69,3 +69,9 @@ executable Day08
69 build-depends: base, aoc, containers 69 build-depends: base, aoc, containers
70 default-language: Haskell2010 70 default-language: Haskell2010
71 hs-source-dirs: execs 71 hs-source-dirs: execs
72
73executable Day09
74 main-is: Day09.hs
75 build-depends: base, aoc, containers, parsec
76 default-language: Haskell2010
77 hs-source-dirs: execs
diff --git a/default.nix b/default.nix
index 64861a5..78e9f1f 100644
--- a/default.nix
+++ b/default.nix
@@ -3,8 +3,9 @@ mkDerivation {
3 pname = "aoc"; 3 pname = "aoc";
4 version = "0.1.0.0"; 4 version = "0.1.0.0";
5 src = ./.; 5 src = ./.;
6 isLibrary = false; 6 isLibrary = true;
7 isExecutable = true; 7 isExecutable = true;
8 libraryHaskellDepends = [ base ];
8 executableHaskellDepends = [ base containers parsec ]; 9 executableHaskellDepends = [ base containers parsec ];
9 license = "unknown"; 10 license = "unknown";
10 hydraPlatforms = stdenv.lib.platforms.none; 11 hydraPlatforms = stdenv.lib.platforms.none;
diff --git a/execs/Day08.hs b/execs/Day08.hs
index 7d3fce8..6546990 100644
--- a/execs/Day08.hs
+++ b/execs/Day08.hs
@@ -40,6 +40,6 @@ genAll (acc:rest) = map (acc:) $ genAll rest
40main :: IO () 40main :: IO ()
41main = do 41main = do
42 n <- map (parseLine . words) . lines <$> readFile "input/08" 42 n <- map (parseLine . words) . lines <$> readFile "input/08"
43 let solve1 = run 0 0 Set.empty . Map.fromList . zip [0..] 43 let solve1 = run 0 0 mempty . Map.fromList . zip [0..]
44 print $ solve1 n 44 print $ solve1 n
45 print $ solve1 $ head $ filter (doesEnd 0 0 Set.empty . Map.fromList . zip [0..]) $ genAll n 45 print $ solve1 $ head $ filter (doesEnd 0 0 mempty . Map.fromList . zip [0..]) $ genAll n
diff --git a/execs/Day09.hs b/execs/Day09.hs
new file mode 100644
index 0000000..e2e5fd4
--- /dev/null
+++ b/execs/Day09.hs
@@ -0,0 +1,35 @@
1
2module Main where
3
4import Utils
5import Data.List (inits, tails, find)
6import Data.Bifunctor
7
8parseLine :: String -> Int
9parseLine = read
10
11doCheck :: [Int] -> Int -> Bool
12doCheck preamble target = target `elem` p
13 where p = [x + y | x <- preamble, y <- preamble, x /= y]
14
15checkAll :: [Int] -> [Int] -> [(Int, Bool)]
16checkAll preamble [x] = [(x, doCheck preamble x)]
17checkAll preamble@(p:ps) (x:xs) = (x, doCheck preamble x) : checkAll (ps++[x]) xs
18
19
20findWeakness :: [[Int]] -> Int -> Int
21findWeakness subs target = mn + mx
22 where Just t = find ((== target) . sum) subs
23 mn = minimum t
24 mx = maximum t
25
26main :: IO ()
27main = do
28 n <- map parseLine . lines <$> readFile "input/09"
29 let preambleLen = 25
30 Just (target, _) = find (not . snd)
31 $ uncurry checkAll
32 $ bimap (take preambleLen) (drop preambleLen)
33 $ (,) n n
34 print target
35 print $ findWeakness (sublists n) target
diff --git a/input/09 b/input/09
new file mode 100644
index 0000000..e8a2fe7
--- /dev/null
+++ b/input/09
@@ -0,0 +1,1000 @@
129
244
311
45
542
68
717
823
918
1016
1150
1247
1321
143
1525
1628
1736
1834
1948
2030
2132
2240
2322
2419
2541
2613
2756
2814
2911
3020
3139
3243
3317
3464
3557
3666
3731
3816
39106
4027
4179
4225
4336
4424
4546
4638
4728
4833
4992
5029
5130
5234
5389
5435
5553
5683
5742
5840
59137
6041
6150
6243
6368
6449
6569
6652
6779
6860
6954
7062
71165
7257
7387
74102
7595
76110
77114
7875
7976
80239
8181
8294
83176
8484
85163
8692
87119
88173
89143
90167
91106
92111
93116
94197
95132
96133
97194
98151
99156
100157
101159
102160
103313
104165
105175
106249
107178
108240
109176
110269
111198
112217
113286
114310
115227
116424
117289
118273
119434
120265
121393
122332
123415
124337
125444
126530
127325
128340
129341
130351
131354
132451
133374
134403
135492
136649
137922
138500
139537
140516
141538
142554
143590
144639
145597
146825
147867
148662
149665
150666
151841
152676
1531113
154715
155705
156957
157777
1581242
1591134
1601016
1611037
1621038
1631328
1641054
1651155
1661092
1671144
1681187
1691236
1701312
1711327
1721799
1731623
1741331
1751342
1761381
1772518
1781420
1791482
1801662
1812049
1821814
1832419
1842146
1852404
1863382
1872342
1882209
1892247
1903141
1912236
1923296
1932563
1942567
1952654
1964727
1972673
1982712
1992863
2002723
2012801
2024050
2033469
2043144
2056270
2065287
2073960
2084456
2094355
2104445
2114483
2124551
2135236
2147099
2155099
2165217
2179278
2186142
2198568
2205327
2215385
2225396
2238540
2245524
2255867
2265945
2278996
2289196
2297104
2308315
23112316
2328405
2339681
2348800
2358928
2369034
23711341
23810316
23912203
24010909
24110544
24210712
24310723
24410781
24510851
24610920
24711263
24814979
24911391
25011812
25118609
25218445
25315419
25417827
25517205
25617333
25717439
25817962
25917834
26019244
26119350
26220860
26321028
26431447
26529956
26635796
26721504
26830270
26922732
27023203
27122183
27235273
27347289
27426810
27527231
27632858
27740566
27853718
27941976
28034538
28137206
28235401
28338694
28453888
28547838
28640854
28741888
28853159
28954041
29078108
29182830
29243687
29344915
29457741
29558604
29648993
29759668
29860089
29961348
30086576
30183531
30269939
30371744
30473232
30572607
30674095
30798362
30891853
30982742
310127430
31184541
31285575
31393908
314103519
31588602
31692680
317101428
318106263
319106734
320107597
321108661
322121016
323135443
324134580
325175178
326141683
327171344
328144351
329145839
330181104
331158636
332193172
333250489
334167283
335173143
336170116
337174177
338245779
339313122
340200277
341194108
342207691
343311799
344214331
345216258
346295193
347255596
348356014
349278931
350286034
351287522
352374974
353290190
354304475
355325919
356328752
357340426
358361391
359341460
360343259
361462037
362374454
363401799
364394385
365407968
366408439
367422022
368430589
369501853
370506448
371534527
372628982
373700893
374564965
375683685
376695961
377594665
3781223647
379776253
380836491
381669178
382681886
383772049
3841351064
385717713
386824974
387768839
388989050
389802353
390816407
391830461
392852611
393932442
3941036380
3951571192
3961248650
3971295558
3981379646
3991159630
4001263843
4011276551
4021312378
4031386891
4041399599
4052560242
4062097911
4071450725
4081540888
4091520066
4101542687
4111593813
4121585246
4132763103
4142435979
4151646868
4161683072
4171785053
4181968822
4192821433
4202436181
4212408280
4222423473
4232539276
4242472008
4252995450
4262711977
4272929578
4283691724
4292850324
4302970791
4312991613
4325257614
4333060954
4343276885
4354444137
4363179059
4373232114
4383468125
4396229190
4405183985
4415655587
4423753875
4434908189
4446007401
4454831753
4464880288
4479920914
4485011284
4495322332
4505562301
45110563776
4525779902
4535821115
4545841937
45514752667
4566932934
4579843037
4586240013
4596745010
4608585628
4618937860
4629076207
4637222000
4648634163
4659316176
4669739942
46710442589
46810470490
4699891572
4709712041
47112774871
47210333616
47310573585
47416824506
47511342203
47614427565
47716682602
47812061128
47913063937
48015330638
48112985023
48213462013
48313967010
48415379173
48517664589
48615856163
48716538176
48816934041
48925747735
49020465157
49119451983
49221773169
49319603613
49420045657
49520285626
49620907201
49738597675
49821915788
49925046151
50023403331
50125125065
50225523141
50326028138
50428364196
50542331206
50626447036
50727429023
50835424830
50939649270
51032394339
51136141789
51233472217
51336386024
51447816179
51542855314
51658057577
51739889239
51851970177
51982504584
52041192827
52148336224
52259500355
52347040853
52448926472
52548528396
52654392334
52753876059
52879241338
52976275263
530134332840
53176617657
53262853853
53365866556
53473587166
53568780363
53683182642
53793163004
538111933636
53987705418
54081082066
54186930092
54291859416
54388233680
544122513638
545135274533
546162423980
547207920006
548111382249
549102802531
550131711038
551108268393
552116729912
553246266476
554142367529
555185367491
556131634216
557134646919
558370343986
559149862429
560195965535
561168012158
562204963592
563144381670
564258130822
565169315746
566175163772
567180093096
568284742070
569279394407
570211070924
571214184780
572219532443
573224998305
574234436747
575239902609
576434604499
577248364128
578519178817
579519297016
580281496645
581266281135
582279028589
583294244099
584344479518
585312393828
586380127364
587563770659
588363914113
589519643290
590388848189
591399625539
592453969190
593425255704
594430603367
595433717223
596908491479
597444530748
598459435052
599678967495
600683092288
601591422417
602733327707
6031252970997
604712745812
6051472262138
606545309724
607573272688
608701242017
609708393631
6101499913896
611833342762
612752762302
613844156287
614788473728
6151556902099
616853594729
617855859071
618893152275
619864320590
6201752647766
6211004744776
622903965800
6231032707740
6241118582412
6251501219540
6261424765179
6271745453552
6281326034990
6291246551741
6301253703355
6311274514705
6321454004319
6331534584779
6341461155933
6351541236030
6361596918589
6371644332799
6381632630015
6392445201830
6401709453800
6411717915319
6422995240349
6432871230178
6442151290152
6451908710576
6461936673540
6472178480505
6482707707674
6492365134153
6502500255096
6512600549695
6522521066446
6534672356598
6542528218060
6552714859288
6564405814957
6574424313088
6584304626263
6594074587953
6604244882494
6615100804791
6623896395824
6633342083815
6643427369119
6653618164376
6663845384116
6677692752329
6684060000728
6694087191081
6704115154045
6714886200599
6724543614658
6735079993441
6744865389249
6756424613884
6768568752422
6775049284506
6788269697204
6795243077348
6806142228407
68113669557213
6827501957072
6836769452934
6846960248191
6858360036539
68618749550654
6877272753235
68811779943410
6897045533495
6907463548492
6918388998774
69216081751103
69324441787642
6948202345126
6958658768703
6969409003907
69720625365761
69812911681341
69910292361854
70012012530282
70114652081255
70219811785620
70311385305755
70412203325539
70513644185479
70614509081987
70718658058990
70814318286730
70914005781686
71014736301727
71115247878621
71221114026467
71315665893618
71433100556345
71515852547266
71616591343900
71727962472209
71816861113829
71922304892136
72018067772610
72119701365761
72223588631294
72328159559962
72425391087441
72523397836037
72636779920085
72725029491234
72825894387742
72926939627266
73027649967165
73128324068416
73228742083413
73329054588457
73444511080994
73537552770757
73650628960552
73746391841026
73832713661095
73932443891166
74045020673791
74145717739775
74240258949866
74334928886439
74458326722476
74537769138371
74643099201798
74748427327271
74848788923478
74953353559650
75049292223779
75177481915728
75250923878976
75361037729511
75454589594431
75556392050578
75657066151829
75771841285211
75870213029537
75970266431852
76084160979397
76165157552261
76267372777605
76367642547534
76472698024810
76575187836305
766104277438626
767115392874305
768105141915976
76987061362150
77086196465642
77191526529069
77297216250749
773103881818210
774100216102755
775150179940538
776111655746260
777105513473407
778204769534969
779126658482430
780113458202407
781122223704090
782142107717063
783156409495179
784137855577071
785132530329866
786179298293794
787158899306674
788154703909684
789147885861115
790190473904268
791197040002476
792206919403374
793173257827792
794177722994711
795183412716391
796235681906497
797217340020617
798350980822503
799217169219667
800247621190470
801328824965927
802227737177497
803301007023737
804240116684837
805361135711102
806380151520336
807292559486755
808287234239550
809328329481339
810280416190981
811327184154909
812302589770799
813395063015328
814321143688907
815356670544183
816475798591334
817390332119765
818390427047459
819394892214378
820573744836156
821434509240284
822444906397164
823504403459217
824530326948296
825581423214718
826620888968094
827716206704235
828520532875818
829527350924387
830755652929191
831567650430531
832682126453928
8331151215916390
8341057221806052
835675479206309
8361043390859144
837659260314982
8381095001354918
839677814233090
8401065811326074
841835238516929
8421208141181386
8431077018668306
844829401454662
845879415637448
846938912699501
8471551445221164
8481752497874615
8491047883800205
8501088183306349
8511141421843912
8521635068566639
8531179793190800
8541341386768910
8551488661769644
8561226910745513
8571747443621331
8581334739521291
8591337074548072
8602482808612822
8611494498831911
8621743625559164
8631507215687752
8641513052750019
8651923421823278
8662015931367807
8671708817092110
8681768314154163
8691818328336949
8701927299437653
8712273652220792
8722136067106554
8733650999934446
8742189305644117
8752429570075259
8762476161365203
8772976455335549
8782568297514423
8792561650266804
8803695613591816
8814991220342063
8822671814069363
8832847792271310
8842831573379983
8853001714519663
8863007551581930
8873216032779862
8885808028715532
8895233464336167
8903850721260931
8913477131246273
8923586642491112
8934091980557741
8944775091708963
8954063366544207
8964325372750671
8974565637181813
8985147975434566
8994618875719376
9009473348185237
9015323953636513
9025129947781227
9035416089785733
9045679365651293
9058314489311192
9065839124961913
9075503387449346
9087977740052537
9095833287899646
9106693164026135
9118625106680839
9126802675270974
9139766851153942
9147063773737385
9157327852507204
9167540497790480
9177650009035319
91815642341818396
9199942829355889
92012688473225046
92114868350297684
92210034965505109
9239748823500603
92410122263168722
92510453901417740
92612017117662648
92711182753100639
92811512653550939
92911342512411259
93011336675348992
93112526451925781
93212897061637031
93317726563553140
93418877173139472
93513495839297109
93613866449008359
93714130527778178
93814977861542523
93917362818012313
94015190506825799
94123809278364248
94217398832535922
94332917070378939
94419691652856492
94545605543603985
94621790576766732
94724409715187970
94819871086669325
94920576164586462
95023353793011640
95122519428449631
95231207762018317
95332218104782273
95422679187760251
95526763510645390
95633473226223493
95741080356564780
95828473700839632
95927362288305468
96027626367075287
96127996976786537
96263599785014411
96342390515118956
96460215081568810
96532589339361721
96637090485392414
96737269919205247
96839562739525817
96942366741353194
97040447251255787
97150264277606364
97261099593298780
97358981615427663
97454125798950858
97545873221461271
97661063040201353
97749442698405641
97861946927063125
97950041476065719
98054988655380755
981104431353786396
98264632207510715
983105079458766502
98455359265092005
98555623343861824
98667559716312354
98769859258566968
98872152078887538
98989889949661428
99069679824754135
991144878605042183
99295315919866912
99380009990781604
99482813992608981
995152162069669142
996101496565323095
997104854836888934
99895914697526990
99999484174471360
1000100861876842026
diff --git a/input/09sample b/input/09sample
new file mode 100644
index 0000000..28d66e4
--- /dev/null
+++ b/input/09sample
@@ -0,0 +1,20 @@
135
220
315
425
547
640
762
855
965
1095
11102
12117
13150
14182
15127
16219
17299
18277
19309
20576
diff --git a/lib/Utils.hs b/lib/Utils.hs
index 4319714..89b1bd3 100644
--- a/lib/Utils.hs
+++ b/lib/Utils.hs
@@ -5,11 +5,13 @@ module Utils ( binaryToInt
5 , bet 5 , bet
6 , (&+) 6 , (&+)
7 , howMany 7 , howMany
8 , sublists
8 ) where 9 ) where
9 10
10import Data.Char (digitToInt) 11import Data.Char (digitToInt)
11import Control.Monad 12import Control.Monad
12import Data.Either 13import Data.Either
14import Data.List (inits, tails)
13 15
14 16
15binaryToInt :: String -> Int 17binaryToInt :: String -> Int
@@ -34,3 +36,6 @@ bet k (l, u) = k >= l && k <= u
34 36
35howMany :: (a -> Bool) -> [a] -> Int 37howMany :: (a -> Bool) -> [a] -> Int
36howMany predicate = length . filter predicate 38howMany predicate = length . filter predicate
39
40sublists :: [a] -> [[a]]
41sublists = concatMap inits . tails