Advent of Code day 14

Today's puzzle was pretty straightforward. We're working with a bitmask:

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.

I had to hunt a bit to find a way to keep the numbers in binary notation.

value = f'{int(value):036b}'

Once I did that, the Part 1 was easy enough.

For part 2 we find that it's actually the memory location that's masked, not the value, and the X's actually indicate that the memory address bit is floating -- i.e. it could be a 0 or a 1. Note (this caught me) in part 2 a zero in the mask means the value at that location is unchanged -- don't overwrite it with zero!

The fact that some of the address bits are floating means that every X in the mask doubles the number of address locations that are referenced -- so instead of putting a value into an address, you're putting a value into multiple addresses. I used a list to keep track of the addresses and just appended the additional address to the list.

def apply_mask_v2(val, mask):
    val = [str(val)]
    for ind in range(len(mask)):

        if mask[ind] == '1':
            for i in range(len(val)):
                val[i] = val[i][0:ind] + mask[ind] + val[i][ind+1:]

        elif mask[ind] == 'X':
            for i in range(len(val)):
                val[i] = val[i][0:ind] + '0' + val[i][ind+1:]
                val.append(val[i][0:ind] + '1' + val[i][ind+1:])

    return(val)

This didn't really slow down my code and I got Part 2 pretty quickly.