Advent of Code 2020: Python Solution Day 14
Tags: adventofcode, python, algorithms, challenge
Published: 2020-12-14 07:48:25

Today's challenge was associated with binary system and it was pretty tricky too. I had to take help from [here](https://topaz.github.io/paste/#XQAAAQAUBwAAAAAAAAAyGUj/T32X5leZ5R0tKWDjkddL9GGHIDr5WbxQ2YlPgMG3UJZWQ/w32Is2GppdQF+UzAicr1hRnC8CEQe1grdVM/ILGxAnQhOAT7p26sjzTIfAl4f2z/xaejDha9l17PsRVjix1QFgK2qMobG3XHhRmlUHUuyy5L0fAST2KUn8E/MjHbcfhaBX1/9mlwF92qwkYp1ra8jVmzvZUP8ogAbXJm/JPpPeqR3J/egvpKl8bO7KS/e59gkHsU40tT63I2aQoVNWWMcSU0NUVa+y0dbq7WWC5BKJT1BMXMTfSvKWdqi2qUYzN0vKqY8pC36FPx0DdCr38lzJbItaLf2rrmGxNXQLOb4GOk6BwqkPQUKrY41P/MTc/iKi3v3r5wVHSeSFyPnWQA9AJupHw8Hdd8znPVnxsjUr5PUpQL8aXvwkmxBi6r/mXXuI53UjTXoQfc87FHWL97WUi8WM6GDNBJaImhhjQqR3mMLCXWaFoldfXQpjJBPZ3eps1PyZxK6nKRDoxkStqv6ENhBxeLIG1hT65ERHpQt96nPXYSi8rxGFE9AZt27K/0LT18IbQK2GcLlnhp4zZF5VjUAVbgNHvNvDr1LgMicR0tZYNJOKvufrPqmxUNQY8BspOPeuMdo/d1jMZnMLnmqZcZ7UZSLyLXMPV5QmDI1jSRb9SpGwjEcI//8yEEoA). I also have updated my repository [here](https://github.com/q-viper/Adevent-Of-Code-2020).

**Please share your solution too.**
## Part 1
```python
with open("day14.txt", 'r') as fp:
    lines = [line.rstrip() for line in fp.readlines()]
    
memory = {}
masks = []
curr_mask = None
curr_value = None
curr_address = None

for line in lines:
    k, v = line.split(" = ")
    if k == "mask":
        masks.append(v)
        curr_mask = v
    else:
        # get address
        curr_address = int(k[4:-1])
        curr_value = int(v)       
        
        bin_value = list(bin(curr_value)[2:].zfill(36))
        new_value = [0] * 36
        
        for i, (mask, value) in enumerate(zip(curr_mask, bin_value)):
            # do nothing if X
            if mask == "X":
                new_value[i] = value
            else:
                # change value to mask else
                new_value[i] = mask

        memory[curr_address] = int("".join(new_value), 2)
print(f"Part one solution: {sum(memory.values())}")    
```
## Part 2
```python
"""
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.
"""
with open("day14.txt", 'r') as fp:
    lines = [line.rstrip() for line in fp.readlines()]

memory = {}
for line in lines:
    k, v = line.split(" = ")
    if k == "mask":
        masks.append(v)
        curr_mask = v
    else:
        # get address
        curr_address = int(k[4:-1])
        curr_value = int(v)       
        
        bin_add = list(bin(curr_address)[2:].zfill(36))
        new_add = ["0"] * 36
        
        v = len(list(bin(curr_address)[2:]))
                
        for i, (mask, value) in enumerate(zip(curr_mask, bin_add)):
            # if mask bit is floating then keep it floating on new address too
            if mask == "X":
                new_add[i] = "X"
            elif mask == "0":
                
                # change value to mask else
                new_add[i] = value
            elif mask == "1":
                new_add[i] = "1"
        
        new_add = "".join(new_add)
        
        # count floatings
        num_poss = new_add.count("X")
        
        flucts = []
        for i in range(2**num_poss):
            flucts.append( list(bin(i)[2:].zfill(num_poss)))

        for fluct in flucts:
            i = 0
            nadd = ""
            for a in new_add:
                if a == "X":
                    nadd+=str(fluct[i])
                    i+=1
                else:
                    nadd+=str(a)
            memory[int(nadd, 2)]=curr_value
sum(memory.values())
```