Viper

Posted on

# Advent of Code 2020: Python Solution Day 14

Today's challenge was associated with binary system and it was pretty tricky too. I had to take help from here. I also have updated my repository here.

## Part 1

with open("day14.txt", 'r') as fp:
lines = [line.rstrip() for line in fp.readlines()]

memory = {}
curr_value = None

for line in lines:
k, v = line.split(" = ")
else:
curr_value = int(v)

bin_value = list(bin(curr_value)[2:].zfill(36))
new_value = [0] * 36

# do nothing if X
new_value[i] = value
else:
# change value to mask else

print(f"Part one solution: {sum(memory.values())}")

## Part 2

"""
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(" = ")
else:
curr_value = int(v)

# if mask bit is floating then keep it floating on new address too

# change value to mask else

# count floatings

flucts = []
for i in range(2**num_poss):
flucts.append( list(bin(i)[2:].zfill(num_poss)))

for fluct in flucts:
i = 0
if a == "X":
i+=1
else:
sum(memory.values())

rozbrajaczpoziomow

So I have smaller code for part 1. (I'd like to note, that instead of changing the file on-the-fly, i just assign the variable prompt as (in this case) an array of strings of every line)

import re
prompt = (input)
memory = []

for command in prompt:
elif(command.startswith('mem')):
index = int(re.findall(r'mem\[(.*)\]', command)[0])
value = int(re.findall(r' = (.*)', command)[0])
finalbin = ''
else:
final = int(finalbin, 2)
while(len(memory) <= index + 5):
memory.append(0)
memory[index] = final

print('[Solv]', sum(memory))

Viper

Awesome

Viper

I think it gets more handy as we keep doing.

Viper

Honestly, on average 1 hour. What about you?