aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornytpu <alex@nytpu.com>2020-12-14 13:35:32 -0700
committernytpu <alex@nytpu.com>2020-12-14 13:35:32 -0700
commitf859bb3d4c2aaffe14426d95f8164bc83314f550 (patch)
tree68e48697525bacc9714a20e65d24130655968e2d
parentadded day 13 (diff)
downloadadvent_of_code_2020-f859bb3d4c2aaffe14426d95f8164bc83314f550.tar.bz2
advent_of_code_2020-f859bb3d4c2aaffe14426d95f8164bc83314f550.zip
added day 14
-rw-r--r--day14/input.txt567
-rw-r--r--day14/problem_1.c82
-rw-r--r--day14/problem_2.c136
-rw-r--r--day14/test_input.1.txt4
-rw-r--r--day14/test_input.2.txt4
5 files changed, 793 insertions, 0 deletions
diff --git a/day14/input.txt b/day14/input.txt
new file mode 100644
index 0000000..2a83350
--- /dev/null
+++ b/day14/input.txt
@@ -0,0 +1,567 @@
+mask = X111000X0101100001000000100011X0000X
+mem[4812] = 133322396
+mem[39136] = 1924962
+mem[35697] = 29912136
+mem[41065] = 2558851
+mask = 11001101X110000X010X01101100X1X0X001
+mem[38134] = 481
+mem[53084] = 5470
+mem[37619] = 2696
+mem[12248] = 262787806
+mask = 011011000111X1000100010X00XXX0X010X0
+mem[47555] = 417868017
+mem[43020] = 11374336
+mem[9849] = 677
+mem[10472] = 2780071
+mem[6278] = 44095
+mem[61339] = 4820103
+mask = 0001X1X0111101X0000000101110101XX010
+mem[54969] = 910
+mem[39162] = 100172
+mem[17392] = 11619
+mask = 110111X10111010X00000X1X10011X00X001
+mem[45516] = 1541
+mem[4373] = 50557596
+mem[59075] = 12671
+mask = 110111XX0X100100000011011001XX0XX100
+mem[62464] = 171815
+mem[46196] = 148784
+mem[20537] = 616
+mem[21990] = 737337799
+mem[5155] = 1869813
+mem[55244] = 25386
+mask = 1X011XXX0X110X000000111111X0010X1100
+mem[14027] = 131770871
+mem[35273] = 329
+mem[27168] = 13145
+mem[15552] = 20442
+mask = 000011110X1X1110100011XX010X0X0X00XX
+mem[45041] = 535
+mem[41349] = 2943
+mem[32249] = 13150
+mem[42109] = 12648516
+mem[38042] = 469004
+mem[31535] = 3710
+mem[36786] = 4985
+mask = 01XX01X10111X0X1XX0X011111101110X000
+mem[21541] = 149981598
+mem[55577] = 358503898
+mask = 010XXXX10X100000010010010001011XX1XX
+mem[61804] = 6027
+mem[31021] = 7248
+mem[13164] = 3531
+mem[44086] = 3037407
+mem[36788] = 692
+mask = 000X1111011XX0100000X1011101X110XX00
+mem[54870] = 3410195
+mem[56040] = 124015462
+mem[44086] = 7010
+mem[12761] = 1494532
+mem[19537] = 46357
+mask = 1100110X01101100X0001100111X000110X0
+mem[22275] = 64052559
+mem[44465] = 140458063
+mem[40841] = 137069
+mem[3249] = 32613
+mask = 01011X010010011X01011001000000X01X00
+mem[39991] = 823052
+mem[9454] = 18550503
+mem[46196] = 49070
+mask = 00001X110011X1110XX0X011XX0X00100111
+mem[24003] = 69183
+mem[51214] = 1231
+mem[11159] = 190497327
+mem[60674] = 2663233
+mem[45359] = 221181147
+mem[24393] = 6821
+mask = 0X00XXX1011100XX0100X11110X001110001
+mem[2424] = 16287
+mem[62077] = 204882244
+mem[51144] = 1552
+mask = 0X011X11001101X00000000X10010101X000
+mem[52533] = 814
+mem[62367] = 2681
+mem[15329] = 2524
+mem[5958] = 33152916
+mem[5169] = 14940999
+mem[7316] = 16737491
+mem[46594] = 82304549
+mask = X0011111X11X0010X0X000000X0X01000000
+mem[33860] = 9277
+mem[40193] = 5247776
+mem[21536] = 5090
+mem[55159] = 525789978
+mem[7272] = 1947
+mem[43269] = 1262
+mem[60321] = 366
+mask = 0001111100111X10000X0X0001XX0110X001
+mem[39371] = 20355
+mem[20076] = 130194
+mem[62464] = 165608
+mem[5363] = 12798999
+mem[23392] = 172431
+mem[26007] = 1025238334
+mem[31848] = 2690303
+mask = 01000001XX1X0000010010X0001X1111X001
+mem[47846] = 67159609
+mem[20937] = 4060
+mem[25317] = 2498
+mem[3297] = 10289
+mem[24930] = 32257217
+mem[18446] = 224279204
+mask = 1X01111100X1010X00X0001100011001010X
+mem[13122] = 14370686
+mem[4812] = 1894
+mem[6609] = 52772339
+mem[14721] = 1976711
+mask = 01X1X111X011X000X000000X00X10100X000
+mem[40113] = 2623234
+mem[323] = 652
+mem[1945] = 7426
+mem[57708] = 9306
+mask = 11011X101X100100000001110100X111000X
+mem[33159] = 2659
+mem[22884] = 64195
+mem[834] = 833
+mem[45165] = 9968
+mem[28935] = 4681138
+mem[44677] = 505010306
+mask = 1101110X0X11010000001X010X001X000X00
+mem[16970] = 183048247
+mem[42799] = 257494062
+mem[13112] = 533254525
+mem[62010] = 512202
+mem[20033] = 426
+mem[43020] = 400947
+mem[48764] = 118538673
+mask = 0001X1X10011111000X1000X101XX010X0X0
+mem[27241] = 68762
+mem[32235] = 3226
+mem[20033] = 371335116
+mask = 0100110101110000X1X1XX11X011X01X1100
+mem[54774] = 1707102
+mem[53424] = 1648856
+mem[15029] = 167830926
+mask = 11X11101X01000000100010XX00101100100
+mem[56937] = 2076568
+mem[44446] = 3568
+mask = 0X01011XX11XX1X000001X11000100010010
+mem[26707] = 257188
+mem[20837] = 1147
+mem[35774] = 8287058
+mem[59176] = 31911785
+mem[50627] = 323142
+mem[51555] = 112545
+mem[13400] = 2049087
+mask = 010101010111000001X0X010X01101X10100
+mem[2294] = 1466
+mem[31885] = 1602
+mem[59859] = 61908
+mask = X0001001011100X10X0011X1X0X0X11110X1
+mem[27773] = 7111614
+mem[39484] = 7001259
+mem[31946] = 84116977
+mem[39162] = 96247952
+mem[51914] = 12595221
+mem[49298] = 71588604
+mask = X0X0X100101101XX0000001X100000000001
+mem[48415] = 80182
+mem[59003] = 7049
+mem[6017] = 2787426
+mask = 0X0X1X1101100010000X10000110111X0000
+mem[24393] = 4269
+mem[23336] = 64391144
+mem[46643] = 130406
+mem[59662] = 113947
+mask = 01X0XX0X01110X00010X0100001X01X0X001
+mem[25720] = 322160
+mem[57525] = 111148340
+mem[43480] = 1919
+mask = 010XX111001100X0000X0001000101X00X11
+mem[33465] = 911366523
+mem[13169] = 81794
+mem[57515] = 1697
+mem[4419] = 29555
+mem[4240] = 416685
+mem[26177] = 28183
+mask = X001X11X111100100XX010000X0101X00000
+mem[65077] = 6440266
+mem[33028] = 10910
+mask = 100110111110001X100000011X00X110X000
+mem[8931] = 10038533
+mem[53984] = 2831
+mem[17466] = 14082894
+mem[38326] = 49428809
+mem[4156] = 9647963
+mask = 01001001011XX0X101X0111111001XX01X10
+mem[28561] = 7722001
+mem[53427] = 179659
+mem[37705] = 1016
+mem[32033] = 1659677
+mem[42597] = 97610103
+mem[40939] = 126093
+mask = X001111X00X110100000001X010101110000
+mem[39162] = 1420260
+mem[49152] = 2802
+mem[54774] = 2549
+mem[6401] = 1061
+mem[25048] = 791
+mem[21448] = 56382
+mask = 0X011X1X1110X01XX010000101010XXX0000
+mem[12854] = 4443
+mem[16736] = 3097
+mem[15329] = 2868
+mask = X1011X0100100XX001X1X001X10XX1101110
+mem[22173] = 146697
+mem[11532] = 4316
+mem[41112] = 143103
+mem[35929] = 36019
+mem[14185] = 18987
+mask = 010011X1011X00000X0X1110000X0X1X100X
+mem[51323] = 196738
+mem[20861] = 775
+mem[49853] = 5614963
+mem[4852] = 4092504
+mem[34732] = 10160
+mem[63865] = 552463
+mem[53015] = 35092
+mask = 110111X00011X1X00X1000X110010000X000
+mem[59891] = 1040
+mem[14027] = 831
+mem[14721] = 431731202
+mem[43480] = 136732740
+mem[62010] = 53198026
+mem[52533] = 65
+mask = X1011110001X01XX00X000X0X0011X0X0000
+mem[63869] = 7097798
+mem[65485] = 43091
+mem[12625] = 258935
+mask = 1001111111100101000X00111XX100110X11
+mem[29081] = 743
+mem[11697] = 449089661
+mem[27440] = 361017403
+mask = 1X01111111X1001010X00X000X1X11100000
+mem[61025] = 167531331
+mem[42074] = 92704
+mem[18402] = 472
+mem[40360] = 280090
+mem[5198] = 87354676
+mem[9957] = 234433
+mask = 0X001001011XX01101101111111X110010X1
+mem[50537] = 346959
+mem[21274] = 2621975
+mem[4434] = 22182688
+mem[14816] = 111642816
+mem[45030] = 1047
+mask = X10111010X100000010XX001110X0X0X11X0
+mem[54995] = 10747901
+mem[56949] = 580474160
+mem[13122] = 1836825
+mem[53718] = 320640
+mem[46066] = 20031967
+mem[9592] = 496370249
+mask = 011X000101X1X000010XX00XX0X001110X00
+mem[63634] = 58
+mem[6549] = 125465708
+mem[65227] = 106568
+mem[7157] = 119628
+mem[53879] = 3596
+mem[24737] = 2988756
+mem[28613] = 30239165
+mask = 0000X1X10011011100001111110X000011X1
+mem[20783] = 23844666
+mem[60525] = 119787833
+mem[25174] = 31103270
+mask = 1101110X011X000X000011111111110X1101
+mem[45272] = 46518537
+mem[3148] = 1496883
+mem[42109] = 267291213
+mem[45414] = 29326966
+mask = 0X0010010111X011010011X0X11001101101
+mem[55042] = 14456992
+mem[30328] = 1191922
+mem[51217] = 39478
+mem[1322] = 2415193
+mem[24047] = 687485
+mask = X1011001XX1X00000X1110011100X110101X
+mem[42521] = 44802983
+mem[45188] = 10474
+mem[14816] = 1004064
+mask = X00111XX1110010X000001X11XX000010XX1
+mem[64649] = 1321
+mem[27940] = 9780
+mem[10759] = 1316
+mem[42260] = 637
+mask = 000001XXX110X000011X00X0X11111X1011X
+mem[8130] = 586439
+mem[20778] = 6114
+mem[41065] = 69134521
+mem[57668] = 25104
+mask = 0001111000XX010000000001X0X110000001
+mem[10566] = 242598767
+mem[10513] = 341510032
+mem[4240] = 11342
+mem[39251] = 1852
+mem[52533] = 505
+mem[62768] = 1846627
+mem[41273] = 78405
+mask = 110111X000X101000X00X001111X11000001
+mem[33847] = 843
+mem[61025] = 231
+mem[32862] = 3139142
+mem[39477] = 104793
+mem[48709] = 22758
+mem[34944] = 81906
+mem[60884] = 922756598
+mask = 01XX10X1X0X00110011100X1001000X01110
+mem[34411] = 14772
+mem[9081] = 3144
+mem[61341] = 5637589
+mem[50639] = 938779
+mem[50087] = 920425693
+mask = 00001X1100111X1XX0000011X00000000101
+mem[20861] = 406200
+mem[59075] = 9157990
+mem[21990] = 505513
+mem[9592] = 413
+mem[17466] = 858
+mem[8519] = 949888
+mask = 0110X1X00111X1000X0010100010110XX100
+mem[49665] = 865148
+mem[4812] = 68414685
+mem[34425] = 226483883
+mem[63865] = 2137555
+mask = 000111100110010000XXX11110110100X001
+mem[8335] = 716
+mem[46594] = 986
+mem[50399] = 186
+mem[10648] = 865
+mem[22244] = 24152
+mem[34859] = 58911
+mask = 11011X00001X0100001001111X10X0010101
+mem[57303] = 25331540
+mem[51255] = 24
+mem[47101] = 73094
+mem[33918] = 1216
+mem[29564] = 1997625
+mask = 0001111X1X1X0X1000X00XXX0101X0100000
+mem[41256] = 6851204
+mem[64098] = 3891136
+mem[4192] = 1119
+mem[63634] = 412948
+mem[7034] = 184980285
+mask = 0X11X00101111000X10001100100011X1011
+mem[52085] = 320289
+mem[61015] = 172827015
+mem[58495] = 1213483
+mem[31021] = 8612509
+mem[33625] = 36389
+mem[24830] = 609539269
+mask = 1001111X0001010XXX0000X01X0011X10000
+mem[4652] = 50833
+mem[14676] = 853672
+mask = 110011XX0110XX000X001100XX001X010X00
+mem[2230] = 13635
+mem[26596] = 1541
+mem[48691] = 3294711
+mem[54923] = 402638
+mem[16937] = 5998214
+mem[45115] = 914360
+mask = 0X1011000111XX00010011011X00010X0X01
+mem[45320] = 48783
+mem[26007] = 8905339
+mem[17200] = 675445941
+mem[16192] = 2472925
+mem[32043] = 18546
+mask = 0X0X010XX110X0X001X0X010110111000110
+mem[13424] = 2992684
+mem[36655] = 2952618
+mem[15191] = 15889
+mem[46410] = 4107
+mem[10930] = 254266
+mem[17174] = 25
+mask = 01X1010101X000XX010X1X01001101X10000
+mem[46463] = 136939958
+mem[12384] = 35808
+mem[25429] = 58008820
+mem[45359] = 87512582
+mask = 010X1X100011X1X10X0000000001000X0XX1
+mem[46605] = 1230722
+mem[25174] = 437033994
+mem[12673] = 730
+mem[19856] = 1681
+mask = 000X11X0X0110100000000011X11100X0XX1
+mem[11408] = 935967
+mem[20861] = 1048829
+mem[24219] = 67911289
+mask = X1X1110X00X1X0000000111110100XX10000
+mem[40028] = 1018
+mem[59183] = 14694
+mem[2921] = 862972454
+mem[13885] = 4679694
+mem[43628] = 19289
+mask = 000X111X0011X11000010X01X00000101XX1
+mem[12269] = 3866
+mem[17658] = 614591
+mem[9957] = 408492346
+mem[11396] = 4758
+mem[12897] = 14036510
+mask = 01011X100X11010000000X01000111000X01
+mem[19856] = 191255190
+mem[12106] = 3753509
+mem[6353] = 57008
+mem[39019] = 12566363
+mem[48517] = 12346
+mem[8240] = 83254479
+mem[50020] = 11399
+mask = 00011111001X100X0000110000X1X1101X01
+mem[52533] = 2830
+mem[1266] = 7910
+mem[61174] = 2820
+mem[20861] = 95813054
+mem[56965] = 862485
+mem[62822] = 41372
+mem[47420] = 11474585
+mask = 0101111110X001000XXXX0101X1X01000100
+mem[10414] = 48452289
+mem[7061] = 23575482
+mem[8503] = 63531
+mem[42733] = 4606968
+mask = 11XX11100010010X000000110000X000XX01
+mem[25088] = 120084812
+mem[3201] = 6845180
+mem[9592] = 1020
+mem[4433] = 22837
+mask = XX01111XXX1X01000000001110010X010X01
+mem[4240] = 23113
+mem[12224] = 858638082
+mem[17504] = 317
+mask = 0000111100111110X00XX101XX0XX1X110X1
+mem[745] = 904
+mem[53504] = 162
+mem[31021] = 45256
+mask = XXX11100001X01000010X1X1X0X1111110X1
+mem[46463] = 38058788
+mem[31885] = 2525105
+mem[62077] = 13891952
+mem[56140] = 1865
+mem[4652] = 800219
+mem[43269] = 240797
+mem[35058] = 29633574
+mask = 01010101011X000001XX00X01X010001XX1X
+mem[32862] = 187905
+mem[15688] = 239625041
+mem[3826] = 7177029
+mem[45414] = 1397
+mem[3363] = 1396790
+mem[21976] = 10750
+mask = 00X0111100X1X110X00X0100X00XX0111X11
+mem[6233] = 565
+mem[51991] = 1735
+mem[28206] = 1317648
+mem[25174] = 880610
+mask = 110111110X1X0000000010000110010XXX11
+mem[32463] = 8176
+mem[8050] = 6174
+mem[55638] = 3675533
+mem[46594] = 178473
+mask = X00X1X111110001010X000011000001XXX00
+mem[27567] = 1662437
+mem[10759] = 6467
+mem[8908] = 1030
+mem[6248] = 2655
+mask = 0100100001110000010110XX00001XX0010X
+mem[2639] = 8577236
+mem[34870] = 65836
+mem[4053] = 222391
+mem[57945] = 855831793
+mask = 1X011101011X00X0010X0XX1001101111100
+mem[55806] = 1749
+mem[14816] = 56071
+mem[64085] = 451286812
+mem[36655] = 539840
+mask = 0X0X11110X11XXX000000XX1000001000X00
+mem[28935] = 1035091
+mem[28017] = 7410228
+mem[17504] = 6484
+mem[63869] = 56633
+mask = X10X010X0010X00000010011011101001X10
+mem[20976] = 391341
+mem[13770] = 245061
+mask = X10X110X011000000XXX0101111011001001
+mem[55892] = 6223387
+mem[13408] = 26636189
+mem[18959] = 15512439
+mem[13486] = 547061
+mem[51417] = 1732
+mem[47710] = 191002
+mask = 0100111XX110X000X00XX011X0X000101100
+mem[59547] = 6680490
+mem[53421] = 32962554
+mem[4053] = 54327681
+mask = X100X10101100000000XX1X00000X11010X0
+mem[43106] = 972
+mem[16979] = 1712450
+mem[21809] = 60684
+mem[7034] = 32377
+mem[62077] = 1144693
+mask = 110001X1X11000X0X0X0100010000110XX00
+mem[45989] = 43960
+mem[26220] = 2635467
+mem[16394] = 88577466
+mask = 000XX1XX01X0010000000X0010100100XX00
+mem[29081] = 10123
+mem[24649] = 319
+mem[13486] = 1774
+mem[46037] = 182
+mem[17651] = 48948
+mask = 00X111110011110000000X00010X0X000000
+mem[12407] = 3445366
+mem[26177] = 40338
+mem[13408] = 867129107
+mask = 01001X01011000X00X00010100101XX1X010
+mem[36968] = 115095
+mem[5759] = 178851
+mem[27773] = 191592
+mem[35287] = 2056
+mem[26888] = 386883
+mem[20525] = 34626
+mask = 00X1X1100110X100X00001101000010000X0
+mem[27168] = 686235562
+mem[25070] = 380483
+mem[18945] = 1696
+mem[4979] = 15419
+mask = 00XX111100X0010000X0X01X1011X0X1000X
+mem[15024] = 101402
+mem[6571] = 734201087
+mask = 010XX101001000000X01001101XX00001110
+mem[8062] = 974626
+mem[3308] = 45423
+mem[12897] = 147472
+mem[54774] = 247807
+mem[51694] = 1156
+mem[8908] = 615
+mask = 01X011000111110XX1X0011001X101X01001
+mem[1998] = 8383268
+mem[9454] = 105136
+mem[39479] = 6515
+mem[1360] = 239170
+mem[62970] = 13188849
+mask = X001X1110X100100000000111X0100X100XX
+mem[23516] = 899356468
+mem[58739] = 449
+mem[7316] = 4769
+mem[9857] = 373639523
+mem[48106] = 1417
+mask = 1100110101X00000XX011000010111111X01
+mem[8712] = 500891722
+mem[16579] = 431756695
+mem[34425] = 3712
+mem[40112] = 426
+mem[36630] = 610
+mem[20554] = 933886
+mem[48342] = 177911
+mask = 00011X11011X010X00000011X10010X10111
+mem[6531] = 1493325
+mem[35058] = 21547
+mem[13400] = 5230289
diff --git a/day14/problem_1.c b/day14/problem_1.c
new file mode 100644
index 0000000..14b5a20
--- /dev/null
+++ b/day14/problem_1.c
@@ -0,0 +1,82 @@
+/* advent of code day 14, problem 1
+ * by nytpu
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+
+#define NUM_BITS 36
+#define MEM_SIZE 33554432 // decided to reduce the amount from 2^36 a bit...
+#define LINE_BUF 64
+
+struct mask {
+ // x is zero, zeros and ones are literal
+ unsigned long long m;
+ // x is one, everything else is zero
+ unsigned long long x;
+};
+
+void create_mask (struct mask *new, char *val);
+
+int main (int argc, char *argv[])
+{
+ struct mask cur_mask = {0, 0};
+ unsigned long long *mem = calloc(MEM_SIZE, sizeof(*mem));
+ char line[LINE_BUF], *line_st, *instruction, *mask_str;
+ unsigned long long loc, val, sum, i;
+
+ if (argc != 2) {
+ printf("Usage: problem_[x] [input]");
+ }
+
+ FILE *infile = fopen(argv[1], "r");
+ if (infile == NULL) {
+ fprintf(stderr, "Unable to open file %s\n", argv[1]);
+ exit(EXIT_FAILURE);
+ }
+
+ for (
+ line_st = fgets(line, LINE_BUF, infile);
+ line_st != NULL;
+ line_st = fgets(line, LINE_BUF, infile)
+ ) {
+ if (line[0] == '\n') break;
+
+ instruction = strtok(line, " [");
+ if (!strncmp(instruction, "mask", 4)) {
+ strtok(NULL, " ");
+ mask_str = strtok(NULL, "\n");
+ create_mask(&cur_mask, mask_str);
+ } else {
+ loc = strtoull(strtok(NULL, "]"), NULL, 10);
+ strtok(NULL, " ");
+ val = strtoull(strtok(NULL, "\n"), NULL, 10);
+ val = cur_mask.m | (val & cur_mask.x);
+ mem[loc] = val;
+ }
+ }
+
+ for (i = 0, sum = 0; i < MEM_SIZE; ++i) sum += mem[i];
+
+ printf("Sum of all memory addresses: %llu\n", sum);
+ free(mem);
+ return EXIT_SUCCESS;
+}
+
+void create_mask (struct mask *new, char *val)
+{
+ unsigned long long i;
+
+ new->m = 0;
+ new->x = 0;
+
+ for (i = 0; i < NUM_BITS; ++i) {
+ if (val[i] == '1') {
+ new->m |= 1ULL << (NUM_BITS - i - 1);
+ } else if (val[i] == 'X') {
+ new->x |= 1ULL << (NUM_BITS - i - 1);
+ }
+ }
+}
diff --git a/day14/problem_2.c b/day14/problem_2.c
new file mode 100644
index 0000000..a932609
--- /dev/null
+++ b/day14/problem_2.c
@@ -0,0 +1,136 @@
+/* advent of code day 14, problem 2
+ * by nytpu
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <uthash.h>
+
+#define NUM_BITS 36
+#define LINE_BUF 64
+
+struct mask {
+ // x is zero, zeros and ones are literal
+ unsigned long long m;
+ // x is one, everything else is zero
+ unsigned long long x;
+};
+
+struct mem_loc {
+ unsigned long long addr;
+ unsigned long long val;
+ UT_hash_handle hh;
+};
+
+void create_mask (struct mask *new, char *val);
+
+// a clusterfuck of a function type
+void dec_write_mem (
+ struct mem_loc **mem,
+ const struct mask *const mask,
+ unsigned long long loc,
+ unsigned long long val,
+ unsigned long long i
+);
+
+int main (int argc, char *argv[])
+{
+ struct mask cur_mask = {0, 0};
+ struct mem_loc *mem = NULL, *s, *tmp;
+ char line[LINE_BUF], *line_st, *instruction, *mask_str;
+ unsigned long long loc, val, sum = 0;
+
+ if (argc != 2) {
+ printf("Usage: problem_[x] [input]");
+ }
+
+ FILE *infile = fopen(argv[1], "r");
+ if (infile == NULL) {
+ fprintf(stderr, "Unable to open file %s\n", argv[1]);
+ exit(EXIT_FAILURE);
+ }
+
+ for (
+ line_st = fgets(line, LINE_BUF, infile);
+ line_st != NULL;
+ line_st = fgets(line, LINE_BUF, infile)
+ ) {
+ if (line[0] == '\n') break;
+
+ instruction = strtok(line, " [");
+ if (!strncmp(instruction, "mask", 4)) {
+ strtok(NULL, " ");
+ mask_str = strtok(NULL, "\n");
+ create_mask(&cur_mask, mask_str);
+ } else {
+ loc = strtoull(strtok(NULL, "]"), NULL, 10);
+ strtok(NULL, " ");
+ val = strtoull(strtok(NULL, "\n"), NULL, 10);
+ dec_write_mem(&mem, &cur_mask, loc|cur_mask.m, val, 0);
+ }
+ }
+
+ HASH_ITER(hh, mem, s, tmp) {
+ sum += s->val;
+ }
+
+ printf("Sum of all memory addresses: %llu\n", sum);
+
+ HASH_ITER(hh, mem, s, tmp) {
+ HASH_DEL(mem, s);
+ free(s);
+ }
+ return EXIT_SUCCESS;
+}
+
+void create_mask (struct mask *new, char *val)
+{
+ unsigned long long i;
+
+ new->m = 0;
+ new->x = 0;
+
+ for (i = 0; i < NUM_BITS; ++i) {
+ if (val[i] == '1') {
+ new->m |= 1ULL << (NUM_BITS - i - 1);
+ } else if (val[i] == 'X') {
+ new->x |= 1ULL << (NUM_BITS - i - 1);
+ }
+ }
+}
+
+void dec_write_mem (
+ struct mem_loc **mem,
+ const struct mask *const mask,
+ unsigned long long loc,
+ unsigned long long val,
+ unsigned long long i
+)
+{
+ if (i == NUM_BITS || mask->x >> i == 0ULL) {
+ struct mem_loc *s = NULL;
+ HASH_FIND(hh, *mem, &loc, sizeof(loc), s);
+ if (s != NULL) {
+ s->val = val;
+ } else {
+ struct mem_loc *n = malloc(sizeof(struct mem_loc));
+ n->addr = loc;
+ n->val = val;
+ HASH_ADD(hh, *mem, addr, sizeof(unsigned long long), n);
+ }
+ return;
+ }
+
+ for (; i <= NUM_BITS; ++i) {
+ if ((mask->x >> i) & 1ULL) {
+ // write to loc where bit x is zero
+ dec_write_mem(mem, mask, loc & ~(1ULL << i), val, i+1);
+
+ // write to loc where bit x is one
+ dec_write_mem(mem, mask, loc | (1ULL << i), val, i+1);
+ return;
+ }
+ }
+}
diff --git a/day14/test_input.1.txt b/day14/test_input.1.txt
new file mode 100644
index 0000000..e15150a
--- /dev/null
+++ b/day14/test_input.1.txt
@@ -0,0 +1,4 @@
+mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
+mem[8] = 11
+mem[7] = 101
+mem[8] = 0
diff --git a/day14/test_input.2.txt b/day14/test_input.2.txt
new file mode 100644
index 0000000..2e961e3
--- /dev/null
+++ b/day14/test_input.2.txt
@@ -0,0 +1,4 @@
+mask = 000000000000000000000000000000X1001X
+mem[42] = 100
+mask = 00000000000000000000000000000000X0XX
+mem[26] = 1