2020 day 4

This commit is contained in:
tristan 2020-12-14 21:16:03 +00:00
parent b46bf4115d
commit ad3c5b35b6
6 changed files with 678 additions and 0 deletions

577
2020/14/input.txt Normal file
View file

@ -0,0 +1,577 @@
mask = 00101X10011X0X111110010X010011X10101
mem[41248] = 4595332
mem[26450] = 60
mem[32210] = 982366
mem[1060] = 234632920
mem[20694] = 38159
mem[45046] = 58906955
mask = 010110010X1101XX11X0100001X0000X00X1
mem[16069] = 7758
mem[55864] = 2473265
mem[37095] = 103513009
mem[4911] = 1002
mem[63231] = 6932274
mem[21265] = 72322159
mem[43724] = 16591353
mask = 01001X01X101011101010101011X1X000000
mem[63470] = 30339812
mem[16920] = 471738
mem[1014] = 29735753
mem[61061] = 6866
mem[8437] = 9138168
mem[46487] = 1819945
mem[2985] = 15040783
mask = 0X10X1101111001X1X100X1X00011100XX11
mem[32836] = 12902
mem[60365] = 24782
mem[29953] = 10085
mem[18214] = 1160
mask = 001011X10X11100000100X0X0X0X01011001
mem[39434] = 37383633
mem[278] = 670174555
mem[34062] = 20749996
mem[2583] = 6222093
mask = 01X111X1001101X11110100XX001X1000XX1
mem[6075] = 49890
mem[9363] = 2392780
mem[24967] = 218861
mask = X110111X1XX1010101111X01XX1000X001X1
mem[41334] = 11836
mem[24242] = 7263066
mem[17289] = 64986060
mem[2583] = 4702503
mem[21650] = 103905
mem[134] = 486675
mask = 00X010100110XXXX111000XXX1000011000X
mem[45307] = 37940
mem[16597] = 224911
mem[17943] = 392744
mem[55001] = 622484
mem[35954] = 470
mask = 11X01011X11000X1X1100X100X011101X011
mem[1005] = 56755
mem[16146] = 4333571
mem[32347] = 10486693
mem[11452] = 377363
mem[25158] = 328161913
mem[51956] = 250388
mem[10044] = 34078606
mask = 011011X1X111010111110000X001X1X00110
mem[8773] = 10575925
mem[33116] = 175
mem[36101] = 14593
mask = 0100010X110X0101010XX10X011111XX1101
mem[21083] = 1922
mem[3653] = 912
mem[26768] = 7321934
mem[49134] = 17616
mem[62950] = 41565481
mem[12957] = 2136786
mem[10324] = 17788
mask = X11X0X0X11010101110X01111010X1100X11
mem[5462] = 18755
mem[39408] = 2435211
mem[49271] = 6589
mask = X1X011XX01X100010110001X0X0X111X1100
mem[52570] = 2166
mem[28731] = 16573421
mem[18265] = 1192
mem[22435] = 10856992
mem[19263] = 7550
mem[30541] = 434738
mem[36101] = 869138
mask = 010001X001010001XX010100000010110X01
mem[52893] = 125505223
mem[22919] = 597
mem[62950] = 54107
mem[52797] = 7649588
mem[30421] = 3968
mem[30429] = 614720
mask = 01X0X10001X100010X1011XX00000X111X00
mem[44718] = 11141064
mem[42713] = 206218234
mem[51781] = 527553473
mem[1967] = 27527823
mem[6386] = 5404
mask = 00101X10XX11X0XX1110001000001110X11X
mem[62339] = 72046594
mem[14657] = 3243652
mem[750] = 40239
mem[134] = 1936539
mem[5775] = 266384125
mask = 011X111XXX110X01X11X000X00010100011X
mem[2956] = 438895
mem[41520] = 7282
mem[42192] = 34769
mem[8837] = 2587
mask = 01XX11100101000X0X10011XX01010011101
mem[12515] = 450388
mem[62175] = 649233
mem[54743] = 129273
mem[10284] = 159823
mem[31311] = 16983
mem[56137] = 852771967
mask = 11010X1X01010101X1010X11101111X00010
mem[47190] = 526627409
mem[34299] = 540572
mem[61226] = 61426238
mem[12892] = 61446
mem[33421] = 4192
mask = 0110111111X10101111010100XX01XX10100
mem[41685] = 258
mem[26983] = 60795579
mem[28064] = 10483
mem[33070] = 66557269
mem[12624] = 448724
mem[38125] = 141175913
mask = 010X1X00X101000X0111010101XX01011000
mem[12957] = 7693971
mem[45285] = 4628
mem[48546] = 799
mem[17857] = 7578026
mask = 00101X100101X0010110000000XX1010X110
mem[41841] = 234511
mem[27387] = 2990
mem[24636] = 1269957
mem[15638] = 428392
mem[22064] = 272
mask = 0XXX10X01011X011111000000XX0X100X010
mem[26764] = 482715793
mem[8422] = 70439
mem[17857] = 28381730
mem[4524] = 750659820
mask = 11101100010100X1011000111000XX00X010
mem[52570] = 517468200
mem[25263] = 11113122
mem[33421] = 32762600
mask = 11101X01XX1000010X10111000X1101X0X00
mem[16577] = 910
mem[32450] = 16924479
mem[4421] = 24801362
mem[46638] = 8546454
mask = 01X11X1101110101X1X1X010000XX101X001
mem[34209] = 24703796
mem[30481] = 831
mem[46487] = 147322
mem[38619] = 11686
mem[26615] = 1174
mask = 010X0X00110100X1XX000010110XX100X001
mem[53587] = 198046
mem[38420] = 22334
mem[20181] = 962
mask = XX101101X01000010XX01111001111010100
mem[33812] = 107321
mem[8613] = 7395
mem[1117] = 149990
mem[22919] = 23596
mask = 1X01110110010X01X100000001111011X010
mem[57800] = 254591077
mem[6633] = 60308580
mem[8980] = 104196938
mem[5936] = 289911936
mem[44806] = 297364592
mask = 11X10XX0X1010X01010110XXX01111100X00
mem[49271] = 177794
mem[15368] = 259266583
mem[19327] = 590
mem[40243] = 24245
mem[57130] = 1201404
mem[22545] = 1831196
mem[59161] = 25210381
mask = 0X101X11111X010111100X110XX11000X10X
mem[38749] = 2091454
mem[45138] = 621877
mem[52107] = 3430339
mask = 0010X110X11X00101X100011XX111X000100
mem[17228] = 252642
mem[23892] = 13721
mem[43787] = 2786942
mem[55481] = 58875
mem[513] = 892
mem[62445] = 40312
mask = 0010X11XXX11001011X01010X0111110X100
mem[17415] = 7415167
mem[9048] = 46059
mem[2159] = 636711036
mask = X010111X111X010X1110X10100XX1000X00X
mem[38420] = 104527
mem[24790] = 85
mem[58634] = 127952377
mem[8958] = 11672057
mask = X01X111X00X110XX0X10000000000X0X0100
mem[283] = 241
mem[8898] = 36719
mem[49134] = 217820
mem[31884] = 419937
mask = 0XX11110X1110X0101111000000100110X00
mem[27694] = 6848
mem[25843] = 331711
mem[6688] = 581239
mem[41591] = 171
mask = 0100X100X1010X01010X001XX01XX1010101
mem[30429] = 1103121
mem[42192] = 7844667
mem[21668] = 51727200
mask = 001X1X10001X101XX1100X1000101100X010
mem[4322] = 157863993
mem[49962] = 9140
mem[16964] = 1599
mem[14443] = 2038
mem[3767] = 16636129
mem[13476] = 485497191
mem[1663] = 163345
mask = X101110111010101X1X10011001X10110000
mem[13172] = 195
mem[33921] = 5684133
mem[1337] = 51317
mask = X1XXX101110101X1010X0100XX111X101001
mem[63928] = 4636
mem[56436] = 3887978
mem[6185] = 3037
mem[7095] = 11521156
mem[1663] = 121401
mem[7218] = 20750
mask = 010001001X0101010X00001XXX100XX10100
mem[24149] = 309519
mem[16287] = 12731276
mem[29772] = 65227
mem[37172] = 2824
mem[17508] = 59271
mem[22133] = 3806
mask = 01X0X101011X000X0X101000100011111101
mem[14401] = 158547520
mem[37172] = 16841
mem[40439] = 461272566
mem[60909] = 478018315
mem[43219] = 2154608
mem[25369] = 46117
mem[54852] = 79656
mask = XX10111001X100X1X1100X1X0001110001X1
mem[4213] = 900609324
mem[19327] = 28071
mem[30421] = 782
mem[4804] = 17293
mask = 0100X1000101X0010X010101X01000011001
mem[18139] = 1546181
mem[14021] = 33793814
mem[46699] = 2014
mem[51956] = 171606030
mem[29702] = 475302805
mem[18265] = 198549
mask = 0101X0110X1X0101X1X1X01101001X001XX1
mem[38962] = 132592128
mem[9436] = 7464578
mem[12650] = 49333
mem[8837] = 3234578
mask = 011X11101011X101111000XX001110001110
mem[61694] = 1206
mem[32263] = 20761769
mem[2116] = 193628
mem[13505] = 123039
mem[62164] = 14323289
mask = 1X101010100X0101X1110X01000101X00100
mem[21385] = 1022949
mem[51318] = 5667643
mem[17420] = 36980027
mem[29202] = 801
mask = 0101X01XX11X0101X10110X1010001001001
mem[15338] = 23103863
mem[10488] = 4521
mem[13172] = 17055515
mask = X10111X11X01X111X100000000111011X111
mem[36577] = 397263
mem[8992] = 11944917
mem[22064] = 738796
mem[17310] = 1562710
mem[30068] = 4950154
mask = 011111X10111X10X010X00X1X100X0010001
mem[31166] = 6551
mem[62218] = 1528
mem[11467] = 35999360
mem[39578] = 11530695
mem[30855] = 27864
mem[18369] = 1610323
mem[58953] = 12938251
mask = 01X111010X1111X0010X0XX010000X000111
mem[15411] = 1096
mem[49541] = 3181
mem[23568] = 276408
mem[45168] = 1721
mem[11394] = 155136
mask = 1111X1X011010001X101010X100XX11001X1
mem[61945] = 26647548
mem[63262] = 110741
mem[33783] = 158
mem[12753] = 200460
mem[43229] = 7579
mem[37084] = 26507
mask = 0100110011X10101010X00X01X11X1X10101
mem[65089] = 636807464
mem[5775] = 4440830
mem[52107] = 69328099
mem[38420] = 859060126
mem[21272] = 1700
mem[12062] = 176162
mem[12094] = 8733
mask = X100010111XX01X1X1010100X01101001X1X
mem[44718] = 33650499
mem[26507] = 165784650
mem[12622] = 2023
mem[5651] = 120398699
mask = 110001011X010X0111X10X00011110001X00
mem[44975] = 666498
mem[11614] = 751
mem[61354] = 5063
mem[4396] = 1131
mem[25418] = 882
mem[49245] = 64151
mask = 011X1X110111X1XX11X1100X01000X101001
mem[59013] = 1141214
mem[18016] = 95668408
mem[30067] = 18132964
mem[38900] = 286972459
mem[42265] = 13529062
mem[59369] = 6028326
mask = 0110110X010100010XXX0X00X0011X101X1X
mem[6479] = 8816055
mem[28451] = 29446
mem[61417] = 59156
mem[6694] = 15597
mem[29264] = 115437
mask = 11110100110101011101XX0001X1X1110101
mem[46886] = 114630
mem[17383] = 452299
mask = 010X110X11X10101000X001X011010000100
mem[19215] = 487176198
mem[59629] = 2120284
mem[27009] = 3064
mem[42335] = 22072
mem[514] = 2010
mask = 0100X1001101010X010000X0001000X00100
mem[37232] = 2564
mem[20561] = 29506163
mem[27396] = 380700410
mem[34075] = 868
mem[24967] = 1882926
mask = 010X1X01XX110111111X0XX0000X010X0101
mem[61084] = 3068852
mem[33028] = 188720342
mem[17375] = 62850
mask = X10X1X0X110101010X01001001101000X000
mem[24149] = 1815
mem[51489] = 197928369
mem[27694] = 231814
mem[11813] = 1002177793
mem[526] = 104755102
mem[22216] = 8396
mask = 0110111101010001X1X0X11100X010001111
mem[21083] = 2509191
mem[13215] = 172339241
mem[12386] = 106305632
mask = X1X101X11101010101000X010X11101101X1
mem[35709] = 64980388
mem[51838] = 62510
mem[48641] = 1174272
mem[42157] = 149
mask = 0X101100010100010X0X00X1100101111111
mem[35807] = 1100541
mem[10044] = 69616152
mem[3047] = 142725213
mask = 11101X1X10X101010111X001XX10X0X00100
mem[38049] = 110
mem[43097] = 14955394
mem[61810] = 3545867
mem[61238] = 5370
mem[20585] = 191903
mem[26133] = 24248
mask = 010X110X011X00010110010100X0XXX1X011
mem[15950] = 140910
mem[12062] = 424527462
mem[11876] = 236
mem[5182] = 4776
mem[50278] = 490
mask = 010XX011011101X1110101110111110110X0
mem[53736] = 2314
mem[12633] = 5053
mem[66] = 49557761
mask = 01X01101X10101010101000X00111110100X
mem[18849] = 911
mem[20666] = 12891678
mem[5609] = 10432
mem[59720] = 22145720
mem[17508] = 42631
mem[8585] = 3448
mask = 11X1X10X110101X101010X01X0011011X001
mem[30601] = 9140827
mem[30361] = 4166366
mem[46057] = 16057
mem[26983] = 251682577
mem[63197] = 1603252
mem[52893] = 462048575
mask = 011011100111X00X11100X00100010001XXX
mem[17534] = 25807901
mem[4932] = 106350673
mem[42192] = 735653575
mem[10874] = 59007
mask = 01000101110001X1XX001000001X1X011111
mem[48049] = 386
mem[1538] = 138451275
mem[50333] = 15707
mask = 0101100X01110111XX1X0000001011001X01
mem[18139] = 102960
mem[41277] = 5837
mem[44484] = 29937
mask = 11101XX10X1X0001011000100X010010X00X
mem[30615] = 95201946
mem[719] = 3697022
mem[27391] = 150969140
mem[62680] = 427952
mem[7349] = 46922
mem[17375] = 41348888
mem[57800] = 1901
mask = 0010X11X1X11X011111X0011001X01001111
mem[46994] = 118757653
mem[32947] = 23571
mem[8653] = 1364
mem[3767] = 6954112
mask = 01001X0011010X000111X0X0X1X011011XXX
mem[37908] = 88438829
mem[20630] = 618075182
mem[21520] = 101250753
mem[10703] = 475904
mask = 001001101XX10010111X1X10100111XX11X1
mem[17310] = 2889476
mem[2725] = 463419
mem[65001] = 910330085
mask = X1011011X111010101X110X1X1X0010110X0
mem[15999] = 18586203
mem[12825] = 51333145
mem[29966] = 596120517
mem[26866] = 141039
mem[24223] = 415414
mem[24403] = 16110598
mask = 0XX00X01111X01110X011010101X01101010
mem[43382] = 150995
mem[28011] = 1021785
mem[60339] = 7805893
mem[37197] = 268431
mem[17792] = 253366088
mem[21437] = 24057926
mask = XX1011X111100X00111X1100010111010011
mem[18006] = 265940517
mem[55921] = 1634
mem[27656] = 17058
mem[4911] = 3686
mem[33243] = 8125794
mem[47537] = 146165365
mask = 110111011X01X1X10100X000010110111X0X
mem[5775] = 176470
mem[63017] = 24003454
mask = 00011X001X11XX11111X01101010X0000010
mem[1604] = 174349
mem[42888] = 7159712
mem[26615] = 1487
mask = 01X11101100111111X000X1001110001011X
mem[5344] = 8563500
mem[21234] = 166162105
mem[48935] = 10849963
mask = 01000101XX0001111000XX0000X010010X00
mem[24149] = 127627213
mem[27338] = 43164114
mem[47215] = 252815
mem[47431] = 32732410
mask = 01011100000100011X0X0XX101X111111100
mem[13412] = 4193068
mem[45046] = 148
mem[63535] = 11659
mem[6518] = 471308933
mask = 111X111011X1X1X101111X000100001101X1
mem[31114] = 118512878
mem[41334] = 1604
mem[7338] = 571
mem[6001] = 4126415
mem[5215] = 4392
mem[47836] = 1862
mem[22064] = 30804845
mask = 0111010X11X1010X0101X1110011101X0111
mem[13321] = 22426593
mem[37095] = 5357
mem[44281] = 467020
mem[62680] = 2721559
mask = 010001X0010100XX10010000011000101X00
mem[30615] = 261491
mem[31097] = 46202501
mem[27880] = 6002395
mem[51385] = 2780
mem[51435] = 43181943
mem[42192] = 107728750
mask = 101011100X1100110X1X00X0101X10X11101
mem[48366] = 859523
mem[14111] = 859
mem[21668] = 292390073
mem[8073] = 858
mem[12920] = 662378
mask = X111X10011X10101X10101XX01111011X101
mem[20630] = 4051571
mem[55963] = 367
mem[379] = 10962356
mem[33028] = 37
mem[24035] = 9459
mem[50949] = 2030
mask = 001011100X110X111X10101100011X010X11
mem[8437] = 47226
mem[41248] = 319
mem[9624] = 3503
mem[6875] = 5282
mask = 11X111XX100101010100X01XX1X1001X1000
mem[21292] = 1673693
mem[51132] = 10346473
mem[7504] = 4325
mask = 111X010X1101011101X100010XX1X0111100
mem[35415] = 6296
mem[19215] = 1263591
mem[49977] = 379136185
mem[62950] = 28156510
mem[8265] = 28662942
mask = 01X110X01X1X0X11X1X00110000X01000100
mem[54672] = 131784041
mem[11394] = 24602
mem[24646] = 10584
mem[44349] = 4883
mem[54743] = 2940969
mem[8265] = 14841530
mask = 0X10111X11100X0011X011XX00X011011010
mem[49374] = 45910
mem[25923] = 368017518
mem[25114] = 8076340
mem[62690] = 904875563
mask = 010X11X0X1X1010X01010X01101001110101
mem[39408] = 3080
mem[6918] = 125955053
mem[27880] = 29186
mask = 01001101X101010X0101X1001111100010X1
mem[11813] = 153838914
mem[20585] = 1917
mem[21385] = 1881773
mem[8556] = 25758757
mem[22435] = 802061
mem[27631] = 13285866
mask = 01101110111X0001011X10100X0100X10101
mem[23441] = 186656612
mem[2186] = 189388742
mem[12866] = 874882
mem[12947] = 23895
mem[20630] = 77211
mem[42083] = 63015239
mem[51838] = 4984972
mask = 00101110X111001X1X10001XX0X111XXX101
mem[14789] = 244532376
mem[21292] = 736136092
mem[10874] = 513949
mem[16755] = 12361
mem[5416] = 22987
mem[39578] = 106587
mask = 010X11000X010001X1XXX0110011X1X1110X
mem[43479] = 61
mem[47199] = 15617564
mem[18265] = 6027808

27
2020/14/part1.js Normal file
View file

@ -0,0 +1,27 @@
// I wanted to do today's puzzle using bitwise operators, but it kept on spitting out negative numbers, so I gave up with that and ended up with this way that uses arrays and strings instead.
const fs = require('fs');
const input = fs.readFileSync('input.txt', 'utf-8').split('\n').map(l=>l.split(' = '));
let mask = [];
let mem = []
input.forEach(command => {
if (command[0] == 'mask') {
// convert the mask into an array
mask = command[1].split('')
} else {
// grab the number in mem[x]
let index = Number(command[0].substring(4,command[0].length-1));
// convert the arg to binary string
let value = Number(command[1]).toString(2)
// add 0s to front so it is the same length as the mask
while (value.length < mask.length) {
value = '0' + value
}
// run the value thru the mask and convert back to a Number
let result = parseInt(mask.map((m,i)=>m=='X'?value[i]:m).join(''),2)
// store the result
mem[index] = result
}
});
// log the total of mem
console.log(mem.reduce((a,b)=>a+b, 0))

65
2020/14/part2.js Normal file
View file

@ -0,0 +1,65 @@
const fs = require('fs');
const input = fs.readFileSync('input.txt', 'utf-8').split('\n').map(l=>l.split(' = '));
let mask = [];
let mem = new Map();
let memo = new Map()
let runningTotal = 0
input.forEach((command,i) => {
if (command[0] == 'mask') {
// reset the mask and memorised values from that mask
mask = command[1].split('');
memo = new Map();
} else {
let value = Number(command[1]);
let index = Number(command[0].substring(4,command[0].length-1)).toString(2);
while (index.length < mask.length) {
index = '0' + index;
}
let addresses = [...address(index.split(''))]
addresses.forEach(add => {
// keep a running total instead of adding it up at the end by adding the value and subtracting the value that it replaces
runningTotal += value - (mem.get(parseInt(add, 2))||0)
mem.set(parseInt(add, 2), value)
})
}
});
console.log(runningTotal)
// calculate all addresses from a given address after the mask is applied
function address(loc, i = 0, found = new Set()) {
// reached the end of the address
if (i >= mask.length) {
// add the new address
return found.add(loc.join(''))
}
// if I have already seen this address ending
let key = loc.slice(i).join('')
if (memo.has(key)) {
// add all the addresses that result from this ending
let prefix = loc.slice(0,i).join('');
[...memo.get(key)].forEach(suffix => {found.add(prefix+suffix)});
return found
}
// apply the mask from point i and get all possibilities into the array rem
let rem = []
switch (mask[i]) {
case '1':
loc[i] = '1'
case '0':
rem = [...address([...loc],i+1,found)]
break;
case 'X':
loc[i] = '0';
rem = [...address([...loc],i+1,found)]
loc[i] = '1';
rem.push(...[...address([...loc],i+1,found)])
break;
default:
console.log('oops, bad mask at index', i)
}
// store the possibilities in the memo in case we come across this pattern again.
memo.set(key, new Set(rem.map(o => o.slice(i))))
return found
}

4
2020/14/test1.txt Normal file
View file

@ -0,0 +1,4 @@
mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
mem[8] = 11
mem[7] = 101
mem[8] = 0

4
2020/14/test2.txt Normal file
View file

@ -0,0 +1,4 @@
mask = 000000000000000000000000000000X1001X
mem[42] = 100
mask = 00000000000000000000000000000000X0XX
mem[26] = 1

View file

@ -54,3 +54,4 @@ if (advent) {
11. [:star: :star:](https://adventofcode.com/2020/day/11 "see puzzle")
12. [:star: :star:](https://adventofcode.com/2020/day/12 "see puzzle")
13. [:star: :star:](https://adventofcode.com/2020/day/13 "see puzzle")
14. [:star: :star:](https://adventofcode.com/2020/day/14 "see puzzle")