advent-of-code/2015/python/day07.py

57 lines
1.1 KiB
Python
Raw Normal View History

2022-12-19 04:55:45 +00:00
import shared
import matrix
from pprint import pprint
import numpy as np
import operator
OPS = {
'AND': operator.and_,
'OR': operator.or_,
'LSHIFT': operator.lshift,
'RSHIFT': operator.rshift,
}
def NOT(x):
return int(x) ^ 65535
def pt1(inp):
print(inp)
registers = {}
for i in inp:
inst, dest = i.split(" -> ")
if " " not in inst:
registers[dest] = inst
continue
inst = inst.split()
if inst[0] == "NOT":
registers[dest] = NOT(registers[inst[1]])
continue
a,op,b = inst
try:
a = int(a)
except ValueError:
a = int(registers[a])
try:
b = int(b)
except ValueError:
b = int(registers[b])
print(a,b)
registers[dest] = OPS[op](a,b)
#x,op,y = inst.split(" ")
pprint(registers)
def main():
with open(shared.get_fname(7), "r") as f:
inp = [x.rstrip() for x in f.readlines()]
pt1(inp)
#pt2(inp)
if __name__ == "__main__":
main()