コード例 #1
0
 def test_jumping(self):
     # Here are some jump tests that take an input, then output 0 if the input was zero or 1 if the input was
     # non-zero:
     test_comp1 = comp.Computer(output_to_screen=False)
     test_comp1.load_list(
         [3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9])
     test_comp1.input = 0
     test_comp1.run()
     self.assertEqual(test_comp1.output, 0)
     test_comp2 = comp.Computer(output_to_screen=False)
     test_comp2.load_list(
         [3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9])
     test_comp2.input = 4  # Non-zero.
     test_comp2.run()
     self.assertEqual(test_comp2.output, 1)
     test_comp3 = comp.Computer(output_to_screen=False)
     test_comp3.load_list([3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1])
     test_comp3.input = 0
     test_comp3.run()
     self.assertEqual(test_comp3.output, 0)
     test_comp4 = comp.Computer(output_to_screen=False)
     test_comp4.load_list([3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1])
     test_comp4.input = 4  # Non-zero.
     test_comp4.run()
     self.assertEqual(test_comp4.output, 1)
コード例 #2
0
    def test_complex_programs(self):
        test_comp1 = comp.Computer(output_to_screen=False)
        test_comp1.load_list([1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50])
        self.assertEqual(test_comp1.memory[0], 1)
        test_comp1.run()
        self.assertEqual(test_comp1.memory[0], 3500)

        test_comp2 = comp.Computer(output_to_screen=False)
        test_comp2.load_list([1, 1, 1, 4, 99, 5, 6, 0, 99])
        self.assertEqual(test_comp2.memory[0], 1)
        test_comp2.run()
        self.assertEqual(test_comp2.memory[0], 30)
コード例 #3
0
    def test_big_numbers(self):
        test_comp1 = comp.Computer(output_to_screen=False)
        test_comp1.load_list([1102, 34915192, 34915192, 7, 4, 7, 99, 0])
        test_comp1.run()
        self.assertEqual(len(str(test_comp1.output)),
                         16)  # "...should output a 16-digit number."

        test_comp2 = comp.Computer(output_to_screen=False)
        test_comp2.load_list([104, 1125899906842624, 99])
        test_comp2.run()
        self.assertEqual(
            test_comp2.output, 1125899906842624
        )  # "...should output the large number in the middle."
コード例 #4
0
    def test_multiply(self):
        test_comp1 = comp.Computer(output_to_screen=False)
        test_comp1.load_list([2, 3, 0, 3, 99])  # (3 * 2 = 6)
        self.assertEqual(test_comp1.memory[3],
                         3)  # Before program is run, memory[3] should be 3.
        test_comp1.run()
        self.assertEqual(test_comp1.memory[3], 6)

        test_comp2 = comp.Computer(output_to_screen=False)
        test_comp2.load_list([2, 4, 4, 5, 99, 0])  # (99 * 99 = 9801)
        self.assertEqual(test_comp2.memory[5],
                         0)  # Before program is run, memory[5] should be 0.
        test_comp2.run()
        self.assertEqual(test_comp2.memory[5], 9801)
コード例 #5
0
 def test_add(self):
     test_comp = comp.Computer(output_to_screen=False)
     test_comp.load_list([1, 0, 0, 0, 99])  # (1 + 1 = 2)
     self.assertEqual(test_comp.memory[0],
                      1)  # Before program is run, memory[0] should be 1.
     test_comp.run()
     self.assertEqual(test_comp.memory[0], 2)
コード例 #6
0
 def __init__(self, filename: str):
     self.comp = comp.Computer(output_to_screen=False)   # The game needs a computer to run game logic.
     self.comp.load_file(filename)                       # Load program from parm filename into computer memory.
     self.scr = screen.Screen()                          # The game needs a screen.
     self.ball = (0, 0)                                  # Current (x, y) position of the ball.
     self.paddle = (0, 0)                                # Current (x, y) position of the paddle.
     self.score = 0                                      # Player's score.
     self.frames = 0                                     # Number of frames so far.
コード例 #7
0
    def __init__(self):

        self.curr_x, self.curr_y = 0, 0                             # Current coordinates of the droid in the world.

        # Master comp is the machine 'image' that the work comp will be copied from.
        self.master_comp = comp.Computer(output_to_screen=False)
        self.master_comp.load_file('input.txt')

        self.comp = deepcopy(self.master_comp)
コード例 #8
0
 def test_input_output(self):
     test_comp = comp.Computer(output_to_screen=False)
     test_comp.load_list(
         [3, 0, 4, 0,
          99])  # This program puts input into address 0, then outputs it.
     test_comp.input = 7
     self.assertEqual(test_comp.output,
                      0)  # Before program is run, output should be 0.
     test_comp.run()
     self.assertEqual(test_comp.output, 7)
コード例 #9
0
    def test_conditionals(self):
        # 3,9,8,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is equal to 8; output 1 (if it is)
        # or 0 (if it is not).
        test_comp1 = comp.Computer(output_to_screen=False)
        test_comp1.load_list([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8])
        test_comp1.input = 8
        test_comp1.run()
        self.assertEqual(test_comp1.output, 1)
        test_comp2 = comp.Computer(output_to_screen=False)
        test_comp2.load_list([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8])
        test_comp2.input = 9  # This is not 8.
        test_comp2.run()
        self.assertEqual(test_comp2.output, 0)

        # 3,9,7,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is less than 8; output 1 (if it
        # is) or 0 (if it is not).
        test_comp3 = comp.Computer(output_to_screen=False)
        test_comp3.load_list([3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8])
        test_comp3.input = 7  # This is less than 8.
        test_comp3.run()
        self.assertEqual(test_comp3.output, 1)
        test_comp4 = comp.Computer(output_to_screen=False)
        test_comp4.load_list([3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8])
        test_comp4.input = 9  # This is not less than 8.
        test_comp4.run()
        self.assertEqual(test_comp4.output, 0)

        # 3,3,1108,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is equal to 8; output 1 (if it is)
        # or 0 (if it is not).
        test_comp5 = comp.Computer(output_to_screen=False)
        test_comp5.load_list([3, 3, 1108, -1, 8, 3, 4, 3, 99])
        test_comp5.input = 8
        test_comp5.run()
        self.assertEqual(test_comp5.output, 1)
        test_comp6 = comp.Computer(output_to_screen=False)
        test_comp6.load_list([3, 3, 1108, -1, 8, 3, 4, 3, 99])
        test_comp6.input = 3  # This is not 8.
        test_comp6.run()
        self.assertEqual(test_comp6.output, 0)

        # 3,3,1107,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is less than 8; output 1 (if it is)
        # or 0 (if it is not).
        test_comp7 = comp.Computer(output_to_screen=False)
        test_comp7.load_list([3, 3, 1107, -1, 8, 3, 4, 3, 99])
        test_comp7.input = 5  # This is less than 8.
        test_comp7.run()
        self.assertEqual(test_comp7.output, 1)
        test_comp8 = comp.Computer(output_to_screen=False)
        test_comp8.load_list([3, 3, 1107, -1, 8, 3, 4, 3, 99])
        test_comp8.input = 20  # This is greater than 8.
        test_comp8.run()
        self.assertEqual(test_comp8.output, 0)
コード例 #10
0
 def test_relative_mode(self):
     # "...takes no input and produces a copy of itself as output."
     test_comp1 = comp.Computer(output_to_screen=False)
     program = [
         109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101,
         0, 99
     ]
     test_comp1.load_list(program)
     for i in program:
         test_comp1.run_until_output()
         self.assertEqual(i, test_comp1.output)
コード例 #11
0
    def test_day_2_program(self):
        test_comp = comp.Computer(output_to_screen=False)
        test_comp.load_file('day_2_program.txt')

        # "Replace position 1 with the value 12 and replace position 2 with the value 2"
        test_comp.memory[1] = 12
        test_comp.memory[2] = 2

        test_comp.run()
        self.assertEqual(test_comp.memory[0],
                         5482655)  # This was the solution to Part 1 of Day 2.
コード例 #12
0
 def test_large_example(self):
     # Uses an input instruction to ask for a single number. The program will then output 999 if the input value is
     # below 8, output 1000 if the input value is equal to 8, or output 1001 if the input value is greater than 8.
     test_comp1 = comp.Computer(output_to_screen=False)
     test_comp1.load_list([
         3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31,
         1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104,
         999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99
     ])
     test_comp1.input = 7  # Below 8.
     test_comp1.run()
     self.assertEqual(test_comp1.output, 999)
コード例 #13
0
 def test_immediate_mode(self):
     test_comp1 = comp.Computer(output_to_screen=False)
     test_comp1.load_list([1002, 4, 3, 4, 33])
     self.assertEqual(test_comp1.memory[4], 33)
     test_comp1.run()
     self.assertEqual(test_comp1.memory[4], 99)