diff --git a/2020/14/example b/2020/14/example new file mode 100644 index 0000000..e15150a --- /dev/null +++ b/2020/14/example @@ -0,0 +1,4 @@ +mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X +mem[8] = 11 +mem[7] = 101 +mem[8] = 0 diff --git a/2020/14/example.2 b/2020/14/example.2 new file mode 100644 index 0000000..2e961e3 --- /dev/null +++ b/2020/14/example.2 @@ -0,0 +1,4 @@ +mask = 000000000000000000000000000000X1001X +mem[42] = 100 +mask = 00000000000000000000000000000000X0XX +mem[26] = 1 diff --git a/2020/14/input b/2020/14/input new file mode 100644 index 0000000..eadc753 --- /dev/null +++ b/2020/14/input @@ -0,0 +1,572 @@ +mask = 01X11X10X10110110X111X11010X1X101010 +mem[19409] = 3025 +mem[40104] = 798480382 +mem[25359] = 905 +mask = 01011X111100XX1100X1X10X110000000000 +mem[55479] = 930785 +mem[25548] = 130263864 +mem[60518] = 202648 +mem[11955] = 1138 +mem[45248] = 753 +mask = 00XX10001XXX00101X1XX1101000010X0010 +mem[2050] = 27965 +mem[5662] = 110507779 +mem[60807] = 1608 +mask = 000110101100XX10X01000X010X000010100 +mem[28713] = 1039 +mem[22733] = 182274602 +mem[21460] = 12248397 +mem[60257] = 103 +mem[13722] = 137279 +mem[2117] = 208446548 +mask = 100110111X011X1X10110X11010001X11XX0 +mem[29709] = 6606 +mem[27812] = 143567051 +mem[3595] = 478522065 +mem[13123] = 7445318 +mem[37070] = 32452 +mem[60140] = 47608 +mem[21316] = 69201021 +mask = X10X0010110000111111X0X11X10X1000111 +mem[29528] = 3980 +mem[41054] = 274606 +mem[34884] = 265241899 +mem[7496] = 228368 +mem[35014] = 109110 +mem[40696] = 172101503 +mask = 010X011010X0001X11101011X1X100010100 +mem[23210] = 3864280 +mem[53761] = 5046 +mem[6853] = 1214 +mem[45297] = 219 +mem[33797] = 1843462 +mask = 01011011110011110X0X01110110010XX000 +mem[15509] = 16472647 +mem[19332] = 526724681 +mask = 0101X0101100X01X111X1X11X000X1X1X110 +mem[16393] = 122368236 +mem[18852] = 4351408 +mem[56526] = 780 +mem[46701] = 31085562 +mem[53459] = 47134 +mem[19409] = 7629114 +mem[47891] = 76573711 +mask = 000110X0010010X0X010XX110000X0X1000X +mem[35342] = 1095 +mem[56466] = 3462270 +mem[31124] = 204698678 +mem[104] = 2115 +mem[22733] = 154721 +mask = 00X100X01XX000101X1001110000010XX110 +mem[44047] = 6823624 +mem[11955] = 242152 +mem[41039] = 515174779 +mask = 0101101X110X10110X11101011X00110XXXX +mem[46716] = 52535895 +mem[20578] = 851818 +mem[7307] = 1658 +mem[59777] = 7566837 +mem[38136] = 6 +mem[18835] = 95379 +mem[37574] = 28195477 +mask = 010XXX101X0X0011X1100X1001110X01X100 +mem[17831] = 4467 +mem[45439] = 61064 +mem[43070] = 291981105 +mem[9562] = 725 +mem[18574] = 82455219 +mem[53761] = 2223 +mask = 0001X010X1001010XX10011110000011000X +mem[24954] = 229575 +mem[49643] = 118597 +mem[40891] = 218656479 +mem[6549] = 655 +mem[16413] = 105021 +mask = 01X0X01010010X111X1001X0X1X1X0000110 +mem[24722] = 3306225 +mem[18574] = 2046331 +mem[51991] = 2879 +mask = 010X10001X00XX10000000101100000X0000 +mem[64694] = 69415191 +mem[42979] = 1589 +mem[49282] = 199 +mem[3987] = 2386 +mem[64631] = 17661 +mask = 011XXX1010010111101X0000100100X10XX0 +mem[21201] = 341993 +mem[61134] = 328 +mem[19716] = 463044 +mem[53800] = 23668576 +mem[46317] = 368717 +mem[3978] = 2699 +mask = X10X01X011001011X111101010X0XX111111 +mem[5567] = 9644235 +mem[50029] = 4717 +mem[34043] = 119207 +mem[35949] = 665131137 +mem[58233] = 98752 +mask = 0X00001011X0001111X000100X0000XX0110 +mem[28818] = 809 +mem[20113] = 604 +mem[58178] = 11229 +mem[9389] = 38294680 +mem[34657] = 8016112 +mem[10161] = 1585984 +mem[8020] = 1403857 +mask = 01011011110XX0110111111101100X000X01 +mem[47451] = 201798 +mem[62498] = 61888 +mem[49564] = 16728 +mem[60513] = 23392513 +mem[36774] = 56575 +mem[22431] = 70709 +mask = 011X100011000X1X10X0101100X10X011111 +mem[41938] = 34906 +mem[62853] = 221817 +mem[50173] = 471027372 +mem[55286] = 4561108 +mask = 1X010X0011010000X1100X000101X101X000 +mem[22998] = 62382 +mem[18574] = 57889052 +mem[57700] = 1018 +mask = X1XX0X1011001011111X101X111001111111 +mem[34453] = 6483 +mem[49122] = 391290 +mask = 01010000000100110XX000011X00X01XX10X +mem[23870] = 19517 +mem[24291] = 616878 +mem[12134] = 1990123 +mem[26637] = 55962054 +mem[47968] = 712481177 +mem[20878] = 242502 +mem[30722] = 1568 +mask = 11X11111X100XX1000100111100000000010 +mem[35335] = 59630 +mem[63185] = 11256526 +mem[30722] = 266092278 +mem[9776] = 63532545 +mask = 10010010010X1010111X11001000X110101X +mem[12312] = 4029860 +mem[27593] = 42705942 +mem[46156] = 27895 +mem[64088] = 1408576 +mem[50342] = 15980145 +mem[46315] = 29427 +mem[47451] = 18865 +mask = 011111001X0XX01010100110X11000X01100 +mem[1293] = 837 +mem[29000] = 10697 +mask = XX0X0010X1001010XX11X01110X101101100 +mem[42918] = 1028 +mem[2608] = 3093 +mem[21904] = 6098595 +mem[41278] = 1039294 +mem[53102] = 858102784 +mask = 0X0110X01100X010X0X0001X10X00XX00000 +mem[20578] = 860568571 +mem[52466] = 143792 +mem[10261] = 182 +mask = 0X0110101100X011XX111X111110111X11XX +mem[5752] = 37841428 +mem[31368] = 1094581 +mask = 000X11101X01001110100001000X0100100X +mem[56372] = 1046359 +mem[19541] = 315663570 +mem[37436] = 437380 +mem[54334] = 241690746 +mem[16559] = 3127549 +mem[59609] = 176914004 +mask = 110101X01XX1000X0XX0100000X0000XX101 +mem[1033] = 8174 +mem[49587] = 107787 +mem[1572] = 430 +mem[49873] = 115828687 +mem[24389] = 2707208 +mem[30170] = 91827506 +mask = 01XX01001XX10010X0101001000000011101 +mem[26015] = 198698 +mem[50136] = 19382 +mem[16413] = 103882 +mem[8340] = 2066093 +mask = XX01101X1101101XX01111X01X1100X00100 +mem[42378] = 518494 +mem[13927] = 94055 +mem[48225] = 15652034 +mask = 000100X0110X101X0X1XXX1X10110011101X +mem[65006] = 6769 +mem[46625] = 5473325 +mem[22440] = 617624684 +mem[24954] = 719974 +mem[53626] = 62067 +mask = 00011XX01101X01X1010X11X10000X00X0X0 +mem[43072] = 106139234 +mem[53459] = 26813614 +mem[31162] = 184146764 +mask = 0X01X0101X00001XXX10X11X000001000010 +mem[5467] = 121320 +mem[63724] = 11067492 +mem[57246] = 315 +mem[16413] = 2008242 +mem[10240] = 11073 +mem[24282] = 618660016 +mask = 1101111011000010101X1101X0X0001X1011 +mem[61598] = 627237127 +mem[16057] = 235475116 +mem[5662] = 6226 +mem[61721] = 26023344 +mem[58178] = 209547 +mem[59687] = 141941 +mem[9548] = 1392254 +mask = 00011X1000X01010011X01111X0110X10100 +mem[13185] = 300556 +mem[51203] = 17097 +mem[59687] = 8787507 +mem[12337] = 124607 +mem[46043] = 4378256 +mask = 1100X100110100100110X10X000X01111101 +mem[17458] = 420459 +mem[46315] = 142385 +mem[7273] = 58415 +mem[49604] = 57549 +mem[33375] = 12460422 +mem[61540] = 7752 +mask = X1X11XXX1100X010X0100100000000X00010 +mem[28889] = 13691764 +mem[27546] = 355436 +mem[45337] = 10614 +mem[64088] = 960 +mem[39291] = 3019 +mem[30722] = 1976602 +mem[18725] = 299 +mask = 0X111X1011000X101010X0X001X100XX1010 +mem[5532] = 413573 +mem[7707] = 78463710 +mem[46156] = 25164851 +mem[17354] = 15295191 +mask = 000110100X0X10100X100011XX0010010000 +mem[18725] = 7003 +mem[49536] = 49752 +mem[33519] = 116272721 +mem[46701] = 253380665 +mask = 000X001001001XXX101XX00X0101X1010000 +mem[57459] = 792510 +mem[10350] = 217210394 +mem[43612] = 178868 +mem[2374] = 42534899 +mem[40891] = 621 +mem[7270] = 1014999 +mem[49038] = 1657373 +mask = 00XX10100001100X01000110001001010X10 +mem[7270] = 1904 +mem[33267] = 171621958 +mem[42531] = 623 +mask = 011111001100001X00X0010X10010010001X +mem[59756] = 19646 +mem[45248] = 182118 +mem[49395] = 186 +mem[46043] = 1875998 +mem[42378] = 2150393 +mem[16423] = 449813446 +mask = 00X10X1001001010111X0X11100XX0101X01 +mem[42378] = 11316 +mem[6217] = 448726 +mem[56349] = 105698 +mem[18523] = 6560236 +mask = 0X01101011000X101111X1110010X1110111 +mem[57685] = 1052364113 +mem[42200] = 1624 +mem[64281] = 162750 +mem[53459] = 900417618 +mem[44010] = 311326 +mem[38385] = 168338 +mem[64234] = 715 +mask = 0110001010X1001X111000XX01X110X1000X +mem[46270] = 413222 +mem[20358] = 301418973 +mask = 0101X01010000X1X111001000X010000X100 +mem[25549] = 9478586 +mem[27938] = 186993583 +mem[10014] = 630139 +mem[50316] = 22183454 +mask = 00X10XX0110010100X10XX11X01000100010 +mem[51762] = 575 +mem[39895] = 33305 +mem[19768] = 31036515 +mem[30918] = 522221 +mem[26371] = 790132 +mem[43705] = 13814 +mask = 00XXX010X1100010100101110101X0001110 +mem[12495] = 2801000 +mem[43811] = 35764 +mem[59173] = 235362 +mem[50677] = 13747007 +mem[47458] = 49520 +mask = 00X1101X1100X010X010001010000000010X +mem[19737] = 309 +mem[10289] = 1391 +mem[44222] = 202053013 +mem[32818] = 57015 +mask = XX111010X10XX0101010XX00000X00100X01 +mem[25062] = 92115406 +mem[40507] = 8539848 +mem[6853] = 1555113 +mem[59566] = 55734 +mem[29440] = 3860 +mem[2339] = 1687 +mask = X0X100100100X000X0011000100100110001 +mem[62983] = 496942 +mem[55239] = 31959819 +mem[23037] = 185 +mem[14426] = 11052660 +mem[59756] = 11483028 +mask = 0X1X101011X0101X0X100110100110X101XX +mem[9761] = 26687118 +mem[152] = 1818 +mask = 01X110X01010001X1110XXX0000100X100X0 +mem[46327] = 780262 +mem[11424] = 1003003 +mask = 010011XX100X0011X1XX000X01X10X011110 +mem[40928] = 10697 +mem[989] = 22449916 +mem[9034] = 107225 +mask = 010100X0X001001101100XX110X0000X10X0 +mem[50403] = 60110 +mem[1465] = 9126 +mem[4598] = 348452 +mem[26661] = 17672110 +mask = 00X110101XX00010100X0010010011X10010 +mem[38295] = 20183 +mem[61069] = 22691 +mem[51394] = 7278100 +mem[18711] = 34474 +mem[52888] = 1962576 +mask = 1001001X0X001010101X0110XXX001XX100X +mem[33226] = 3641501 +mem[2376] = 72068973 +mem[57257] = 11382653 +mem[22489] = 47282 +mem[45359] = 38362 +mask = 010X1X10010X101X01111010111110111011 +mem[26980] = 755824 +mem[47763] = 385 +mem[23332] = 30083831 +mem[32975] = 61896119 +mask = XX0110111101X011X011X0XXX10000011100 +mem[29709] = 711754376 +mem[36513] = 71516 +mem[7293] = 5061813 +mem[60256] = 419151 +mask = 0101101011000XX1111111X001100100XX00 +mem[19475] = 35852 +mem[57183] = 6494332 +mem[1327] = 872346 +mem[2543] = 943 +mem[2188] = 868813 +mem[29387] = 209125695 +mask = 00X1X0101X1000X01X010X1X01100X0X1101 +mem[51955] = 196066365 +mem[38207] = 15671526 +mem[26980] = 75520251 +mem[11077] = 161630247 +mem[26456] = 30666501 +mem[19737] = 9386 +mask = 1101011010110X0X01101XX001X0X001X001 +mem[49292] = 858273 +mem[11497] = 884831 +mem[49282] = 93065 +mem[54031] = 862594 +mask = 0X01011010X1X01111X0X0X00X11X1010100 +mem[58536] = 4031842 +mem[11621] = 155458283 +mem[8786] = 12859 +mask = 00010010XXX000X010010X1XX001X0X01100 +mem[45429] = 122467 +mem[57256] = 759 +mem[3687] = 384128816 +mem[56464] = 10758724 +mem[11869] = 652805159 +mem[50173] = 75914445 +mask = 0001001001001010111XX10X1X0XX0111101 +mem[5809] = 743780 +mem[52067] = 806 +mem[12750] = 22132 +mem[13019] = 654 +mask = 00X110X01100101X00100X1X00XX00100001 +mem[2117] = 4067660 +mem[2068] = 9851885 +mem[48662] = 52185630 +mem[24246] = 72048 +mem[25978] = 5182633 +mask = 010X1010X0X0001111100X001101X01X0XX0 +mem[10242] = 1118 +mem[57601] = 525 +mem[38099] = 930509 +mask = 10111101110X0X10001001X00001001000X1 +mem[50741] = 59787235 +mem[1588] = 218533 +mem[33080] = 182579 +mem[3978] = 1591079 +mem[62070] = 15472 +mask = 0101100X1100X0X10110X010000X000X00X1 +mem[3814] = 788124 +mem[63265] = 215577374 +mem[57364] = 1311024 +mem[36364] = 3985 +mem[18564] = 4526 +mem[23647] = 376609 +mask = 001XX00011010010111000101X100X0011X0 +mem[61429] = 6896 +mem[22094] = 1966698 +mask = X00110111101XX111011101X011010100X01 +mem[36214] = 310642 +mem[59733] = 1160 +mem[10909] = 1820 +mem[25225] = 51102962 +mem[35074] = 514484736 +mem[21460] = 3630 +mask = 1001001X01001010XX10111001XX1X1X1000 +mem[53608] = 6145 +mem[44618] = 302105 +mem[50955] = 12609449 +mem[48282] = 22035626 +mask = 01X1XX001XXX001XX0100111000000001100 +mem[47458] = 4534 +mem[26444] = 4150059 +mem[10366] = 1061 +mem[51657] = 2817023 +mem[35995] = 1064419 +mem[38295] = 148703436 +mask = 01X0001010010X1110X0000010111X001000 +mem[26046] = 2672378 +mem[1080] = 682 +mem[2151] = 737 +mask = 110X0100110100X001X0X1XXX100011101X0 +mem[56044] = 527135884 +mem[39296] = 107094645 +mem[61785] = 1261 +mask = 01001X00100X0011010000000101000XX101 +mem[54752] = 16579540 +mem[36330] = 1696582 +mem[1435] = 240113842 +mem[49758] = 7811 +mem[51729] = 2543212 +mem[10909] = 13139 +mask = 0X0XXX100001101X011010101000011001XX +mem[58487] = 9986 +mem[7175] = 3371969 +mem[14294] = 10275 +mem[36225] = 13168 +mem[7934] = 48879 +mem[47891] = 1571293 +mem[18711] = 399 +mask = X1010XX01X01001X01101010X1X00001X010 +mem[62247] = 18380710 +mem[20715] = 15548870 +mem[61924] = 28821546 +mem[40119] = 181518508 +mem[50251] = 59934 +mask = XXX1011X11X100X1X110001011X001100001 +mem[13627] = 4734 +mem[36208] = 48295 +mem[37672] = 184327969 +mem[60518] = 9137 +mem[46168] = 105126453 +mask = 1X111101110X0010001000XX000100XXX011 +mem[4455] = 17333982 +mem[58592] = 931411 +mem[61752] = 198443 +mem[183] = 808 +mask = XX010010010XX0101011111111010011X100 +mem[6221] = 256009562 +mem[3528] = 422478 +mem[16002] = 6328770 +mask = 0X01X00011001010XX000010100000100010 +mem[31570] = 32237 +mem[14971] = 846258186 +mem[18978] = 1202 +mem[15368] = 120674 +mem[13185] = 22420 +mask = 010110111101101XX011X11XX11X00X00X00 +mem[59330] = 628 +mem[9283] = 58883 +mem[44010] = 387833048 +mask = 010010101001X1111010111X000X00X0X110 +mem[50633] = 751888 +mem[11056] = 31979 +mem[50741] = 4724 +mem[40028] = 7336181 +mem[42263] = 6863 +mask = 0001X010110000X00010010X10000XX10100 +mem[20546] = 8708 +mem[36908] = 234294 +mem[63185] = 1408 +mem[57531] = 1054 +mem[13722] = 1045167819 +mem[4617] = 3519205 +mask = 00111000X10010X10010X01010X00010010X +mem[46693] = 4740 +mem[17824] = 884 +mem[54997] = 339096 +mem[2117] = 26803 +mem[20] = 84635057 +mask = 0101XXX01100X011011X010000000X00X100 +mem[40142] = 140297 +mem[53459] = 5575659 +mem[57435] = 69641959 +mem[28563] = 433881 +mem[59188] = 230341 +mem[26483] = 151116806 +mask = 110101X01XX100X10110001XX1X0X01000X1 +mem[37526] = 454911 +mem[35793] = 3340 +mem[54537] = 630806 +mem[58456] = 115228 +mem[59172] = 13363 +mask = 010110X0110000101010X001111X0X010010 +mem[45969] = 1736711 +mem[2487] = 610736260 +mem[54173] = 453 +mem[55144] = 2748104 +mem[52466] = 6109568 +mem[31890] = 3977366 +mask = 01011001110000X101100X00X00X10010000 +mem[64336] = 3971537 +mem[60265] = 85267 +mem[24597] = 7455656 +mem[34924] = 703390248 +mem[40391] = 328312 +mem[49255] = 1117186 +mask = 110X0100101100X1011010101X0X11X11010 +mem[31313] = 789405 +mem[7934] = 1803 +mem[36190] = 1836611 +mask = X0010010010010101X1X1X1010X111X010XX +mem[4017] = 64210 +mem[40696] = 58930789 +mem[18166] = 195479433 +mem[41257] = 40207 +mem[40948] = 1058796 +mem[30803] = 486 +mem[29709] = 72337 +mask = 000X101000X110X001X00110X1X0011X0XX1 +mem[11077] = 1653 +mem[2376] = 77389 +mem[19561] = 4876923 +mem[32851] = 538156 +mem[38532] = 1110 +mask = X101101011X010111110X0111001X101010X +mem[44618] = 13980 +mem[10366] = 711247 +mem[4942] = 74171 +mem[25306] = 350133 +mem[21409] = 27748 +mask = 00011010X1001010XXX0001110000001011X +mem[5809] = 1082 +mem[36908] = 180 +mem[59172] = 196430412 +mem[60137] = 1388 +mem[49643] = 1887 +mask = 01011010X10000X1111111111XX00101111X +mem[56344] = 1237 +mem[23638] = 37922654 +mem[20307] = 593227321 diff --git a/2020/14/solution1.js b/2020/14/solution1.js new file mode 100644 index 0000000..68a96aa --- /dev/null +++ b/2020/14/solution1.js @@ -0,0 +1,46 @@ +const fs = require("fs"); + +const input = fs.readFileSync("input", "utf-8").trim().split("\n"); + +function main() { + // original mask string + let mask = null; + const memory = []; + for (const line of input) { + if (line.startsWith("mask")) { + mask = line.slice(7); + } else { + const address = parseInt(line.slice(4)); + const num = BigInt(parseInt(line.split(" = ")[1])); + memory[address] = apply(num, mask); + // debug(num, mask); + } + } + const sum = memory.reduce((a, b) => a + b, 0n); + console.log("Solution:"); + console.log(sum.toString()); +} + +function debug(value, mask) { + const result = apply(value, mask); + console.log("=".repeat(18), " DEBUG ", "=".repeat(18)); + console.log(`value: ${numToBin(value)} (decimal ${value})`); + console.log(`mask: ${mask}`); + console.log(`result: ${numToBin(result)} (decimal ${result})`); +} + +function numToBin(num) { + return num.toString(2).padStart(36, "0"); +} + +function binToNum(bin) { + return BigInt(parseInt(bin, 2)); +} + +function apply(num, mask) { + num = num & binToNum(mask.replaceAll("X", 1)); + num = num | binToNum(mask.replaceAll("X", 0)); + return num; +} + +main(); diff --git a/2020/14/solution2.js b/2020/14/solution2.js new file mode 100644 index 0000000..2869dc3 --- /dev/null +++ b/2020/14/solution2.js @@ -0,0 +1,68 @@ +const fs = require("fs"); + +const input = fs.readFileSync("input", "utf-8").trim().split("\n"); + +function main() { + // original mask string + let sum = 0n; + let mask = null; + const memory = []; + for (const line of input) { + if (line.startsWith("mask")) { + mask = line.slice(7); + } else { + const sourceAddress = BigInt(parseInt(line.slice(4))); + const num = BigInt(parseInt(line.split(" = ")[1])); + const addresses = apply(sourceAddress, mask); + for (const address of addresses) { + if (typeof memory[address] !== "undefined") { + sum -= memory[address]; + } + memory[address] = num; + sum += num; + } + // debug(sourceAddress, mask, addresses); + } + } + console.log("Fill memory done"); + console.log("Solution:"); + console.log(sum.toString()); +} + +function debug(address, mask, addresses) { + console.log("=".repeat(18), " DEBUG ", "=".repeat(18)); + console.log(`address: ${numToBin(address)} (decimal ${address})`); + console.log(`mask: ${mask}`); + console.log(`result: `); + for (const address of addresses) { + console.log(`${numToBin(address)} (decimal ${address})`); + } +} + +function numToBin(num) { + return num.toString(2).padStart(36, "0"); +} + +function binToNum(bin) { + return BigInt(parseInt(bin, 2)); +} + +function apply(address, mask) { + address = address | binToNum(mask.replaceAll("X", 0)); + address = numToBin(address); + let addresses = [""]; + for (let i = 0; i < address.length; i++) { + const bit = address[i]; + const maskBit = mask[i]; + if (maskBit === "X") { + const zeroConcated = addresses.map((adr) => adr + "0"); + const oneConcated = addresses.map((adr) => adr + "1"); + addresses = [...zeroConcated, ...oneConcated]; + } else { + addresses = addresses.map((adr) => adr + bit); + } + } + return addresses.map(binToNum); +} + +main();