In [1]:
"""
--- Day 7: Some Assembly Required ---
This year, Santa brought little Bobby Tables a set of wires and bitwise logic gates! Unfortunately, little Bobby is a little under the recommended age range, and he needs help assembling the circuit.
Each wire has an identifier (some lowercase letters) and can carry a 16-bit signal (a number from 0 to 65535). A signal is provided to each wire by a gate, another wire, or some specific value. Each wire can only get a signal from one source, but can provide its signal to multiple destinations. A gate provides no signal until all of its inputs have a signal.
The included instructions booklet describes how to connect the parts together: x AND y -> z means to connect wires x and y to an AND gate, and then connect its output to wire z.
For example:
123 -> x means that the signal 123 is provided to wire x.
x AND y -> z means that the bitwise AND of wire x and wire y is provided to wire z.
p LSHIFT 2 -> q means that the value from wire p is left-shifted by 2 and then provided to wire q.
NOT e -> f means that the bitwise complement of the value from wire e is provided to wire f.
Other possible gates include OR (bitwise OR) and RSHIFT (right-shift). If, for some reason, you'd like to emulate the circuit instead, almost all programming languages (for example, C, JavaScript, or Python) provide operators for these gates.
For example, here is a simple circuit:
"""
Out[1]:
In [ ]:
lines =
In [2]:
def open_file(p):
f = open(p, 'r')
contents = f.read()
f.close()
return contents
base = (2 ** 16) - 1
if __name__ == "__main__":
signals = {}
# lines = open_file("../day7.txt")
for line in lines.split("\n"):
line = line.split(" ")
count = len(line)
print(line)
if (count == 3):
# Assignmentif
signals[line[2]] = line[0]
elif (count == 4):
# Not
print("Not")
if signals.get(line[1]):
signals[line[3]] = ~(signals.get(line[1])) % base
elif (count == 5):
# LSHIFT, RSHIFT, AND, OR
print ("Other")
print(signals.get(line[0]), signals.get(line[0]))
x = signals.get(line[0])
y = signals.get(line[2])
# if signals.get(line[0]) else int(line[0])
if (x and y):
if (line[1] == "LSHIFT"):
signals[line[4]] = int(x) << int(y)
print()
elif (line[1] == "RSHIFT"):
signals[line[4]] = int(x) >> int(y)
elif (line[1] == "AND"):
print()
signals[line[4]] = int(x) & int(y)
elif (line[1] == "OR"):
print()
signals[line[4]] = int(x) | int(y)
print (signals)
In [ ]: