part1 (3104B)
1--- Day 14: Docking Data --- 2 3As your ferry approaches the sea port, the captain asks for your help again. The computer system 4that runs this port isn't compatible with the docking program on the ferry, so the docking 5parameters aren't being correctly initialized in the docking program's memory. 6 7After a brief inspection, you discover that the sea port's computer system uses a strange bitmask 8system in its initialization program. Although you don't have the correct decoder chip handy, you 9can emulate it in software! 10 11The initialization program (your puzzle input) can either update the bitmask or write a value to 12memory. Values and memory addresses are both 36-bit unsigned integers. For example, ignoring 13bitmasks for a moment, a line like mem[8] = 11 would write the value 11 to memory address 8. 14 15The bitmask is always given as a string of 36 bits, written with the most significant bit 16(representing 2^35) on the left and the least significant bit (2^0, that is, the 1s bit) on the 17right. The current bitmask is applied to values immediately before they are written to memory: a 0 18or 1 overwrites the corresponding bit in the value, while an X leaves the bit in the value 19unchanged. 20 21For example, consider the following program: 22 23mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X 24mem[8] = 11 25mem[7] = 101 26mem[8] = 0 27 28This program starts by specifying a bitmask (mask = ....). The mask it specifies will overwrite two 29bits in every written value: the 2s bit is overwritten with 0, and the 64s bit is overwritten with 301. 31 32The program then attempts to write the value 11 to memory address 8. By expanding everything out to 33individual bits, the mask is applied as follows: 34 35value: 000000000000000000000000000000001011 (decimal 11) 36mask: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X 37result: 00000000000000000000000000000[1m[37m1[0m0010[1m[37m0[0m1 (decimal 73) 38 39So, because of the mask, the value 73 is written to memory address 8 instead. Then, the program 40tries to write 101 to address 7: 41 42value: 000000000000000000000000000001100101 (decimal 101) 43mask: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X 44result: 00000000000000000000000000000[1m[37m1[0m1001[1m[37m0[0m1 (decimal 101) 45 46This time, the mask has no effect, as the bits it overwrote were already the values the mask tried 47to set. Finally, the program tries to write 0 to address 8: 48 49value: 000000000000000000000000000000000000 (decimal 0) 50mask: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X 51result: 00000000000000000000000000000[1m[37m1[0m0000[1m[37m0[0m0 (decimal 64) 52 5364 is written to address 8 instead, overwriting the value that was there previously. 54 55To initialize your ferry's docking program, you need the sum of all values left in memory after the 56initialization program completes. (The entire 36-bit address space begins initialized to the value 0 57at every address.) In the above example, only two values in memory are not zero - 101 (at address 7) 58and 64 (at address 8) - producing a sum of [1m[37m165[0m. 59 60Execute the initialization program. [1m[37mWhat is the sum of all values left in memory after it 61completes?[0m 62 63