Update 2015 solutions
This commit is contained in:
134
2015/d7.py
134
2015/d7.py
@@ -1,90 +1,54 @@
|
||||
from lib import *
|
||||
from lib import get_data
|
||||
|
||||
data = open(0).read()
|
||||
data = get_data(__file__)
|
||||
|
||||
part_2 = False
|
||||
if part_2:
|
||||
gates = {"b": 16076}
|
||||
else:
|
||||
gates = {}
|
||||
|
||||
while "a" not in gates:
|
||||
for line in data.splitlines():
|
||||
lhs, rhs = line.split(" -> ")
|
||||
if part_2 and rhs == "b":
|
||||
continue
|
||||
lhs = lhs.strip()
|
||||
if "NOT" in lhs:
|
||||
op, op1 = lhs.split(" ")
|
||||
assert op == "NOT"
|
||||
try:
|
||||
op1 = int(op1)
|
||||
gates[rhs] = ~op1 & 0xffff
|
||||
except ValueError:
|
||||
if op1 in gates and isinstance(gates[op1], int):
|
||||
gates[rhs] = ~gates[op1] & 0xffff
|
||||
elif "OR" in lhs:
|
||||
op1, op, op2 = lhs.split(" ")
|
||||
assert op == "OR"
|
||||
try:
|
||||
op1 = int(op1)
|
||||
except ValueError:
|
||||
if op1 in gates and isinstance(gates[op1], int):
|
||||
op1 = gates[op1]
|
||||
try:
|
||||
op2 = int(op2)
|
||||
except ValueError:
|
||||
if op2 in gates and isinstance(gates[op2], int):
|
||||
op2 = gates[op2]
|
||||
|
||||
if not (isinstance(op1, int) and isinstance(op2, int)):
|
||||
continue
|
||||
gates[rhs] = (op1 | op2)
|
||||
elif "AND" in lhs:
|
||||
op1, op, op2 = lhs.split(" ")
|
||||
assert op == "AND"
|
||||
try:
|
||||
op1 = int(op1)
|
||||
except ValueError:
|
||||
if op1 in gates and isinstance(gates[op1], int):
|
||||
op1 = gates[op1]
|
||||
try:
|
||||
op2 = int(op2)
|
||||
except ValueError:
|
||||
if op2 in gates and isinstance(gates[op2], int):
|
||||
op2 = gates[op2]
|
||||
|
||||
if not (isinstance(op1, int) and isinstance(op2, int)):
|
||||
continue
|
||||
gates[rhs] = (op1 & op2)
|
||||
elif "LSHIFT" in lhs:
|
||||
op1, op, op2 = lhs.split(" ")
|
||||
op2 = int(op2)
|
||||
try:
|
||||
op1 = int(op1)
|
||||
except ValueError:
|
||||
if op1 in gates:
|
||||
op1 = gates[op1]
|
||||
else:
|
||||
continue
|
||||
gates[rhs] = (op1 << op2) & 0xffff
|
||||
elif "RSHIFT" in lhs:
|
||||
op1, op, op2 = lhs.split(" ")
|
||||
op2 = int(op2)
|
||||
try:
|
||||
op1 = int(op1)
|
||||
except ValueError:
|
||||
if op1 in gates:
|
||||
op1 = gates[op1]
|
||||
else:
|
||||
continue
|
||||
gates[rhs] = (op1 >> op2) & 0xffff
|
||||
def run(wires={}):
|
||||
def get(a):
|
||||
try:
|
||||
return int(a)
|
||||
except ValueError:
|
||||
pass
|
||||
if a in wires:
|
||||
return wires[a]
|
||||
else:
|
||||
try:
|
||||
lhs = int(lhs)
|
||||
gates[rhs] = lhs
|
||||
except ValueError:
|
||||
if lhs in gates:
|
||||
gates[rhs] = gates[lhs]
|
||||
return None
|
||||
|
||||
print(gates["a"])
|
||||
while "a" not in wires:
|
||||
for line in data.splitlines():
|
||||
lhs, rhs = line.split(" -> ")
|
||||
if rhs in wires:
|
||||
continue
|
||||
match lhs.split():
|
||||
case [a, "AND", b]:
|
||||
a, b = get(a), get(b)
|
||||
if a is not None and b is not None:
|
||||
wires[rhs] = a & b
|
||||
case [a, "OR", b]:
|
||||
a, b = get(a), get(b)
|
||||
if a is not None and b is not None:
|
||||
wires[rhs] = a | b
|
||||
case [a, "LSHIFT", b]:
|
||||
a, b = get(a), get(b)
|
||||
if a is not None and b is not None:
|
||||
wires[rhs] = a << b
|
||||
case [a, "RSHIFT", b]:
|
||||
a, b = get(a), get(b)
|
||||
if a is not None and b is not None:
|
||||
wires[rhs] = a >> b
|
||||
case ["NOT", a]:
|
||||
a = get(a)
|
||||
if a is not None:
|
||||
wires[rhs] = ~a & 0xFFFF
|
||||
case [a]:
|
||||
a = get(a)
|
||||
if a is not None:
|
||||
wires[rhs] = a
|
||||
return wires
|
||||
|
||||
|
||||
a = run()["a"]
|
||||
print(a)
|
||||
|
||||
a2 = run(wires={"b": a})["a"]
|
||||
print(a2)
|
||||
|
||||
Reference in New Issue
Block a user