challenge.txt 5.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. --- Day 14: Docking Data ---
  2. 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.
  3. 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!
  4. 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.
  5. 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.
  6. For example, consider the following program:
  7. mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
  8. mem[8] = 11
  9. mem[7] = 101
  10. mem[8] = 0
  11. 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.
  12. 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:
  13. value: 000000000000000000000000000000001011 (decimal 11)
  14. mask: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
  15. result: 000000000000000000000000000001001001 (decimal 73)
  16. 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:
  17. value: 000000000000000000000000000001100101 (decimal 101)
  18. mask: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
  19. result: 000000000000000000000000000001100101 (decimal 101)
  20. 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:
  21. value: 000000000000000000000000000000000000 (decimal 0)
  22. mask: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
  23. result: 000000000000000000000000000001000000 (decimal 64)
  24. 64 is written to address 8 instead, overwriting the value that was there previously.
  25. 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.
  26. Execute the initialization program. What is the sum of all values left in memory after it completes?
  27. Your puzzle answer was 7611244640053.
  28. --- Part Two ---
  29. 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!
  30. 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:
  31. If the bitmask bit is 0, the corresponding memory address bit is unchanged.
  32. If the bitmask bit is 1, the corresponding memory address bit is overwritten with 1.
  33. If the bitmask bit is X, the corresponding memory address bit is floating.
  34. 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!
  35. For example, consider the following program:
  36. mask = 000000000000000000000000000000X1001X
  37. mem[42] = 100
  38. mask = 00000000000000000000000000000000X0XX
  39. mem[26] = 1
  40. When this program goes to write to memory address 42, it first applies the bitmask:
  41. address: 000000000000000000000000000000101010 (decimal 42)
  42. mask: 000000000000000000000000000000X1001X
  43. result: 000000000000000000000000000000X1101X
  44. 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:
  45. 000000000000000000000000000000011010 (decimal 26)
  46. 000000000000000000000000000000011011 (decimal 27)
  47. 000000000000000000000000000000111010 (decimal 58)
  48. 000000000000000000000000000000111011 (decimal 59)
  49. Next, the program is about to write to memory address 26 with a different bitmask:
  50. address: 000000000000000000000000000000011010 (decimal 26)
  51. mask: 00000000000000000000000000000000X0XX
  52. result: 00000000000000000000000000000001X0XX
  53. This results in an address with three floating bits, causing writes to eight memory addresses:
  54. 000000000000000000000000000000010000 (decimal 16)
  55. 000000000000000000000000000000010001 (decimal 17)
  56. 000000000000000000000000000000010010 (decimal 18)
  57. 000000000000000000000000000000010011 (decimal 19)
  58. 000000000000000000000000000000011000 (decimal 24)
  59. 000000000000000000000000000000011001 (decimal 25)
  60. 000000000000000000000000000000011010 (decimal 26)
  61. 000000000000000000000000000000011011 (decimal 27)
  62. 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.
  63. 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?
  64. Your puzzle answer was 3705162613854.