Guest User

Untitled

a guest
Dec 14th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. day14_1():
  2.     with open("input.txt", "r") as file:
  3.         data = file.readlines()
  4.  
  5.     mem = {}
  6.     on_mask = 0
  7.     off_mask = 0
  8.     for line in data:
  9.         if line[1] == "a":  # Mask
  10.             mask = line[7:-1]
  11.             on_mask = int(mask.replace("X", "0"), base=2)   # OR to set 1s
  12.             off_mask = int(mask.replace("X", "1"), base=2)   # AND to set 0s
  13.         else:
  14.             addr = line[line.find("[")+1:line.find("]")]
  15.             val = int(line[line.find("=")+2:-1])
  16.  
  17.             mem[addr] = (val | on_mask) & off_mask
  18.  
  19.     total = 0
  20.     for k, v in mem.items():
  21.         total += v
  22.  
  23.     return total
Advertisement
Add Comment
Please, Sign In to add comment