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]:
"\n--- Day 7: Some Assembly Required ---\n\nThis 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.\n\nEach 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.\n\nThe 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.\n\nFor example:\n\n123 -> x means that the signal 123 is provided to wire x.\nx AND y -> z means that the bitwise AND of wire x and wire y is provided to wire z.\np LSHIFT 2 -> q means that the value from wire p is left-shifted by 2 and then provided to wire q.\nNOT e -> f means that the bitwise complement of the value from wire e is provided to wire f.\nOther 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.\n\nFor example, here is a simple circuit:\n\n"

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)


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-2-cc11c69d0509> in <module>()
      8 if __name__ == "__main__":
      9     signals = {}
---> 10     lines = open_file("../day7.txt")
     11     for line in lines.split("\n"):
     12         line = line.split(" ")

<ipython-input-2-cc11c69d0509> in open_file(p)
      1 def open_file(p):
----> 2     f = open(p, 'r')
      3     contents = f.read()
      4     f.close()
      5     return contents

FileNotFoundError: [Errno 2] No such file or directory: '../day7.txt'

In [ ]: