Exemple #1
0
 def test_prog_7(self):
     # input
     program = Program([3, 2, 0, 4, 0])
     program.inputs.extend([3, 99])
     program.run()
     self.assertEqual(program.outputs, deque())
     self.assertEqual(program.memory, [3, 2, 3, 4, 99])
Exemple #2
0
def solve_part_2(puzzle_input):
    prog = Program(puzzle_input)
    prog.memory[0] = 2
    game = BreakoutGame(prog)
    game.play(rendered=False, interactive=False)

    return game.score
Exemple #3
0
 def test_prog_19(self):
     # Big ints
     program = Program('1102,34915192,34915192,7,4,7,99,0')
     program.run()
     self.assertEqual(len(program.outputs), 1)
     self.assertEqual(len(str(program.outputs[0])),
                      16)  # day 9 "should output a 16-digit number"
Exemple #4
0
def solve_part_2(puzzle_input):
    program = Program(puzzle_input)

    for noun in range(100):
        for verb in range(100):
            program.reset()
            if run_program(program, noun, verb) == 19690720:
                return 100 * noun + verb
Exemple #5
0
 def test_prog_18(self):
     # relative mode and expanding memory beyond initial size
     program = Program(
         '109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99')
     program.run()
     self.assertEqual(list(program.outputs), [
         109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101,
         0, 99
     ])
Exemple #6
0
    def __init__(self, program, num_stages, feedback_loop=False):
        if type(program) != Program:
            program = Program(program)

        self.num_stages = num_stages
        self.feedback_loop = feedback_loop

        self.programs = []
        for _ in range(num_stages):
            self.programs.append(deepcopy(program))
Exemple #7
0
 def test_reset(self):
     ints = [109, -3, 2002, 6, 9, 7, 99, 0]
     program = Program(ints)
     program.run()
     self.assertNotEqual(program.ip, 0)
     self.assertNotEqual(program.memory, ints)
     program.reset()
     self.assertEqual(program.memory, ints)
     self.assertEqual(program.ip, 0)
     program.run()
     self.assertNotEqual(program.ip, 0)
     self.assertEqual(program.memory, [109, -3, 2002, 6, 9, 7, 99, 9801])
Exemple #8
0
    def __init__(self, robot_program, starting_colour=0):
        if not isinstance(robot_program, Program):
            robot_program = Program(robot_program)
        self.program = robot_program
        self.hull = defaultdict(int)
        self.hull[(0, 0)] = starting_colour
        self.painted = set()

        self.x = 0
        self.y = 0
        self.direction = [0, 1]
Exemple #9
0
    def test_hull_painting_bot(self):
        # Mock the example program from the puzzle description
        program = Mock(spec=Program('0,1,2,3'))
        program.outputs = deque([1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0])
        program.inputs = deque()
        halted_values = [False] * 7 + [True]
        type(program).halted = PropertyMock(side_effect=halted_values)

        bot = HullPaintingBot(program)
        bot.run()

        self.assertEqual(program.inputs, deque([0, 0, 0, 0, 1, 0, 0]))
        self.assertEqual(bot.hull, {(0, 0): 0,
                                    (-1, 0): 0,
                                    (-1, -1): 1,
                                    (0, -1): 1,
                                    (1, 0): 1,
                                    (1, 1): 1})
        self.assertEqual(bot.painted, {(0, 0), (-1, 0), (-1, -1), (0, -1), (1, 0), (1, 1)})
Exemple #10
0
    def test_prog_17(self):
        # multiple inputs, not all at once
        program = Program([
            3, 5, 3, 6, 1102, -1, -1, 9, 104, -1, 3, 11, 1, 11, 0, 17, 4, -1,
            99
        ])

        program.inputs.extend([2, 10])
        program.run()
        self.assertEqual(list(program.outputs), [20])
        self.assertFalse(program.halted)
        self.assertEqual(program.inputs, deque())

        program.inputs.append(5)
        program.run()
        self.assertEqual(list(program.outputs), [20, 104])
        self.assertTrue(program.halted)
        self.assertEqual(program.inputs, deque())
Exemple #11
0
 def test_prog_3(self):
     # multiplication, input as string
     program = Program('2,4,4,5,99,0')
     program.run()
     self.assertEqual(program.memory, [2, 4, 4, 5, 99, 9801])
Exemple #12
0
 def test_prog_4(self):
     program = Program(self.input_program_4)
     amp = AmplifierSeries(program, 5, feedback_loop=True)
     self.assertEqual(amp.run([9, 8, 7, 6, 5]), 139629729)
Exemple #13
0
 def test_prog_2(self):
     program = Program(self.input_program_2)
     amp = AmplifierSeries(program, 5)
     self.assertEqual(amp.run([0, 1, 2, 3, 4]), 54321)
Exemple #14
0
 def test_prog_20(self):
     # Big ints
     program = Program('104,1125899906842624,99')
     program.run()
     self.assertEqual(len(program.outputs), 1)
     self.assertEqual(program.outputs[0], 1125899906842624)
Exemple #15
0
 def test_prog_4(self):
     # multiplication and addition
     program = Program([1, 1, 1, 4, 99, 5, 6, 0, 99])
     program.run()
     self.assertEqual(program.memory, [30, 1, 1, 4, 2, 5, 6, 0, 99])
Exemple #16
0
    def test_prog_16(self):
        # slightly larger test of opcodes 1--8, 99. Program outputs 999 if input is below 8, 1000 if input equals 8 and
        # 1001 if input is greater than 8.
        program = Program(
            '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')

        program.inputs.append(0)
        program.run()
        self.assertEqual(list(program.outputs), [999])

        program.reset()
        program.inputs.append(7)
        program.run()
        self.assertEqual(list(program.outputs), [999])

        program.reset()
        program.inputs.append(8)
        program.run()
        self.assertEqual(list(program.outputs), [1000])

        program.reset()
        program.inputs.append(9)
        program.run()
        self.assertEqual(list(program.outputs), [1001])

        program.reset()
        program.inputs.append(70)
        program.run()
        self.assertEqual(list(program.outputs), [1001])
Exemple #17
0
def solve_part_1(puzzle_input):
    program = Program(puzzle_input)
    program.inputs.append(1)
    program.run()
    return program.outputs.pop()
Exemple #18
0
 def test_prog_6(self):
     # immediate mode
     program = Program('102,3,4,4,33')
     program.run()
     self.assertEqual(program.memory, [102, 3, 4, 4, 99])
Exemple #19
0
def solve_part_1(puzzle_input):
    prog = Program(puzzle_input)
    game = BreakoutGame(prog)
    game.play(rendered=False, interactive=False)

    return game.num_remaining_block_tiles
Exemple #20
0
 def test_prog_8(self):
     # output
     program = Program([101, -93, 0, 5, 4, -1, 4, 0, 99])
     program.run()
     self.assertEqual(list(program.outputs), [99, 101])
     self.assertEqual(program.memory, [101, -93, 0, 5, 4, 8, 4, 0, 99])
Exemple #21
0
def solve_part(puzzle_input, input_val):
    program = Program(puzzle_input)
    program.inputs.append(input_val)
    program.run()
    return program.outputs.pop()
Exemple #22
0
 def test_prog_9(self):
     # input and output
     program = Program('3,0,4,0,99')
     program.inputs.append(1337)
     program.run()
     self.assertEqual(list(program.outputs), [1337])
Exemple #23
0
def solve_part_1(puzzle_input):
    program = Program(puzzle_input)

    return run_program(program, 12, 2)
Exemple #24
0
    def test_prog_10(self):
        # equals, position mode. Program tests whether input equals 8.
        program = Program('3,9,8,9,10,9,4,9,99,-1,8')

        program.inputs.append(8)
        program.run()
        self.assertEqual(list(program.outputs), [1])

        program.reset()
        program.inputs.append(-8)
        program.run()
        self.assertEqual(list(program.outputs), [0])

        program.reset()
        program.inputs.append(4)
        program.run()
        self.assertEqual(list(program.outputs), [0])

        program.reset()
        program.inputs.append(40)
        program.run()
        self.assertEqual(list(program.outputs), [0])
Exemple #25
0
 def test_prog_1(self):
     program = Program(self.input_program_1)
     amp = AmplifierSeries(program, 5)
     self.assertEqual(amp.run([4, 3, 2, 1, 0]), 43210)
Exemple #26
0
 def test_prog_1(self):
     # addition
     program = Program([1, 0, 0, 0, 99])
     program.run()
     self.assertEqual(program.memory, [2, 0, 0, 0, 99])
Exemple #27
0
 def test_prog_3(self):
     program = Program(self.input_program_3)
     amp = AmplifierSeries(program, 5)
     self.assertEqual(amp.run([1, 0, 4, 3, 2]), 65210)
Exemple #28
0
    def test_prog_11(self):
        # equals, immediate mode. Program tests whether input equals 8.
        program = Program('3,3,1108,-1,8,3,4,3,99')

        program.inputs.append(8)
        program.run()
        self.assertEqual(list(program.outputs), [1])

        program.reset()
        program.inputs.append(-8)
        program.run()
        self.assertEqual(list(program.outputs), [0])

        program.reset()
        program.inputs.append(4)
        program.run()
        self.assertEqual(list(program.outputs), [0])

        program.reset()
        program.inputs.append(40)
        program.run()
        self.assertEqual(list(program.outputs), [0])
Exemple #29
0
 def test_prog_5(self):
     program = Program(self.input_program_5)
     amp = AmplifierSeries(program, 5, feedback_loop=True)
     self.assertEqual(amp.run([9, 7, 8, 5, 6]), 18216)
Exemple #30
0
    def test_prog_15(self):
        # jumps, immediate mode. Program tests whether input is nonzero
        program = Program('3,3,1105,-1,9,1101,0,0,12,4,12,99,1')

        program.inputs.append(0)
        program.run()
        self.assertEqual(list(program.outputs), [0])

        program.reset()
        program.inputs.append(1)
        program.run()
        self.assertEqual(list(program.outputs), [1])

        program.reset()
        program.inputs.append(-1)
        program.run()
        self.assertEqual(list(program.outputs), [1])