Example #1
0
def run_tests_create_tape():
    tape = Tape()
    data = ["A", "B", "C", "B", "C", "A", "\n"]
    expected = "65,66,67,66,67,65,10"

    for ch in data:
        tape.append(ch, ord)

    res = str(tape)
    if res == expected:
        print(f"SUCCESS: Got {res} as expected")
    else:
        print(f"FAILED: Expected {expected} but got {res}")

    tape = Tape()
    data = ["R", ",", "8", ",", "L", ",", "6", "\n"]
    expected = "82,44,56,44,76,44,54,10"

    tape.append(data, ord)

    res = str(tape)
    if res == expected:
        print(f"SUCCESS: Got {res} as expected")
    else:
        print(f"FAILED: Expected {expected} but got {res}")
Example #2
0
 def to_tape(self, items):
     """
     Build a Tape from list of strings given in <items>, inserting a comma between
     individual items and adding a newline after the last item.
     """
     tape = Tape()
     separators = [','] * (len(items) - 1) + ['\n']
     for item, sep in zip(items, separators):
         tape.append(item, ord)
         tape.append(sep, ord)
     return tape
Example #3
0
    def execute(self, beam):
        self.beam = beam
        readings = []

        for self.coord in self.positions():
            if self.finished():
                break

            y,x = self.coord
            self._vprint(f"Position: {(x,y)}")

            tape = Tape(self.tape)
            tape.rewind() # TODO: necessary?

            computer = Interpreter(tape, inputs=[x,y], outputs=readings)
            computer.execute()
            if readings.pop(0) == 1:
                self.beam.add((y,x))

            self._vprint(str(self.beam))