aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2020/14-Docking_Data/example4
-rw-r--r--2020/14-Docking_Data/first.hs72
-rw-r--r--2020/14-Docking_Data/input564
3 files changed, 640 insertions, 0 deletions
diff --git a/2020/14-Docking_Data/example b/2020/14-Docking_Data/example
new file mode 100644
index 0000000..e15150a
--- /dev/null
+++ b/2020/14-Docking_Data/example
@@ -0,0 +1,4 @@
+mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
+mem[8] = 11
+mem[7] = 101
+mem[8] = 0
diff --git a/2020/14-Docking_Data/first.hs b/2020/14-Docking_Data/first.hs
new file mode 100644
index 0000000..78f765e
--- /dev/null
+++ b/2020/14-Docking_Data/first.hs
@@ -0,0 +1,72 @@
+-- requires cabal install --lib megaparsec parser-combinators
+module Main (main) where
+import Control.Monad (void, when)
+import Data.Bits (clearBit, setBit)
+import Data.List (foldl')
+import Data.Map qualified as M
+import Data.Void (Void)
+import Text.Megaparsec
+import Text.Megaparsec.Char
+import System.Exit (die)
+
+exampleExpectedOutput = 165
+
+type Bits = [Maybe Bool]
+data Op = Mask Bits| Mem Int Int
+type Input = [Op]
+
+type Parser = Parsec Void String
+
+parseMask :: Parser Op
+parseMask = do
+ void $ string "mask = "
+ bits <- some $ (char '1' *> (return $ Just True)) <|> (char '0' *> (return $ Just False)) <|> (char 'X' *> (return Nothing))
+ return $ Mask bits
+
+parseMem :: Parser Op
+parseMem = do
+ void $ string "mem["
+ index <- some digitChar
+ void $ string "] = "
+ value <- some digitChar
+ return $ Mem (read index) (read value)
+
+parseOps :: Parser Input
+parseOps = do
+ ops <- some $ (parseMask <|> parseMem) <* (optional $ char '\n')
+ void $ eof
+ return $ ops
+
+parseInput :: String -> IO Input
+parseInput filename = do
+ input <- readFile filename
+ case runParser parseOps filename input of
+ Left bundle -> die $ errorBundlePretty bundle
+ Right ops -> return ops
+
+computeValue :: Int -> Bits -> Int
+computeValue value bits = fst $ foldl' evalBit (value, 35) bits
+ where
+ evalBit :: (Int, Int) -> Maybe Bool -> (Int, Int)
+ evalBit (value, index) (Just True) = (setBit value index, index-1)
+ evalBit (value, index) (Just False) = (clearBit value index, index-1)
+ evalBit (value, index) Nothing = (value, index-1)
+
+compute :: Input -> Int
+compute = extract . foldl' compute init
+ where
+ extract :: (M.Map Int Int, Bits) -> Int
+ extract = sum . M.elems . fst
+ compute :: (M.Map Int Int, Bits) -> Op -> (M.Map Int Int, Bits)
+ compute (acc, _) (Mask bits) = (acc, bits)
+ compute (acc, bits) (Mem index value) = (M.insert index (computeValue value bits) acc, bits)
+ init :: (M.Map Int Int, Bits)
+ init = (M.empty, [])
+
+main :: IO ()
+main = do
+ example <- parseInput "example"
+ let exampleOutput = compute example
+ when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
+ input <- parseInput "input"
+ print $ compute input
diff --git a/2020/14-Docking_Data/input b/2020/14-Docking_Data/input
new file mode 100644
index 0000000..70594e7
--- /dev/null
+++ b/2020/14-Docking_Data/input
@@ -0,0 +1,564 @@
+mask = 00000X110010111111X000100XX01010000X
+mem[20690] = 435
+mem[54036] = 231
+mem[27099] = 118644255
+mem[55683] = 22299263
+mem[26119] = 2279399
+mask = 00X000X0001X111111101X1111XX11X001XX
+mem[42072] = 1658073
+mem[63234] = 2277
+mask = 1001X010011011111110101101X0XX11X010
+mem[31090] = 52291
+mem[31244] = 377352406
+mem[10621] = 18801757
+mem[31666] = 5100853
+mask = 10X0110X11XX101XX1000011001001010100
+mem[18680] = 80608039
+mem[13197] = 7957847
+mem[17080] = 117501010
+mask = 1000110011111X11X1XXXX1X000X010011X1
+mem[25308] = 257586
+mem[14518] = 62108102
+mem[21633] = 1544993
+mem[36955] = 1363
+mem[45764] = 49755959
+mem[40967] = 425
+mem[47858] = 611686
+mask = 0010111010X0111111011X0110X0101010X1
+mem[7451] = 1208
+mem[31918] = 769
+mem[29313] = 1888678
+mem[52254] = 32237487
+mask = 00X001001111X11111X010000X0110XX0X11
+mem[61531] = 15796066
+mem[305] = 130785
+mem[25845] = 197912
+mem[29251] = 374061
+mem[37177] = 17950
+mask = 100100X00110111111100110001X1X100X00
+mem[40491] = 66538375
+mem[42244] = 240009051
+mem[18805] = 33518831
+mem[17072] = 518835559
+mask = XX100100X1101X11010001X11001100XX1XX
+mem[16935] = 1124623
+mem[45248] = 155461
+mem[37224] = 5755511
+mask = 00X011101110101X10X1XXX1100X0001000X
+mem[6440] = 116801
+mem[193] = 7318437
+mem[58568] = 8082803
+mem[43695] = 909697
+mem[29001] = 27290
+mem[29210] = 91241
+mask = XXX01X001111111111101010000001XX1011
+mem[21289] = 354401446
+mem[33814] = 1605382
+mem[16967] = 242083755
+mem[60470] = 22550
+mem[16485] = 3945104
+mem[37687] = 86474
+mem[51031] = 5255
+mask = 00100100111101111100100X0X001XX10011
+mem[34832] = 191857526
+mem[30126] = 180246093
+mem[310] = 1895
+mem[49300] = 117732
+mask = 00000100011011X11X10010XX1XX0X1101XX
+mem[54544] = 1368
+mem[30126] = 596855
+mem[18483] = 124319430
+mem[63246] = 95337119
+mem[3917] = 1620395
+mask = 00XX010001101111X110011X010100X10XX1
+mem[31090] = 203896198
+mem[36989] = 203
+mem[8762] = 372392
+mem[59728] = 486751
+mask = 00101X00X11X11111X100010XXXX0011011X
+mem[59728] = 30591660
+mem[43720] = 315507593
+mem[39732] = 42157
+mem[3440] = 242110717
+mem[36955] = 871544
+mem[51251] = 2489781
+mask = 00X00X0X01111111110000001100100X0011
+mem[51149] = 12451455
+mem[17566] = 351620601
+mem[33842] = 1119118
+mem[23677] = 100601411
+mem[12826] = 2474316
+mask = 00X00100XX1111111110111X1X010X10010X
+mem[43163] = 27012
+mem[53314] = 2717910
+mem[20842] = 239857
+mem[43816] = 3173699
+mem[11343] = 37315312
+mem[37493] = 262038
+mem[25824] = 13598271
+mask = 00X0X00001101X1X10100000110001110101
+mem[39732] = 1402
+mem[50014] = 32437274
+mem[10770] = 192187204
+mask = 000101110110X111111011010X11000000XX
+mem[51283] = 2490405
+mem[33814] = 471881
+mem[15119] = 3807095
+mask = 000X00XX0X10111111X0011XX11X10X00010
+mem[10405] = 46099021
+mem[42308] = 1001
+mem[57329] = 2310
+mask = 11X10110011XX100XX1010101100010X0000
+mem[40240] = 28185370
+mem[43296] = 2212
+mem[15632] = 3512122
+mem[61953] = 2534700
+mem[58797] = 258533
+mask = 00000X00011X1X11X1X001000010010X1110
+mem[13671] = 66116
+mem[5234] = 46868488
+mem[48068] = 259070
+mem[35833] = 1904
+mask = XX1101000XX01111X1100XX10X1000011000
+mem[58276] = 827
+mem[29197] = 6552
+mem[21249] = 173
+mem[5723] = 4730123
+mem[59627] = 3299104
+mem[17008] = 74955518
+mask = 0X000100111111XX11001000000110X00100
+mem[53231] = 909153
+mem[28837] = 1739162
+mem[21336] = 3932
+mem[32899] = 872661
+mem[29051] = 228916
+mask = 0X1X11000111111X101111100000X10X00XX
+mem[23121] = 4940
+mem[64259] = 339599819
+mem[268] = 2533
+mem[5725] = 1430
+mem[56946] = 618
+mask = 001X1X0001111111101001111001X101XX10
+mem[46780] = 339675
+mem[57420] = 10161
+mem[32105] = 5534
+mask = X1000X10011001111110110100X01010X011
+mem[47922] = 892051565
+mem[50583] = 2962439
+mem[43673] = 107
+mask = 000001001X11011111101X00XX1111100111
+mem[17938] = 29693823
+mem[27809] = 17197
+mem[62755] = 6590924
+mem[26483] = 15837
+mem[5245] = 486
+mem[8213] = 1239
+mask = 0010X0000XX11XX1100010X0X11000101XX1
+mem[3842] = 3541
+mem[55663] = 76779528
+mem[29851] = 2801
+mask = XX011X0001111111X1XX0000000001X00010
+mem[20066] = 97384
+mem[35212] = 10209
+mem[15847] = 499740
+mem[9349] = 9638367
+mask = 001011X00XXX11X111100X1XX00010100X01
+mem[52845] = 1056563
+mem[30126] = 13918626
+mem[17709] = 25538089
+mem[1413] = 459461
+mem[59577] = 52944410
+mask = X1011001X0XX111011100XX010101X01X010
+mem[56449] = 144
+mem[8753] = 984864
+mem[23728] = 173703761
+mem[34970] = 28269
+mem[32500] = 49931
+mask = X0000100111X11111110X10X0101001100X1
+mem[64582] = 6646737
+mem[37177] = 10
+mem[57474] = 313623
+mem[17322] = 147838906
+mem[28766] = 15110001
+mem[49] = 80836580
+mask = 001010000X11111X10X0X111X0XX00X100X1
+mem[53163] = 15243
+mem[61002] = 406400
+mem[28930] = 465647779
+mask = 0010000X11101111100010X00XX1XX01XX01
+mem[1315] = 625209
+mem[44187] = 14395
+mask = X0010XX0011011111110110X0101XX0X00XX
+mem[31859] = 95408
+mem[16534] = 121119590
+mem[26550] = 8188494
+mem[37302] = 407378
+mask = 001XX1XXX1111X1110100001X00000111001
+mem[37574] = 31364
+mem[26443] = 2676291
+mem[22192] = 26966115
+mask = 001X010X001X011110001101XXX0X101XX11
+mem[41368] = 50472035
+mem[25252] = 3850
+mem[21011] = 7912441
+mem[55890] = 2474497
+mask = 0010010000X11XX111X0101X100000101000
+mem[57489] = 10006848
+mem[7880] = 30889
+mem[54742] = 14408
+mask = 0010100X00X1X11X101000X1X00X001100XX
+mem[28474] = 137340532
+mem[57910] = 3261
+mem[35212] = 974067528
+mem[24595] = 15641
+mask = 0X0X01X00110X11111X0X001X1011010X101
+mem[1515] = 4597
+mem[20626] = 483632
+mem[50912] = 101611112
+mem[62450] = 463312
+mask = 00101X000X0111011110X10X10011X100001
+mem[5378] = 132014
+mem[13345] = 2058543
+mem[42684] = 2824
+mem[34576] = 6385683
+mem[27201] = 2519
+mem[9632] = 202081
+mask = X0X1010001X011X111100001001X01XX1XX1
+mem[1538] = 2389067
+mem[4972] = 19131
+mem[23129] = 256828081
+mem[17188] = 185346747
+mem[44295] = 143437003
+mem[44830] = 5686
+mem[46528] = 4177799
+mask = X0010010001X1111110011X011XX11X1X011
+mem[7033] = 2748
+mem[2431] = 17997007
+mem[13924] = 90861
+mem[63656] = 497878
+mem[61841] = 891
+mem[10405] = 6177
+mem[55811] = 43078384
+mask = X110XX0011111XX1X11001X000000001X000
+mem[62283] = 8553774
+mem[14788] = 308418
+mem[5878] = 2324
+mask = 0000010001101111101XX1X00001001101XX
+mem[23816] = 69720
+mem[29524] = 197631
+mask = 10X100100011X111110011010X0X10X10XX1
+mem[5288] = 1072
+mem[34681] = 1902
+mem[47529] = 1012160
+mem[42117] = 232642695
+mem[7153] = 420427964
+mem[23129] = 10261
+mem[24545] = 1661292
+mask = X1011X010XX0X1X1X010001001001X001111
+mem[3984] = 17460969
+mem[43208] = 1626
+mem[12288] = 3244
+mem[1261] = 685777140
+mem[35662] = 3875
+mem[13197] = 807702837
+mem[8450] = 39850899
+mask = 001X0X00011X11111000000110010X0XXX1X
+mem[35167] = 3384
+mem[1969] = 3362919
+mem[4732] = 99083530
+mem[58162] = 1382314
+mask = 10000X10011011X11010000X11X1X0010100
+mem[50583] = 4112
+mem[4097] = 907
+mem[45785] = 1275731
+mem[31108] = 7733
+mem[50267] = 2625942
+mask = X0010X1001X011111110110011000111X0X0
+mem[35870] = 1100551
+mem[8514] = 8042956
+mem[10848] = 96032
+mem[44678] = 213384
+mem[25743] = 3586812
+mem[34074] = 991022
+mask = X010X1001111X1111110010X000011000011
+mem[61953] = 3703
+mem[41415] = 250960289
+mem[24262] = 14129393
+mask = 000X010XX11011X111100100110X001000X0
+mem[51393] = 320156165
+mem[27955] = 21751009
+mem[61468] = 8941693
+mem[24188] = 176466079
+mem[10717] = 2950
+mask = 0010XX00X11110101000X0110000X101X100
+mem[37149] = 18981413
+mem[12384] = 479738
+mem[17072] = 5196
+mem[59325] = 170080
+mem[3269] = 86268393
+mem[48598] = 18530
+mem[11287] = 4082
+mask = 0X101100X11X11111X10X0X01000X1110010
+mem[47267] = 12410
+mem[8609] = 6923289
+mem[28364] = 23091829
+mem[63780] = 858
+mem[21558] = 48929393
+mem[46110] = 74033138
+mask = 001011X0011111111010X10100X10X000011
+mem[30364] = 14013071
+mem[23121] = 5777
+mem[54108] = 11707710
+mask = 0010XX00X11X11111XX010X00X000X010011
+mem[29453] = 3480476
+mem[7516] = 869816189
+mem[57136] = 130673464
+mem[8609] = 2000
+mem[45543] = 1014823
+mem[3249] = 75
+mem[14460] = 18422415
+mask = 000X011000111X1X111010X0X10X11X10011
+mem[14556] = 7853751
+mem[29755] = 535169084
+mem[24262] = 4027
+mem[34051] = 13187123
+mask = 000X011X0X1X1111X110000000XX0000X11X
+mem[13879] = 2383
+mem[57329] = 749
+mem[54544] = 3055190
+mask = 00110XX00111111X10010001XX1000101000
+mem[4852] = 429814346
+mem[55439] = 7610
+mem[31685] = 811508716
+mem[38296] = 185763
+mem[16482] = 3668
+mem[47529] = 3803
+mask = X01011000111111X1011010101101X011X01
+mem[58499] = 851439
+mem[38516] = 3082
+mem[32500] = 364520
+mask = 001X110001111X10X000001011X001000101
+mem[44653] = 157371860
+mem[2226] = 58088617
+mem[10098] = 67459
+mem[45739] = 3994
+mem[4180] = 206930963
+mask = 001XX0000XX1111010X00011000001011011
+mem[53876] = 843104
+mem[56118] = 1019
+mem[39503] = 6758
+mem[24134] = 9483199
+mem[25914] = 26956
+mem[10098] = 63837172
+mem[40642] = 2366588
+mask = 0XX10X1001X1111X1X101010010XX01000X0
+mem[16432] = 17158914
+mem[29927] = 9292527
+mem[57922] = 24395252
+mem[48327] = 253
+mem[15450] = 496726
+mem[57027] = 518857449
+mask = 0101XXX0011X11111110X0XX01X010000111
+mem[39393] = 570
+mem[38893] = 21253926
+mask = 0010010X01XX1X1101100000010100110101
+mem[64325] = 416581774
+mem[26376] = 1666947
+mem[6276] = 90042
+mask = 0X00001101101111X11X010010XX1X1XX01X
+mem[20354] = 2180
+mem[50761] = 7237731
+mem[54710] = 5718
+mem[43883] = 2618938
+mem[59235] = 22130448
+mem[59325] = 14410783
+mask = 00000001X0101X11X110101001X11100X0X1
+mem[24262] = 10756242
+mem[59282] = 296121
+mem[15931] = 49
+mem[44067] = 339152264
+mem[22192] = 2750756
+mem[897] = 639
+mask = 001011X011111X1110X01X01X0XX0101X101
+mem[21410] = 5056
+mem[43472] = 198924166
+mem[50343] = 5363196
+mem[7486] = 773744
+mem[49418] = 77311216
+mask = 001X0000011011111X00000X1011X1X01XX0
+mem[19633] = 5522082
+mem[4682] = 51724569
+mem[36252] = 260
+mask = 001011X0111X1X1110XX000X100001X100X0
+mem[34373] = 803
+mem[61841] = 25585959
+mem[29051] = 2011
+mem[53885] = 4255251
+mem[55135] = 49781551
+mem[11748] = 5712
+mask = 000001000X1011111110X11X0XX11101100X
+mem[28073] = 257781932
+mem[32292] = 7788
+mem[47529] = 21491591
+mem[26354] = 3991
+mem[46496] = 225777
+mem[19054] = 6818
+mem[46391] = 1804050
+mask = 0X01X1110011111101X011X1X10010011100
+mem[8848] = 3301953
+mem[21325] = 828483041
+mem[35954] = 393891988
+mask = 001010XX11X0X11X1010001000001111X1X1
+mem[14556] = 48978
+mem[17078] = 3023995
+mem[41895] = 1263
+mem[26354] = 982
+mem[47494] = 9997
+mem[42458] = 139205796
+mask = 01011X0X01XX11X11X10001X010X10001100
+mem[30326] = 230268
+mem[13671] = 406
+mem[13219] = 816366
+mask = 00X0111000101101XX10X010X100X0101X01
+mem[9172] = 5603
+mem[10540] = 399
+mem[6994] = 2969
+mem[41827] = 157730
+mem[57564] = 713362
+mem[16823] = 335722
+mem[38893] = 724
+mask = X1X10110011111X01X101X111101X0X1X000
+mem[5336] = 623
+mem[31859] = 88241437
+mem[7282] = 19257561
+mem[37684] = 5467271
+mem[50354] = 2120264
+mask = 0000X100111X1111X1X001X0000X00110001
+mem[61468] = 9124391
+mem[35212] = 23096803
+mem[9949] = 9454
+mem[1331] = 995
+mem[39238] = 74423
+mask = 00100100111X1111111011X1XX010X1X0010
+mem[39294] = 114968517
+mem[23155] = 1708
+mem[29927] = 7693420
+mem[26742] = 2017
+mask = 001001001X101111X1X0XX01100X0X0101XX
+mem[29056] = 30646
+mem[59210] = 113022
+mem[43000] = 144138476
+mem[35167] = 721
+mem[30809] = 507151422
+mask = 0001X0100101111010X0100000X0X0100010
+mem[34554] = 49221
+mem[7437] = 62877
+mem[59828] = 184498
+mem[45586] = 20089049
+mem[48248] = 98197865
+mem[44772] = 113026522
+mask = 00X0X000111X11111010X10X1011010X0X01
+mem[40499] = 11427785
+mem[60906] = 496319403
+mem[55126] = 270707060
+mask = 001XXXX10011X1111000110X0X000XX10001
+mem[17938] = 535040
+mem[59138] = 250862772
+mem[8507] = 41576622
+mem[14146] = 1026
+mem[32774] = 2975
+mem[39952] = 440004
+mem[13671] = 2552
+mask = 00000100X110111X111001X0X10X0XX100X1
+mem[43163] = 1156
+mem[35680] = 6039549
+mem[15378] = 657
+mem[52635] = 15396
+mem[25926] = 210
+mem[28827] = 206264701
+mask = 001X100X001X1111100011X1XX1000100X01
+mem[5753] = 1286
+mem[6271] = 6375
+mem[47122] = 5264524
+mask = 0X01100XXX10111X1X100X10000X1000001X
+mem[51736] = 2480
+mem[16722] = 663122146
+mem[18483] = 119830
+mem[13423] = 136699070
+mem[65442] = 23484946
+mem[50742] = 716
+mem[14788] = 129808
+mask = 100100000X101111111001000101X00X01X0
+mem[35279] = 11923915
+mem[12886] = 216539704
+mem[29197] = 258471
+mem[34051] = 24342647
+mem[34556] = 219870381
+mask = 0010X100011011X11110X1101X010X1001X1
+mem[17497] = 684
+mem[8762] = 255129
+mem[44486] = 49869056
+mem[35680] = 210517
+mem[18805] = 17289
+mask = 001011101X1011111X01111010001000XX01
+mem[31918] = 89826257
+mem[31090] = 57962
+mem[53807] = 2817
+mem[12784] = 2137
+mem[31369] = 64658
+mask = X000X1X0011011111010X0X111XX00X1X100
+mem[63989] = 3267
+mem[23677] = 2600
+mem[42781] = 125518
+mem[37480] = 3172
+mem[23573] = 8287963
+mask = 0010110001111XXX10XX0X1X0X0000010100
+mem[1654] = 317418946
+mem[46425] = 459126
+mem[31666] = 9190945
+mem[12079] = 168185843
+mem[39256] = 178728
+mem[52287] = 59458806
+mask = 000001000110111X1010X00000X100110X00
+mem[55820] = 2071
+mem[33429] = 821261571
+mem[16244] = 3215
+mask = 100110100110X1111110X01101X1XX10X011
+mem[47796] = 98
+mem[31040] = 1031334
+mem[9622] = 1580517
+mask = 00100X001110XX11110000011101X0011001
+mem[42781] = 312404
+mem[39942] = 123252858
+mem[20867] = 1356
+mem[14667] = 576
+mem[35502] = 298476332
+mem[53427] = 365745
+mask = 00010X100110111111100010XX00XX000100
+mem[53876] = 603517
+mem[10405] = 16459102
+mem[45543] = 4443
+mem[41543] = 1411
+mem[62450] = 6470215
+mask = 100X110X11111X110X0010000X0X10001X10
+mem[5336] = 650575
+mem[50124] = 3080229
+mem[51618] = 156
+mem[42185] = 1366
+mask = 00X0X1101110101110XX1011X001001110X0
+mem[30736] = 333574460
+mem[13675] = 8643742
+mem[12826] = 453315
+mask = 000X00100X1X1X111XX0101X0X100111000X
+mem[54995] = 183737953
+mem[63234] = 679
+mem[61488] = 337
+mask = 0XX11111X0111X1X01001111X00X11001XX1
+mem[10621] = 122118726
+mem[6109] = 9210
+mem[15688] = 184799
+mem[25564] = 367237
+mask = 00110000011X1111100X000X00XXX1XX1000
+mem[17938] = 33020705
+mem[2666] = 88651117
+mem[21482] = 161753