Beispiel #1
0
class Robot:
    def __init__(self, code, initial_color=0):
        self.computer = Intcomputer(code)
        self.grid = defaultdict(int)
        self.grid[(0, 0)] = initial_color
        self.orientation = 0
        self.position = (0, 0)

    def run(self):
        """Executes program and manages IO operations.

        Assumes that the program always follows the same pattern: take
        exactly one input, then output exactly two values (or halt)
        """
        while True:
            state, color = self.computer.execute(self.grid[self.position])

            if state == 'OUTPUT':
                self.grid[self.position] = color

            if state == 'HALT':
                break

            direction = self.computer.execute()[1]
            self.move(direction)

    def move(self, direction):
        """Set new position after move in given direction."""
        update = -1 if direction == 0 else 1
        self.orientation = (self.orientation + update) % 4
        step = [(0, 1), (1, 0), (0, -1), (-1, 0)][self.orientation]
        self.position = (self.position[0] + step[0], self.position[1] + step[1])

    def draw(self):
        """Return a string representation of the currently painted panels."""
        w_min = min(self.grid, key=itemgetter(0))[0]
        w_max = max(self.grid, key=itemgetter(0))[0]
        h_min = min(self.grid, key=itemgetter(1))[1]
        h_max = max(self.grid, key=itemgetter(1))[1]

        res = ''
        for j in range(h_max, h_min - 1, -1):
            for i in range(w_min, w_max + 1):
                res += ' ' if self.grid[(i, j)] == 0 else '█'
            res += '\n'

        return res
Beispiel #2
0
 def test_sample_2(self):
     code = [1102, 34915192, 34915192, 7, 4, 7, 99, 0]
     computer = Intcomputer(code)
     output = []
     while True:
         status, out = computer.execute()
         if status == 'HALT': break
         output.append(out)
     self.assertEqual(len(str(output[0])), 16)
Beispiel #3
0
class Arcade:
    def __init__(self, code, mode=1):
        self.cpu = Intcomputer(code)
        self.cpu.memory[0] = mode
        self.tiles = np.empty((21, 40), dtype=np.int)
        self.score = 0
        self.x_ball = -1
        self.x_paddle = -1

    def play(self):
        state = 'INIT'

        while state != 'HALT':
            state, output = self.step(state)
            self.update(output)

    def step(self, state):
        output = []

        if state == 'WAITING':
            move = np.sign(self.x_ball - self.x_paddle)
            state, i = self.cpu.execute(move)
            output.append(i)

        while True:
            state, i = self.cpu.execute()
            if state != 'OUTPUT':
                break
            output.append(i)

        return state, output

    def update(self, output):
        for x, y, value in np.reshape(output, (-1, 3)):
            if x == -1 and y == 0:
                self.score = value
                continue

            if value == BALL:
                self.x_ball = x
            if value == PADDLE:
                self.x_paddle = x

            self.tiles[y, x] = value
Beispiel #4
0
 def test_sample_1(self):
     code = [
         109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101,
         0, 99
     ]
     computer = Intcomputer(code)
     output = []
     while True:
         status, out = computer.execute()
         if status == 'HALT': break
         output.append(out)
     self.assertEqual(code, output)
Beispiel #5
0
 def test_sample_3(self):
     code = [104, 1125899906842624, 99]
     computer = Intcomputer(code)
     status, out = computer.execute()
     self.assertEqual(out, 1125899906842624)
Beispiel #6
0
 def test_adjust_relative_mode_0(self):
     code = [9, 3, 99, 10]
     computer = Intcomputer(code)
     computer.execute()
     self.assertEqual(computer.relative_base, 10)
Beispiel #7
0
 def test_command_1006b(self):
     code = [1006, 4, 5, 99, 1, 0]
     computer = Intcomputer(code)
     computer.execute()
     self.assertTrue(True)
Beispiel #8
0
 def test_command_2105(self):
     code = [2105, 1, -7, 4, 99]
     computer = Intcomputer(code)
     computer.relative_base = 10
     computer.execute()
     self.assertTrue(True)
Beispiel #9
0
 def test_command_20101(self):
     code = [20101, 5, 1, -5, 99, 0]
     computer = Intcomputer(code)
     computer.relative_base = 10
     computer.execute()
     self.assertEqual(computer.memory[:6], [20101, 5, 1, -5, 99, 10])
Beispiel #10
0
 def test_command_1(self):
     code = [1, 5, 6, 7, 99, 2, 3, 0]
     computer = Intcomputer(code)
     computer.execute()
     self.assertEqual(computer.memory[:8], [1, 5, 6, 7, 99, 2, 3, 5])
Beispiel #11
0
 def test_command_1101(self):
     code = [1101, 5, -4, 5, 99, 0]
     computer = Intcomputer(code)
     computer.execute()
     self.assertEqual(computer.memory[:6], [1101, 5, -4, 5, 99, 1])
Beispiel #12
0
 def test_mul_mode_12(self):
     code = [1202, -5, 2, 3, 99, 6]
     computer = Intcomputer(code)
     computer.relative_base = 10
     computer.execute()
     self.assertEqual(computer.memory[:6], [1202, -5, 2, 12, 99, 6])
Beispiel #13
0
 def test_add_mode_12(self):
     code = [1201, -5, 5, 3, 99, 11]
     computer = Intcomputer(code)
     computer.relative_base = 10
     computer.execute()
     self.assertEqual(computer.memory[:6], [1201, -5, 5, 16, 99, 11])
Beispiel #14
0
 def test_command_1001(self):
     code = [1001, 5, 10, 6, 99, 7, 0]
     computer = Intcomputer(code)
     computer.execute()
     self.assertEqual(computer.memory[:7], [1001, 5, 10, 6, 99, 7, 17])
Beispiel #15
0
 def test_add_mode_21(self):
     code = [2101, 5, -5, 6, 99, 11]
     computer = Intcomputer(code)
     computer.relative_base = 10
     computer.execute()
     self.assertEqual(computer.memory[:7], [2101, 5, -5, 6, 99, 11, 16])
Beispiel #16
0
 def test_adjust_relative_mode_2(self):
     code = [209, -1, 99, 20]
     computer = Intcomputer(code)
     computer.relative_base = 4
     computer.execute()
     self.assertEqual(computer.relative_base, 24)
Beispiel #17
0
 def test_adjust_relative_mode_1(self):
     code = [109, 3, 99]
     computer = Intcomputer(code)
     computer.execute()
     self.assertEqual(computer.relative_base, 3)
Beispiel #18
0
 def test_203_input(self):
     code = [203, -6, 99, -1, -1]
     computer = Intcomputer(code)
     computer.relative_base = 10
     status, out = computer.execute(1)
     self.assertEqual(computer.memory[:5], [203, -6, 99, -1, 1])
Beispiel #19
0
 def test_21107_less_than(self):
     code = [21107, 1, 2, -5, 99, -1]
     computer = Intcomputer(code)
     computer.relative_base = 10
     computer.execute()
     self.assertEqual(computer.memory[:6], [21107, 1, 2, -5, 99, 1])
Beispiel #20
0
 def test_jump_if_true_mode_2_b(self):
     code = [205, 3, 6, 0, 99, 1, 4]
     computer = Intcomputer(code)
     computer.relative_base = 2
     computer.execute()
     self.assertTrue(True)
Beispiel #21
0
 def test_command_20001(self):
     code = [20001, 5, 6, -3, 99, 2, 4, 0]
     computer = Intcomputer(code)
     computer.relative_base = 10
     computer.execute()
     self.assertEqual(computer.memory[:8], [20001, 5, 6, -3, 99, 2, 4, 6])
Beispiel #22
0
 def test_jump_if_false_mode_2(self):
     code = [1206, 3, 4, 1, 99, 0]
     computer = Intcomputer(code)
     computer.relative_base = 2
     computer.execute()
     self.assertTrue(True)
Beispiel #23
0
 def test_command_21001(self):
     code = [21001, 5, 1, -4, 99, 2, 0]
     computer = Intcomputer(code)
     computer.relative_base = 10
     computer.execute()
     self.assertEqual(computer.memory[:7], [21001, 5, 1, -4, 99, 2, 3])
Beispiel #24
0
 def test_io(self):
     code = [3, 9, 1002, 9, 2, 10, 4, 10, 99, 0, 0]
     computer = Intcomputer(code)
     status, output = computer.execute(3)
     self.assertEqual(status, 'OUTPUT')
     self.assertEqual(output, 6)
Beispiel #25
0
 def test_command_1105(self):
     code = [1105, 1, 4, 0, 99]
     computer = Intcomputer(code)
     computer.execute()
     self.assertTrue(True)
Beispiel #26
0
 def test_command_1007(self):
     code = [1007, 3, 1, 5, 99, -1]
     computer = Intcomputer(code)
     computer.execute()
     self.assertEqual(computer.memory[:6], [1007, 3, 1, 5, 99, 0])
Beispiel #27
0
 def test_command_1006a(self):
     code = [1006, 3, 4, 0, 99]
     computer = Intcomputer(code)
     computer.execute()
     self.assertTrue(True)
Beispiel #28
0
 def test_command_1008(self):
     code = [1008, 6, 3, 5, 99, -1, 3]
     computer = Intcomputer(code)
     computer.execute()
     self.assertEqual(computer.memory[:7], [1008, 6, 3, 5, 99, 1, 3])
Beispiel #29
0
 def test_command_201(self):
     code = [201, -4, 5, 7, 99, 3, 1, 0]
     computer = Intcomputer(code)
     computer.relative_base = 10
     computer.execute()
     self.assertEqual(computer.memory[:8], [201, -4, 5, 7, 99, 3, 1, 4])
Beispiel #30
0
 def test_command_109(self):
     code = [109, -1, 99]
     computer = Intcomputer(code)
     computer.relative_base = 10
     computer.execute()
     self.assertEqual(computer.relative_base, 9)