diff --git a/day14/Cargo.toml b/day14/Cargo.toml new file mode 100644 index 0000000..d17d08b --- /dev/null +++ b/day14/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day14" +version = "0.1.0" +authors = ["henine "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day14/input b/day14/input new file mode 100644 index 0000000..f04b554 --- /dev/null +++ b/day14/input @@ -0,0 +1,551 @@ +mask = 01101X001X111X010X0000X1001X010XX0X0 +mem[4841] = 3942 +mem[9168] = 414370178 +mask = 0110010011X1X1X0000000XX0110000XX111 +mem[33062] = 288 +mem[20612] = 182 +mask = 11X00100011111110000X1010X001X010000 +mem[8509] = 140 +mem[17752] = 2606028 +mem[49078] = 5406 +mem[51242] = 109058175 +mem[32524] = 5834185 +mem[18889] = 416675 +mask = X1100X00111111X0000000X1XX1X1X000000 +mem[20551] = 7515 +mem[17979] = 6511051 +mask = 111000X0111X111110000X0X01X1X0010001 +mem[63807] = 65801880 +mem[38229] = 1958228 +mem[60238] = 10317226 +mem[8245] = 37777 +mem[13328] = 106103916 +mask = 0110001111XXXX10X010X00101XX01000111 +mem[2637] = 6952795 +mem[29819] = 10089122 +mem[47364] = 3403584 +mem[59421] = 16626230 +mem[10381] = 23975082 +mask = 1X100X101110X11110101100X10101100001 +mem[47584] = 7588784 +mem[7998] = 209420582 +mem[2215] = 101018542 +mem[10326] = 33499943 +mask = 010011001111XX01010101XX00110X100X00 +mem[1380] = 14157477 +mem[14979] = 3077 +mem[56589] = 65261 +mem[50403] = 966 +mem[54831] = 485513 +mem[15967] = 490311105 +mask = 01XX11001110010100010X001X1X11011101 +mem[32012] = 415 +mem[23771] = 395310 +mem[66] = 186902 +mask = 1X10X110X1X01X1X10000X001X0100X00000 +mem[37189] = 834 +mem[59763] = 64348057 +mask = X1100100111XX1X1X00X001101XX0100001X +mem[12021] = 123589 +mem[150] = 691812 +mem[1988] = 4844 +mem[14979] = 866 +mask = 11X001001X1X101X0X0X0010111000X00010 +mem[1988] = 184319453 +mem[30263] = 3126 +mem[36910] = 56981746 +mem[14016] = 25485783 +mask = 1XX010X01110X110100000X11101X10X0X11 +mem[3513] = 1787222 +mem[11652] = 13761 +mem[25508] = 235920 +mem[49386] = 7440645 +mem[51287] = 197564380 +mem[9697] = 1812 +mem[62638] = 5207143 +mask = X01X110X1101010101101110X01010100010 +mem[35711] = 311172 +mem[11538] = 92602 +mem[56210] = 147702 +mem[36910] = 3677 +mem[46719] = 80249 +mask = X100X10010110101000X001X100111100110 +mem[52498] = 7142 +mem[55838] = 108173611 +mem[8836] = 3620232 +mem[52288] = 1254336 +mask = 1110X11X11001111100000001X00X1100X01 +mem[53505] = 2320049 +mem[34984] = 22763 +mem[48172] = 484320 +mem[2670] = 334861 +mask = X1100010X1X0111100X0XX00011110010100 +mem[19607] = 60274605 +mem[15403] = 4876 +mem[50026] = 155030489 +mem[54997] = 3872114 +mem[45248] = 6299950 +mem[8245] = 1107 +mask = 10100X0X110111X110X00001101101100X00 +mem[36085] = 6621 +mem[57943] = 47436 +mem[12709] = 21928466 +mem[21724] = 534 +mem[44782] = 460274 +mem[62043] = 447 +mem[48293] = 5706496 +mask = 111X0100XX101110X00010XX110X101000X1 +mem[61318] = 29864 +mem[384] = 3350802 +mem[85] = 63068 +mem[60071] = 760978 +mem[44357] = 15965 +mask = 01X0X100X111X10X0X0X0001XX1011000000 +mem[15426] = 479567220 +mem[1988] = 8279 +mem[21835] = 43376 +mem[1257] = 99466 +mem[56118] = 29525895 +mask = XX1011X011X1X101011000XX1X100X110000 +mem[51649] = 1040282 +mem[36903] = 40633 +mem[52146] = 50010 +mem[41230] = 1549 +mem[2090] = 61683 +mem[45237] = 4280 +mem[4116] = 639494 +mask = 1X10XX00X11X010110000010010011000110 +mem[32858] = 333331 +mem[12610] = 3307080 +mem[13553] = 849370649 +mem[12442] = 49072 +mask = 11100100X111X11100000110000X110000X1 +mem[11661] = 26500 +mem[48770] = 566036264 +mem[25452] = 6655520 +mem[20604] = 4661 +mem[2240] = 30475 +mem[5992] = 645113282 +mask = 0110010001X1010001001X010111011XX010 +mem[16596] = 38553734 +mem[8245] = 288 +mem[6256] = 2527 +mem[8779] = 392795412 +mask = 1010X1XX110X111110001X00000100100101 +mem[40037] = 3557515 +mem[40660] = 4025046 +mem[12007] = 26303 +mem[25863] = 1501 +mem[51197] = 34569606 +mask = 011X1X010010110100010XXXXX111X1100X0 +mem[59421] = 256710 +mem[8848] = 14645067 +mem[20604] = 1810 +mem[3814] = 227 +mask = 111110X00110X010100X0XX110110X11X011 +mem[47599] = 1290920 +mem[21877] = 118276 +mem[51248] = 87777 +mask = 1110X01011101X1010XX0XX11XX0X1110011 +mem[66] = 13465 +mem[44079] = 1000 +mem[5533] = 235749681 +mem[47599] = 90145 +mem[15331] = 828346025 +mem[38599] = 38093 +mem[9618] = 1155 +mask = 0111X0101110X111001010011011000XX001 +mem[51392] = 178163349 +mem[34584] = 3043180 +mem[2401] = 37940538 +mem[5932] = 13078 +mem[60430] = 32668784 +mem[45249] = 13789 +mem[64444] = 98 +mask = 111X0100111X111XX0000XX0X00001X00X11 +mem[37189] = 535092671 +mem[37165] = 365784 +mem[27008] = 24184267 +mem[30336] = 211964 +mask = 0111000101X0110100010010X1X001X0X00X +mem[1297] = 262172173 +mem[15250] = 7218166 +mem[3921] = 576305160 +mem[5238] = 66721 +mask = X100X100X01X010X010100010XX1010X01X0 +mem[64159] = 3372817 +mem[47010] = 13564 +mem[19734] = 218293629 +mem[13014] = 7811770 +mem[16596] = 347 +mask = 0110010X111001110X0X00X1100X100000X0 +mem[23241] = 101885 +mem[5238] = 416 +mem[3862] = 1633802 +mask = 111XX11011X0X1X1100000X011X000000011 +mem[12021] = 8163882 +mem[15827] = 136279370 +mem[15875] = 111 +mem[5991] = 178004 +mask = X0111010011X111XX00011110010X1110011 +mem[51143] = 111175 +mem[31811] = 70891 +mem[60840] = 13218 +mem[46324] = 5020 +mask = 1010101011101X10100000X1011X1X100111 +mem[85] = 2036268 +mem[32739] = 8222467 +mem[50840] = 27115578 +mem[46591] = 11383922 +mem[56462] = 3203407 +mask = 111000101110X01010XX11001100011X1XX0 +mem[54079] = 6935 +mem[62079] = 2679 +mask = 10X000111X1011X1101001XX11010010X101 +mem[5191] = 7928110 +mem[31654] = 13131527 +mem[30979] = 6724 +mask = 1X1000X11X1011X0X0100X0111010000X011 +mem[85] = 67719 +mem[35174] = 418373 +mem[51173] = 6061932 +mem[48747] = 27919 +mem[9961] = 16824594 +mask = XX10X10X111001011000X0X000111X000110 +mem[2215] = 12662 +mem[53156] = 31725 +mem[21956] = 18506440 +mem[5954] = 892 +mask = 0100010X1X1101000101X0X10X0001001X01 +mem[14048] = 1256291 +mem[33489] = 74344679 +mem[31942] = 21958 +mem[12007] = 1670564 +mem[53466] = 11991 +mem[33529] = 435881689 +mem[12709] = 77367498 +mask = 0X1100111X1000101X1X1XX1X000010011X0 +mem[20604] = 745 +mem[16901] = 55323 +mem[31026] = 311133 +mem[287] = 2850714 +mem[61333] = 478308 +mem[64074] = 493143 +mask = XX10001X1X10111XX010X0011101001000X1 +mem[5991] = 7705 +mem[54597] = 3447346 +mem[49386] = 28438 +mem[36040] = 56052820 +mem[20469] = 17271 +mem[62913] = 7732604 +mem[3412] = 6102127 +mask = X1XX1000011011X010X001000X1101X11000 +mem[29733] = 3524 +mem[48770] = 125149 +mem[12829] = 6776326 +mask = 010X010X11111X00000X1X011X101X110110 +mem[48337] = 1746 +mem[85] = 574143 +mem[35824] = 205850239 +mem[38811] = 25425 +mem[57154] = 25695 +mem[45249] = 16800535 +mem[19317] = 5109374 +mask = 1X1000XX1X101X10X01011000001011XX110 +mem[40611] = 1356 +mem[12299] = 2095 +mem[23202] = 2536 +mask = 1110X1001X11111100010X0XX11XX0000101 +mem[37400] = 442610 +mem[34803] = 318 +mem[9031] = 249678 +mask = 1X1110X001101110X00010X11110011XXX00 +mem[2670] = 99927 +mem[49078] = 9463 +mem[2993] = 640 +mem[51455] = 428310 +mem[47995] = 13063939 +mask = 01101XXX1XXX100100000XX0001011011000 +mem[48293] = 1674 +mem[61009] = 500098 +mem[5080] = 45615 +mem[60023] = 49663406 +mem[60905] = 1829076 +mem[28346] = 183 +mask = 11X01X0001XX011010X0000X0001X1X11100 +mem[46718] = 4380 +mem[13394] = 1909430 +mem[29741] = 244218235 +mem[3493] = 211917241 +mem[39030] = 1374 +mask = 0100010XX0110100X10100XX0001100X0X00 +mem[19093] = 385 +mem[121] = 770 +mem[8820] = 235181808 +mem[15967] = 44085381 +mem[16580] = 1088367 +mem[33422] = 919067085 +mask = X0X011XX1X11110X01X0X01X111100111110 +mem[39728] = 1714 +mem[15624] = 417143278 +mask = 00XX0001101011101X1001XX11X1X110001X +mem[17164] = 252516 +mem[15654] = 16043 +mem[3180] = 457215773 +mem[41735] = 831 +mem[5533] = 1251 +mem[20687] = 363018 +mem[14898] = 6896717 +mask = 01101X0X1X111001X0000001000011101011 +mem[29510] = 5445530 +mem[58] = 2005940 +mem[64191] = 161526093 +mem[6578] = 736 +mask = X11011X0010011111000XX10110100010001 +mem[6000] = 7668 +mem[9819] = 51057159 +mem[43871] = 30001900 +mem[27557] = 551910519 +mem[52290] = 845 +mask = 011XX010X1101111X010010X0X111XX100X1 +mem[32012] = 13301157 +mem[2422] = 2346 +mem[51649] = 1026605881 +mask = X11X001X111X001010100XX10X1X01X01001 +mem[64367] = 440 +mem[37189] = 248796 +mem[40449] = 32700 +mem[16556] = 386038740 +mask = XX11X10X1X11111100000011010011XX0010 +mem[15875] = 2785525 +mem[37189] = 441 +mem[55319] = 1225305 +mem[41903] = 199582639 +mask = 1011X0X0X1X011101000X010111XX11X01X1 +mem[20675] = 49758 +mem[12342] = 225305969 +mem[8414] = 247229 +mask = 1100X0001X1001101X000001110100X01011 +mem[5089] = 18944 +mem[35956] = 21989 +mem[3169] = 487520844 +mem[24443] = 623 +mask = X110101X1110111010X00X00100000X0X011 +mem[15824] = 85916 +mem[32710] = 1664755 +mem[1243] = 3977144 +mem[23771] = 4115 +mask = X1101110110101X101X00X1000X001X10100 +mem[55138] = 5280 +mem[36161] = 6353162 +mem[56293] = 47505 +mem[10292] = 8795 +mem[56462] = 10685425 +mask = 0110110X11X11X011100001100X011001110 +mem[27214] = 1155726 +mem[46047] = 18512966 +mem[8386] = 204434879 +mask = 01X0X10X1111X1010X010X0XX0001000X010 +mem[51751] = 40804 +mem[11905] = 11919 +mem[51649] = 40498 +mem[13888] = 345577645 +mem[13303] = 918 +mem[56350] = 53749 +mask = X110X0101110111X0X10010X1X1001110011 +mem[8103] = 129154665 +mem[44036] = 5036 +mask = 0X100XX1101011101010000X110X0X100X1X +mem[25004] = 1499470 +mem[55753] = 34968 +mem[22702] = 1142902 +mem[39030] = 926 +mask = XX1001001X111111000XX001100001000000 +mem[33390] = 110 +mem[57874] = 11128226 +mem[51028] = 1148 +mem[45069] = 482759 +mem[55619] = 74382 +mask = 0110X10011110XX111X00000X0X0X00X0X00 +mem[41006] = 2013 +mem[12442] = 920 +mem[4802] = 4069 +mem[63407] = 29200 +mem[383] = 31063618 +mask = 0X1X01X011X1011111000X0X1XX011000010 +mem[63120] = 457184 +mem[58737] = 318 +mem[5932] = 27437790 +mem[63130] = 14901403 +mem[21088] = 11453101 +mem[4224] = 44650697 +mask = 1X111X1011000101100000111100000000XX +mem[30612] = 231194489 +mem[14292] = 381820395 +mem[51679] = 3770 +mem[28848] = 172440 +mem[5123] = 2223 +mem[50307] = 50412008 +mask = 01XX0100111X0101X000X0XX011111000XX1 +mem[12934] = 6379810 +mem[35678] = 3966282 +mem[44674] = 10698 +mask = 001000111X101XX01X101000010X11XX00X0 +mem[150] = 836348 +mem[17812] = 1567355 +mem[16990] = 36 +mask = 10100X001101X10110X000011X0111000000 +mem[28382] = 6079948 +mem[44054] = 739 +mem[65201] = 113868293 +mem[30979] = 8134 +mem[42593] = 183875466 +mem[54476] = 51841 +mem[44938] = 205 +mask = 1110010011X011X1100X10111101X1100111 +mem[269] = 967723800 +mem[10218] = 806086 +mem[38482] = 76541 +mask = 1X101000X110000010001X000100X0100101 +mem[6139] = 179872491 +mem[55619] = 3084490 +mem[33489] = 18805974 +mem[33720] = 1867 +mask = 1110XXX01110111XX0X00001010001XX0001 +mem[29331] = 7426107 +mem[55557] = 797 +mem[37189] = 126043 +mem[10723] = 62212 +mask = 011011001111X101X1X00XX00X100100XX10 +mem[49078] = 57265156 +mem[4802] = 419563 +mem[24177] = 98724 +mem[3708] = 1220 +mask = 010011001X1X01010X010X0X0X11X1X0010X +mem[1156] = 60829 +mem[3611] = 426415170 +mem[15875] = 82238 +mem[19317] = 126875 +mem[30390] = 534080975 +mask = 1110010X1X111XXXX0000011001X01X00010 +mem[8245] = 64067 +mem[4032] = 685799 +mem[29972] = 911 +mask = X11001001110111110001XX110X1011X1111 +mem[59763] = 809103 +mem[38982] = 637 +mem[15766] = 7064924 +mem[26073] = 28058 +mask = X11011X000111X01X000X001110111111000 +mem[39395] = 2147538 +mem[62638] = 60835 +mem[24537] = 14051774 +mem[7587] = 30880 +mem[13737] = 6187454 +mem[61247] = 438831 +mask = 011XXX0XXX101101000100110X1111110X10 +mem[61225] = 41364 +mem[2422] = 6652 +mem[58450] = 13746 +mem[35824] = 368584 +mem[61670] = 18722 +mask = 11110100X110111010001X00010001XX1111 +mem[45069] = 7637 +mem[5972] = 207784 +mem[36269] = 125923 +mask = 11X00X00111111X1000100X011111000001X +mem[19194] = 14408 +mem[36135] = 1777 +mem[9159] = 2558642 +mem[47430] = 21171007 +mem[30390] = 695210 +mask = 0X1011X0XX11100100X000010XXX1101X010 +mem[23225] = 120954910 +mem[7731] = 43441 +mem[54420] = 17279925 +mem[53308] = 319968 +mem[32858] = 700 +mem[12928] = 9385710 +mask = X010010110X011X010XX0101110XX110111X +mem[2502] = 750120 +mem[13231] = 187500162 +mem[13380] = 7025 +mem[23997] = 85647493 +mem[44993] = 7645707 +mem[13771] = 124630838 +mem[46591] = 940782440 +mask = 01X0010011100111XX0000111X0X10110X01 +mem[1325] = 949 +mem[19298] = 943 +mem[15847] = 27961 +mask = 1111010011X011X010001X01000X01XX0111 +mem[269] = 39091531 +mem[44813] = 1679 +mem[31203] = 2018 +mem[52498] = 65627195 +mem[25913] = 10103182 +mem[15766] = 10700 +mask = 111X01001110111X10000X101110000000XX +mem[20675] = 20849753 +mem[55619] = 3881752 +mem[57180] = 4340 +mask = 0X1000100X101111X0X00X11101110XX1101 +mem[21902] = 1166 +mem[2215] = 14172991 +mem[2817] = 167 +mem[45484] = 1582594 +mem[30693] = 333999 +mask = 1X100000111X11110X100010110111X0X011 +mem[29331] = 912 +mem[12131] = 1076046 +mem[15553] = 2914438 +mask = 1110X110110XX0101000100X1001010X0010 +mem[47762] = 5648 +mem[34576] = 18810 +mem[32012] = 2838 +mem[16803] = 31492679 +mem[27243] = 531430088 +mem[43281] = 446489 +mask = 0100X10000110100X101X00001X111000X1X +mem[13396] = 201057 +mem[58199] = 1070284468 +mem[28492] = 776 +mem[4690] = 3285 +mem[38660] = 144569370 +mem[56769] = 3508 +mask = 111X1000X110XXX010000X0101X10110X101 +mem[60825] = 10196 +mem[7331] = 17103725 +mem[25913] = 3665 +mem[33720] = 12442 +mem[56398] = 182436 +mem[22551] = 39589992 +mask = 11XX0100111111X0X0X0110100X101000000 +mem[53178] = 460751 +mem[12299] = 3953 +mask = X0X0XX11111011X01XX010X000111X000110 +mem[50148] = 19839478 +mem[26332] = 145396 +mem[37366] = 1281037 +mem[205] = 41677975 +mem[40745] = 249226 +mem[33529] = 29488129 +mem[15168] = 58169583 +mask = 0X10110XX01111010XX00X000X1011100000 +mem[25461] = 148 +mem[36903] = 116668851 +mem[45581] = 167220499 +mem[5089] = 65669014 +mask = 011X11X011111X0101100X00011011001000 +mem[6869] = 166150610 +mem[4810] = 9250505 +mask = 101000111110111100X0X0X10X000010010X +mem[8103] = 53773 +mem[2805] = 640192 +mem[6481] = 17745 +mask = XX10X10011100X11000100111X110X000XX1 +mem[14014] = 445390635 +mem[5763] = 32807150 +mem[13798] = 32190 +mem[23253] = 317 +mem[53999] = 257358 diff --git a/day14/src/main.rs b/day14/src/main.rs new file mode 100644 index 0000000..f6c4991 --- /dev/null +++ b/day14/src/main.rs @@ -0,0 +1,105 @@ +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::collections::HashMap; + +#[derive(Clone, Debug)] +struct Mask { + mask0: u64, + mask1: u64, + n_x: u32, + p_x: Vec, +} + +fn main() { + let file = File::open("input").unwrap(); + + let input_buffer = BufReader::new(&file); + let lines = input_buffer.lines(); + + let mut memory: HashMap = HashMap::new(); + // let mut mask0 = 0u64; + // let mut mask1 = 0u64; + let mut mask = Mask { + mask0: 0, + mask1: 0, + n_x: 0, + p_x: vec![], + }; + + for rline in lines { + let line = rline.unwrap(); + let mut spline = line.split(" = "); + + let command = spline.next().unwrap().to_owned(); + // let command_prefix = command[0..4].to_owned(); + let param = spline.next().unwrap(); + + match &command[0..4] { + "mask" => { + mask = parse_mask(param); + // mask0 = new_mask0; + // mask1 = new_mask1; + } + "mem[" => memory = update_mem(command, param, &mask, memory), + e => panic!(e.to_owned()) + } + } + + let sum: u64 = memory.iter().map(|(_, v)| v).sum(); + + println!("{}", sum) +} + +fn update_mem(command: String, param: &str, mask: &Mask, mut memory: HashMap) -> HashMap { + let location: u64 = command[4..command.len() - 1].parse().unwrap(); + let parameter: u64 = param.trim().parse().unwrap(); + + // memory.insert(location, (parameter | mask1) & !mask0); + for i in 0..1 << mask.n_x { + let mut address = location & mask.mask0 | mask.mask1; + let mut bi = 0; + for p in mask.p_x.iter() { + address |= ((i >> bi) & 1) << p; + bi += 1 + } + memory.insert(address, parameter); + } + + memory +} + +fn parse_mask(param: &str) -> Mask { + let mut mask0 = 0; + let mut mask1 = 0; + // let mut maskX = 0; + let mut p_x = Vec::new(); + let mut n_x = 0; + + let mut p:i32 = 35; + for char in param.chars() { + match char { + 'X' => { + mask0 <<= 1; + mask1 <<= 1; + p_x.push(p as u64); + n_x += 1; + p-=1 + } + '1' => { + mask0 <<= 1; + mask1 <<= 1; + mask1 += 1; + p -= 1 + } + '0' => { + mask0 <<= 1; + mask0 += 1; + mask1 <<= 1; + p -= 1 + } + e => panic!(e) + } + } + + Mask { mask1, mask0, n_x, p_x } +} diff --git a/day14/test b/day14/test new file mode 100644 index 0000000..fa0dd0a --- /dev/null +++ b/day14/test @@ -0,0 +1,4 @@ +mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X +mem[8] = 11 +mem[7] = 101 +mem[8] = 0 \ No newline at end of file diff --git a/day14/test2 b/day14/test2 new file mode 100644 index 0000000..b4b4e06 --- /dev/null +++ b/day14/test2 @@ -0,0 +1,4 @@ +mask = 000000000000000000000000000000X1001X +mem[42] = 100 +mask = 00000000000000000000000000000000X0XX +mem[26] = 1 \ No newline at end of file