115 lines
No EOL
4.9 KiB
Python
115 lines
No EOL
4.9 KiB
Python
## broken btw #therewasanattempt
|
|
# 123 -> x - signal 123 provided to x
|
|
with open("./input", "r") as f:
|
|
instructions = f.read().split("\n")
|
|
|
|
wires = []
|
|
|
|
## get all wires or sum shit idk
|
|
for instruction in instructions:
|
|
alreadyFound = False
|
|
wireIdentifier = instruction.split(" -> ")[1].replace(" -> ", "")
|
|
if wires != []:
|
|
for wire in wires:
|
|
if wire["identifier"] == wireIdentifier:
|
|
alreadyFound = True
|
|
if not alreadyFound:
|
|
wires.append({"identifier": wireIdentifier, "signal": 0})
|
|
|
|
## ok now do the operations
|
|
for instruction in instructions:
|
|
if instruction != "":
|
|
if "AND" in instruction:
|
|
"""x AND y -> d"""
|
|
fi = instruction.split(" AND ")[0]
|
|
si = instruction.split(" AND ")[1].replace(" AND ", "").split(" -> ")[0]
|
|
di = instruction.split(" AND ")[1].replace(" AND ", "").split(" -> ")[1].replace(" -> ", "")
|
|
fv = 0
|
|
sv = 0
|
|
for wire in wires:
|
|
if wire['identifier'] == fi:
|
|
fv = wire['signal']
|
|
elif wire['identifier'] == si:
|
|
sv = wire['signal']
|
|
for wire in wires:
|
|
if wire['identifier'] == di:
|
|
wire['signal'] = fv & sv
|
|
print(wire, fi,si,fv,sv, "INSTRUCTION TYPE AND")
|
|
elif "OR" in instruction:
|
|
"""x OR y -> e"""
|
|
fi = instruction.split(" OR ")[0]
|
|
si = instruction.split(" OR ")[1].replace(" OR ", "").split(" -> ")[0]
|
|
di = instruction.split(" OR ")[1].replace(" OR ", "").split(" -> ")[1].replace(" -> ", "")
|
|
fv = 0
|
|
sv = 0
|
|
for wire in wires:
|
|
if wire['identifier'] == fi:
|
|
fv = wire['signal']
|
|
elif wire['identifier'] == si:
|
|
sv = wire['signal']
|
|
for wire in wires:
|
|
if wire['identifier'] == di:
|
|
wire['signal'] = fv | sv
|
|
print(wire, fi,si,fv,sv, "INSTRUCTION TYPE OR")
|
|
elif "LSHIFT" in instruction:
|
|
"""x RSHIFT [ammount] -> e"""
|
|
fi = instruction.split(" LSHIFT ")[0]
|
|
ammount = instruction.split(" LSHIFT ")[1].replace(" LSHIFT ", "").split(" -> ")[0]
|
|
di = instruction.split(" LSHIFT ")[1].replace(" LSHIFT ", "").split(" -> ")[1].replace(" -> ", "")
|
|
fv = 0
|
|
for wire in wires:
|
|
if wire['identifier'] == fi:
|
|
fv = wire['signal']
|
|
for wire in wires:
|
|
if wire['identifier'] == di:
|
|
wire['signal'] = fv << int(ammount)
|
|
print(wire, fi,si,fv,sv, "INSTRUCTION TYPE RSHIFT")
|
|
elif "RSHIFT" in instruction:
|
|
"""x RSHIFT [ammount] -> e"""
|
|
fi = instruction.split(" RSHIFT ")[0]
|
|
ammount = instruction.split(" RSHIFT ")[1].replace(" RSHIFT ", "").split(" -> ")[0]
|
|
di = instruction.split(" RSHIFT ")[1].replace(" RSHIFT ", "").split(" -> ")[1].replace(" -> ", "")
|
|
fv = 0
|
|
for wire in wires:
|
|
if wire['identifier'] == fi:
|
|
fv = wire['signal']
|
|
for wire in wires:
|
|
if wire['identifier'] == di:
|
|
wire['signal'] = fv >> int(ammount)
|
|
print(wire, fi,si,fv,sv, "INSTRUCTION TYPE RSHIFT")
|
|
elif "NOT" in instruction:
|
|
"""NOT x -> h"""
|
|
wi = instruction.replace("NOT ", "").split(" ->")[0]
|
|
di = instruction.replace("NOT ", "").split(" ->")[1].replace(" -> ", "")
|
|
wv = 0
|
|
for wire in wires:
|
|
if wire['identifier'] == wi:
|
|
wv = wire['signal']
|
|
for wire in wires:
|
|
if wire['identifier'] == di:
|
|
wire['signal'] = ~wv
|
|
print(wire, fi,si,vw, "INSTRUCTION TYPE NOT")
|
|
else:
|
|
"""123 -> x"""
|
|
if not instruction.split(" -> ")[0].isdigit:
|
|
value = int(instruction.split(" -> ")[0])
|
|
di = instruction.split(" -> ")[1].replace(" -> ", "")
|
|
for wire in wires:
|
|
if wire['identifier'] == di:
|
|
wire['signal'] = value
|
|
print(wire, value, "INSTRUCTION TYPE SET")
|
|
else:
|
|
wi = instruction.split(" -> ")[0]
|
|
value = 0
|
|
for wire in wires:
|
|
if wire['identifier'] == wi:
|
|
value = wire['signal']
|
|
di = instruction.split(" -> ")[1].replace(" -> ", "")
|
|
for wire in wires:
|
|
if wire['identifier'] == di:
|
|
wire['signal'] = value
|
|
print(wire, value, "INSTRUCTION TYPE SET")
|
|
|
|
for wire in wires:
|
|
if wire['identifier'] == "a":
|
|
print(wire['signal']) |