aoc-2020-zig

Advent of Code 2020 Solutions in Zig
git clone https://git.sinitax.com/sinitax/aoc-2020-zig
Log | Files | Refs | README | sfeed.txt

part2 (3382B)


      1--- Part Two ---
      2
      3For some reason, the sea port's computer system still can't communicate with your ferry's docking
      4program. It must be using version 2 of the decoder chip!
      5
      6A version 2 decoder chip doesn't modify the values being written at all.  Instead, it acts as a
      7memory address decoder. Immediately before a value is written to memory, each bit in the bitmask
      8modifies the corresponding bit of the destination memory address in the following way:
      9
     10
     11 - If the bitmask bit is 0, the corresponding memory address bit is unchanged.
     12 - If the bitmask bit is 1, the corresponding memory address bit is overwritten with 1.
     13 - If the bitmask bit is X, the corresponding memory address bit is floating.
     14
     15
     16A floating bit is not connected to anything and instead fluctuates unpredictably. In
     17practice, this means the floating bits will take on all possible values, potentially
     18causing many memory addresses to be written all at once!
     19
     20For example, consider the following program:
     21
     22mask = 000000000000000000000000000000X1001X
     23mem[42] = 100
     24mask = 00000000000000000000000000000000X0XX
     25mem[26] = 1
     26
     27When this program goes to write to memory address 42, it first applies the bitmask:
     28
     29address: 000000000000000000000000000000101010  (decimal 42)
     30mask:    000000000000000000000000000000X1001X
     31result:  000000000000000000000000000000X1101X
     32
     33After applying the mask, four bits are overwritten, three of which are different, and two of which
     34are floating. Floating bits take on every possible combination of values; with two
     35floating bits, four actual memory addresses are written:
     36
     37000000000000000000000000000000011010  (decimal 26)
     38000000000000000000000000000000011011  (decimal 27)
     39000000000000000000000000000000111010  (decimal 58)
     40000000000000000000000000000000111011  (decimal 59)
     41
     42Next, the program is about to write to memory address 26 with a different bitmask:
     43
     44address: 000000000000000000000000000000011010  (decimal 26)
     45mask:    00000000000000000000000000000000X0XX
     46result:  00000000000000000000000000000001X0XX
     47
     48This results in an address with three floating bits, causing writes to eight memory
     49addresses:
     50
     51000000000000000000000000000000010000  (decimal 16)
     52000000000000000000000000000000010001  (decimal 17)
     53000000000000000000000000000000010010  (decimal 18)
     54000000000000000000000000000000010011  (decimal 19)
     55000000000000000000000000000000011000  (decimal 24)
     56000000000000000000000000000000011001  (decimal 25)
     57000000000000000000000000000000011010  (decimal 26)
     58000000000000000000000000000000011011  (decimal 27)
     59
     60The entire 36-bit address space still begins initialized to the value 0 at every address, and you
     61still need the sum of all values left in memory at the end of the program.  In this example, the sum
     62is 208.
     63
     64Execute the initialization program using an emulator for a version 2 decoder chip. What is
     65the sum of all values left in memory after it completes?
     66
     67