2020-12-14

# Day 14: Docking Data

Description:
--- Day 14: Docking Data ---

As your ferry approaches the sea port, the captain asks for your help again. The computer system that runs this port isn't compatible with the docking program on the ferry, so the docking parameters aren't being correctly initialized in the docking program's memory.

After a brief inspection, you discover that the sea port's computer system uses a strange bitmask system in its initialization program. Although you don't have the correct decoder chip handy, you can emulate it in software!

The initialization program (your puzzle input) can either update the bitmask or write a value to memory. Values and memory addresses are both 36-bit unsigned integers. For example, ignoring bitmasks for a moment, a line like mem[8] = 11 would write the value 11 to memory address 8.

The bitmask is always given as a string of 36 bits, written with the most significant bit (representing 2^35) on the left and the least significant bit (2^0, that is, the 1s bit) on the right. The current bitmask is applied to values immediately before they are written to memory: a 0 or 1 overwrites the corresponding bit in the value, while an X leaves the bit in the value unchanged.

For example, consider the following program:

mem[8] = 11
mem[7] = 101
mem[8] = 0

This program starts by specifying a bitmask (mask = ....). The mask it specifies will overwrite two bits in every written value: the 2s bit is overwritten with 0, and the 64s bit is overwritten with 1.

The program then attempts to write the value 11 to memory address 8. By expanding everything out to individual bits, the mask is applied as follows:

value: 000000000000000000000000000000001011 (decimal 11)
result: 000000000000000000000000000001001001 (decimal 73)

So, because of the mask, the value 73 is written to memory address 8 instead. Then, the program tries to write 101 to address 7:

value: 000000000000000000000000000001100101 (decimal 101)
result: 000000000000000000000000000001100101 (decimal 101)

This time, the mask has no effect, as the bits it overwrote were already the values the mask tried to set. Finally, the program tries to write 0 to address 8:

value: 000000000000000000000000000000000000 (decimal 0)
result: 000000000000000000000000000001000000 (decimal 64)

64 is written to address 8 instead, overwriting the value that was there previously.

To initialize your ferry's docking program, you need the sum of all values left in memory after the initialization program completes. (The entire 36-bit address space begins initialized to the value 0 at every address.) In the above example, only two values in memory are not zero - 101 (at address 7) and 64 (at address 8) - producing a sum of 165.

Execute the initialization program. What is the sum of all values left in memory after it completes?

--- Part Two ---

For some reason, the sea port's computer system still can't communicate with your ferry's docking program. It must be using version 2 of the decoder chip!

A version 2 decoder chip doesn't modify the values being written at all. Instead, it acts as a memory address decoder. Immediately before a value is written to memory, each bit in the bitmask modifies the corresponding bit of the destination memory address in the following way:

If the bitmask bit is 0, the corresponding memory address bit is unchanged.
If the bitmask bit is 1, the corresponding memory address bit is overwritten with 1.
If the bitmask bit is X, the corresponding memory address bit is floating.

A floating bit is not connected to anything and instead fluctuates unpredictably. In practice, this means the floating bits will take on all possible values, potentially causing many memory addresses to be written all at once!

For example, consider the following program:

mem[42] = 100
mem[26] = 1

When this program goes to write to memory address 42, it first applies the bitmask:

result: 000000000000000000000000000000X1101X

After applying the mask, four bits are overwritten, three of which are different, and two of which are floating. Floating bits take on every possible combination of values; with two floating bits, four actual memory addresses are written:

000000000000000000000000000000011010 (decimal 26)
000000000000000000000000000000011011 (decimal 27)
000000000000000000000000000000111010 (decimal 58)
000000000000000000000000000000111011 (decimal 59)

result: 00000000000000000000000000000001X0XX

This results in an address with three floating bits, causing writes to eight memory addresses:

000000000000000000000000000000010000 (decimal 16)
000000000000000000000000000000010001 (decimal 17)
000000000000000000000000000000010010 (decimal 18)
000000000000000000000000000000010011 (decimal 19)
000000000000000000000000000000011000 (decimal 24)
000000000000000000000000000000011001 (decimal 25)
000000000000000000000000000000011010 (decimal 26)
000000000000000000000000000000011011 (decimal 27)

The entire 36-bit address space still begins initialized to the value 0 at every address, and you still need the sum of all values left in memory at the end of the program. In this example, the sum is 208.

Execute the initialization program using an emulator for a version 2 decoder chip. What is the sum of all values left in memory after it completes?

Input:
mem[59869] = 296403277
mem[50029] = 14565
mem[52488] = 627950233
mem[48318] = 113982010
mem[22257] = 82
mem[27870] = 17795
mem[55511] = 4594118
mem[61743] = 13086
mem[3724] = 4029841
mem[10560] = 236422779
mem[28511] = 7957
mem[32609] = 3123
mem[3930] = 26884
mem[42884] = 252696502
mem[18565] = 820
mem[28279] = 105604729
mem[41611] = 1567094
mem[36341] = 5551
mem[35962] = 28056
mem[52299] = 24969
mem[41449] = 160272674
mem[34903] = 14302599
mem[35962] = 71766
mem[44250] = 536
mem[21842] = 494315083
mem[51716] = 3417
mem[37939] = 242985
mem[35845] = 48792746
mem[26794] = 1026647
mem[47814] = 92296
mem[63043] = 3100
mem[26635] = 2181
mem[40164] = 2436842
mem[63001] = 36102371
mem[47565] = 2512786
mem[18403] = 512
mem[58372] = 65318068
mem[53197] = 15875
mem[58632] = 231362257
mem[4095] = 116134614
mem[63365] = 186252884
mem[19024] = 2449
mem[30536] = 1021
mem[49282] = 447504
mem[16658] = 98394885
mem[64187] = 1431
mem[11141] = 1904
mem[39855] = 61495
mem[44407] = 1449308
mem[43902] = 2006
mem[62547] = 3478
mem[3885] = 140726549
mem[3307] = 938307640
mem[22257] = 71231
mem[41243] = 508095
mem[25091] = 24579
mem[22764] = 96570583
mem[36352] = 56148675
mem[38443] = 1755
mem[56360] = 25308
mem[7833] = 785
mem[4380] = 2874
mem[61544] = 144576256
mem[2156] = 1658
mem[26571] = 28977534
mem[43902] = 1032904266
mem[21524] = 463
mem[47123] = 5438
mem[28936] = 39885
mem[55045] = 946386
mem[43630] = 143495580
mem[10560] = 3231
mem[39551] = 524769
mem[37150] = 397326
mem[153] = 2696665
mem[43255] = 44
mem[14355] = 14827
mem[51242] = 445851
mem[40969] = 756383545
mem[26794] = 100307329
mem[48938] = 1863389
mem[2345] = 2535119
mem[53880] = 1759799
mem[8343] = 11387
mem[48589] = 60
mem[27590] = 5929131
mem[3690] = 63744500
mem[520] = 31249543
mem[24387] = 473
mem[21573] = 32235112
mem[50565] = 563
mem[26571] = 3496
mem[3930] = 418
mem[35770] = 7558838
mem[12614] = 1619627
mem[50701] = 111977200
mem[19006] = 10909
mem[16052] = 74134
mem[43094] = 14253
mem[50557] = 608639
mem[59406] = 1422
mem[52383] = 4751812
mem[1669] = 833448227
mem[35841] = 439768021
mem[43630] = 7511
mem[53126] = 343642
mem[60802] = 1256603
mem[49511] = 4982217
mem[51824] = 270
mem[51349] = 1890
mem[59808] = 3773655
mem[57730] = 57401
mem[35630] = 973239
mem[54672] = 138240715
mem[8136] = 10996
mem[55459] = 401980
mem[17187] = 941
mem[41484] = 1242838
mem[29717] = 1145160
mem[27394] = 1108224
mem[2816] = 272760856
mem[32201] = 304518
mem[16394] = 79826575
mem[39941] = 13157
mem[35282] = 760997
mem[63951] = 7011404
mem[8985] = 123284486
mem[34856] = 39388801
mem[18190] = 3208452
mem[32295] = 1729184
mem[48809] = 3708309
mem[17960] = 66227
mem[39063] = 5571972
mem[34523] = 1100265
mem[17724] = 1074235
mem[2309] = 1178
mem[56623] = 5006
mem[41091] = 951374
mem[63942] = 21970475
mem[8120] = 1167
mem[50184] = 5634
mem[63703] = 31429
mem[19083] = 32307
mem[48832] = 3825073
mem[53491] = 953
mem[39752] = 91899271
mem[63089] = 3036
mem[38445] = 356279
mem[40137] = 10955
mem[37568] = 477812
mem[18443] = 85
mem[64998] = 758355504
mem[10506] = 5946
mem[58372] = 800
mem[40606] = 1267021
mem[42753] = 86680
mem[38503] = 9164580
mem[4805] = 898
mem[50969] = 219378
mem[3182] = 14757
mem[26794] = 289
mem[27899] = 67683
mem[60968] = 128881
mem[21049] = 153946
mem[4625] = 6492
mem[13554] = 14536684
mem[49387] = 48190714
mem[5514] = 58395
mem[59861] = 2590
mem[59717] = 706
mem[52288] = 594838
mem[47711] = 256545
mem[37150] = 5576185
mem[12194] = 1010012
mem[55682] = 745
mem[19810] = 54828
mem[10747] = 10766086
mem[40969] = 6443091
mem[2563] = 7520
mem[16385] = 16194
mem[9178] = 1770
mem[3885] = 584370
mem[32909] = 551495
mem[21842] = 534
mem[59406] = 4042521
mem[62127] = 1228052
mem[34922] = 165241779
mem[38187] = 7559
mem[37035] = 51004
mem[64187] = 9284
mem[38834] = 163012800
mem[41856] = 13039831
mem[63376] = 1043992
mem[14490] = 10619
mem[63497] = 64
mem[8985] = 2445
mem[3372] = 2134806
mem[43902] = 25402
mem[63408] = 2150231
mem[35251] = 252
mem[11427] = 40388
mem[5594] = 2064
mem[14642] = 3216356
mem[33886] = 16148
mem[22872] = 317877
mem[16905] = 22391
mem[59260] = 14964908
mem[40205] = 162183
mem[52774] = 21039251
mem[47529] = 13977
mem[50214] = 131677558
mem[37828] = 45776303
mem[25486] = 270926
mem[3307] = 100144082
mem[24709] = 4703889
mem[20251] = 4768780
mem[23739] = 292844
mem[33886] = 59676
mem[29424] = 157758852
mem[48117] = 434386871
mem[24094] = 50839
mem[40525] = 62507
mem[16058] = 11731
mem[57853] = 286
mem[13702] = 252
mem[61517] = 92673
mem[28899] = 10302
mem[39054] = 164757015
mem[46804] = 194909
mem[17096] = 16017
mem[50214] = 474
mem[55787] = 471712
mem[50969] = 44594
mem[62079] = 4008
mem[26760] = 1302
mem[40242] = 450994
mem[13951] = 29184
mem[44387] = 733
mem[56481] = 1419987
mem[21049] = 113460142
mem[37955] = 23023
mem[23639] = 7326
mem[41939] = 125079
mem[4000] = 325975899
mem[65121] = 332644116
mem[41463] = 345
mem[16748] = 1087582
mem[37842] = 894
mem[19704] = 11095
mem[8715] = 72847
mem[41939] = 6370749
mem[12294] = 6805400
mem[21842] = 79
mem[50017] = 55985
mem[49915] = 1470
mem[38942] = 1053875
mem[16748] = 874238254
mem[59353] = 678489
mem[57733] = 14739
mem[40606] = 199
mem[17692] = 1696176
mem[11913] = 4169294
mem[23120] = 237551058
mem[28299] = 142
mem[6118] = 115871
mem[19568] = 514315
mem[24764] = 365
mem[47355] = 25865880
mem[51774] = 163286264
mem[37432] = 2954
mem[31036] = 12067
mem[28516] = 323191
mem[43255] = 3274
mem[58073] = 86929637
mem[18330] = 378470
mem[39651] = 1052033
mem[7784] = 143118549
mem[32641] = 10950293
mem[4029] = 836004
mem[64998] = 399249156
mem[16391] = 2391
mem[15113] = 25159
mem[35039] = 1672488
mem[44901] = 4947519
mem[23194] = 192980783
mem[2379] = 26471
mem[52040] = 748413
mem[35610] = 3487435
mem[15827] = 1447
mem[52266] = 8003180
mem[6315] = 254747938
mem[38582] = 21341903
mem[23012] = 1942
mem[52040] = 1040637609
mem[19431] = 6438
mem[55404] = 381
mem[49702] = 21205234
mem[28299] = 43345
mem[32419] = 3944214
mem[15970] = 1406843
mem[63608] = 2388415
mem[19550] = 397
mem[64770] = 354
mem[27886] = 54971772
mem[61000] = 547
mem[9281] = 5472661
mem[32039] = 20450
mem[47529] = 214406
mem[32922] = 2186075
mem[54996] = 136156
mem[1645] = 1422
mem[645] = 51359613
mem[58473] = 54000741
mem[50214] = 118839
mem[60719] = 4225
mem[4967] = 2810
mem[55561] = 270767273
mem[50906] = 323094280
mem[544] = 1779102
mem[12700] = 122462
mem[20654] = 71
mem[40754] = 6033263
mem[3724] = 640
mem[6776] = 274
mem[59461] = 40987
mem[49367] = 418940480
mem[12155] = 58648
mem[50876] = 55597938
mem[39338] = 125566
mem[16658] = 13293
mem[19410] = 355061209
mem[30127] = 9284451
mem[35805] = 2422
mem[10560] = 116
mem[42153] = 4817997
mem[40333] = 970832
mem[19083] = 63018397
mem[51898] = 95230576
mem[49059] = 481728904
mem[25416] = 1192004
mem[6045] = 244681888
mem[22225] = 208962448
mem[10560] = 3688
mem[50016] = 3038
mem[25234] = 52018301
mem[8343] = 111304110
mem[9100] = 833307
mem[43853] = 836994
mem[9178] = 24458493
mem[59596] = 257520799
mem[13597] = 887
mem[16422] = 232
mem[52384] = 120276377
mem[10834] = 6933
mem[16058] = 992102418
mem[24456] = 92155
mem[37568] = 26930
mem[16385] = 232190606
mem[8340] = 113193119
mem[35910] = 832
mem[56429] = 6105943
mem[33886] = 8020
mem[51774] = 168714
mem[37828] = 28012
mem[51551] = 320681847
mem[34281] = 52088
mem[36720] = 26721
mem[39098] = 8365128
mem[28279] = 994775
mem[59762] = 1466626
mem[17088] = 910296
mem[33578] = 4789
mem[31320] = 11279
mem[58476] = 20790
mem[33584] = 339
mem[21127] = 471449302
mem[39915] = 1754512
mem[51774] = 183707725
mem[43094] = 2797257
mem[21120] = 8809
mem[48113] = 54854990
mem[43108] = 127392
mem[57733] = 4840137
mem[17088] = 7272071
mem[65436] = 1211
mem[38787] = 139
mem[9667] = 37073154
mem[18731] = 370
mem[4538] = 15900
mem[13202] = 517905282
mem[12467] = 932
mem[14070] = 12280
mem[40105] = 184487874
mem[33314] = 832859
mem[17724] = 3496777
mem[53907] = 23167
mem[63544] = 1614248
mem[50209] = 2618603
mem[36348] = 384412976
mem[44536] = 16223523
mem[4784] = 11971978
mem[10982] = 398035940
mem[7005] = 515
mem[35241] = 1093398
mem[63779] = 1906
mem[36341] = 2194817
mem[40531] = 724
mem[5658] = 468782968
mem[62005] = 1168
mem[3491] = 1329281
mem[49511] = 672
mem[49173] = 1048435
mem[44290] = 660182
mem[21184] = 291220
mem[39098] = 2769543
mem[30987] = 24159
mem[48117] = 29838
mem[23924] = 4158
mem[46502] = 501874
mem[32481] = 713511
mem[11848] = 60796
mem[35576] = 125075628
mem[7365] = 367051456
mem[49453] = 2467302
mem[63274] = 2446
mem[59406] = 7522
mem[26316] = 194122
mem[33419] = 24756556
mem[17066] = 827
mem[28511] = 54553
mem[9469] = 136199
mem[6481] = 674106955
mem[4029] = 62909806
mem[19006] = 23636330
mem[49695] = 113355
mem[9478] = 25169678
mem[26475] = 7337
mem[1712] = 89775255
mem[32101] = 917
mem[24770] = 10592648
mem[40969] = 757841
mem[42211] = 7319070
mem[42753] = 277734
mem[30310] = 11988774
mem[19470] = 89618256
mem[2816] = 89780492

``````use crate::common::AdventOfCodeDay;

use std::collections::HashMap;

pub struct Day14 {
input: Vec<String>,
}

impl Day14 {
pub fn new() -> Self {
let input_bytes = include_bytes!("../res/14_input.txt");
let input_str = String::from_utf8_lossy(input_bytes);

let lines = input_str
.lines()
.map(|p| String::from(p))
.collect::<Vec<String>>();

Self {
input: lines
}
}
}

let mut mask_set:   u64 = 0;
let mut mask_unset: u64 = 0;

let mut mem: HashMap<u64, u64> = HashMap::new();

for line in &self.input {

} else if line.starts_with("mem[") {

let addr  = line.replace("mem[", "").replace("] =", "").split(' ').nth(0).unwrap().parse::<u64>().unwrap();
let value = line.replace("mem[", "").replace("] =", "").split(' ').nth(1).unwrap().parse::<u64>().unwrap();

}

}

return mem.iter().map(|(_,v)| v).sum::<u64>().to_string();
}

let mut addr: Vec<(u64,u64)> = Vec::with_capacity(1024);

let mut mem: HashMap<u64, u64> = HashMap::new();

for line in &self.input {

verboseln!();

let floats = strmask.chars().rev().enumerate().filter(|(_,v)| *v == 'X').map(|(i,_)| i).collect::<Vec<usize>>();

for fmask in 0..(2u64.pow(floats.len() as u32)) {

let mut ii = 0;
for i in &floats {

let fbmask = 1u64 << *i;

if fmask & (1<<ii) != 0 {
} else {
}

ii += 1;
}

}
verboseln!();
verboseln!();

} else if line.starts_with("mem[") {

let val_addr  = line.replace("mem[", "").replace("] =", "").split(' ').nth(0).unwrap().parse::<u64>().unwrap();
let val_value = line.replace("mem[", "").replace("] =", "").split(' ').nth(1).unwrap().parse::<u64>().unwrap();

}