Ejemplo n.º 1
0
def test_jump_in_immediate_mode():
    program = parse_program('3,3,1105,-1,9,1101,0,0,12,4,12,99,1')
    c = Computer(program)
    c.push_input(0)
    c.execute()
    assert c.pop_output() == 0

    program = parse_program('3,3,1105,-1,9,1101,0,0,12,4,12,99,1')
    c = Computer(program)
    c.push_input(80)
    c.execute()
    assert c.pop_output() == 1
Ejemplo n.º 2
0
def test_jump_in_pos_mode():
    program = parse_program('3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9')
    c = Computer(program)
    c.push_input(0)
    c.execute()
    assert c.pop_output() == 0

    program = parse_program('3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9')
    c = Computer(program)
    c.push_input(80)
    c.execute()
    assert c.pop_output() == 1
Ejemplo n.º 3
0
def test_example3():
    program = parse_program(
        '3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,'
        '1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0')
    settings, signal = AmpSequence.find_max_settings(program)
    assert signal == 65210
    assert settings == [1, 0, 4, 3, 2]
Ejemplo n.º 4
0
def test_inout_opcodes():
    program = parse_program('3,0,4,0,99')
    c = Computer(program)
    c.push_input(27)
    c.execute()
    output = c.pop_output()
    assert output == 27
Ejemplo n.º 5
0
def test_example2():
    program = parse_program('3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,'
                            '1005,55,26,1001,54,-5,54,1105,1,12,1,53,54,53,'
                            '1008,54,0,55,1001,55,1,55,2,53,55,53,4,53,1001,'
                            '56,-1,56,1005,56,6,99,0,0,0,0,10')
    settings, signal = AmpSequence.find_max_feedback_loop_settings(program)
    assert signal == 18216
    assert settings == [9, 7, 8, 5, 6]
Ejemplo n.º 6
0
def test_large_input():
    program = parse_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'
    )
    c = Computer(program)
    c.push_input(6)
    c.execute()
    assert c.pop_output() == 999

    program = parse_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'
    )
    c = Computer(program)
    c.push_input(8)
    c.execute()
    assert c.pop_output() == 1000

    program = parse_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'
    )
    c = Computer(program)
    c.push_input(10)
    c.execute()
    assert c.pop_output() == 1001
Ejemplo n.º 7
0
def test_not_less_than_8_immediate_mode():
    program = parse_program('3,3,1107,-1,8,3,4,3,99')
    c = Computer(program)
    c.push_input(8)
    c.execute()
    assert c.pop_output() == 0
Ejemplo n.º 8
0
def test_equal_to_8_pos_mode():
    program = parse_program('3,9,8,9,10,9,4,9,99,-1,8')
    c = Computer(program)
    c.push_input(8)
    c.execute()
    assert c.pop_output() == 1
Ejemplo n.º 9
0
def test_not_equal_to_8_immediate_mode():
    program = parse_program('3,3,1108,-1,8,3,4,3,99')
    c = Computer(program)
    c.push_input(7)
    c.execute()
    assert c.pop_output() == 0
Ejemplo n.º 10
0
def test_not_less_than_8_pos_mode():
    program = parse_program('3,9,7,9,10,9,4,9,99,-1,8')
    c = Computer(program)
    c.push_input(8)
    c.execute()
    assert c.pop_output() == 0
Ejemplo n.º 11
0
            phase_setting = perm[amplifier_index]
            exit_code = computers[amplifier_index].run()

            if exit_code != "interrupt" and amplifier_index == 4:
                break

            amplifier_index = (amplifier_index + 1) % amp_count

            its += 1

    return max_output


args, lines = parse_args_and_get_input()

if args.part_one:
    test_code_immutable = parse_program(
        "3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0")
    assert_equal(43210, find_max_output_no_feedback(test_code_immutable))
    test_code_immutable2 = parse_program(
        "3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0"
    )
    assert_equal(54321, find_max_output_no_feedback(test_code_immutable2))
    print(find_max_output_no_feedback(parse_program(lines[0])))
else:
    test_code_immutable = parse_program(
        "3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5"
    )
    assert_equal(139629729, find_max_output_with_feedback(test_code_immutable))
    print(find_max_output_with_feedback(parse_program(lines[0])))
Ejemplo n.º 12
0
def test_negatives():
    program = parse_program('1101,100,-1,4,0')
    c = Computer(program)
    c.execute()
    assert c.state == [1101, 100, -1, 4, 99]
Ejemplo n.º 13
0
def test_modes():
    program = parse_program('1002,4,3,4,33')
    c = Computer(program)
    c.execute()
    assert c.state == [1002, 4, 3, 4, 99]
Ejemplo n.º 14
0
def test_example1():
    program = parse_program('3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,'
                            '27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5')
    settings, signal = AmpSequence.find_max_feedback_loop_settings(program)
    assert signal == 139629729
    assert settings == [9, 8, 7, 6, 5]
Ejemplo n.º 15
0
def test_example1():
    program = parse_program('3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0')
    settings, signal = AmpSequence.find_max_settings(program)
    assert signal == 43210
    assert settings == [4, 3, 2, 1, 0]
Ejemplo n.º 16
0
                                         self.current_pos.y)

            self.next_ins_is_direction = False
        else:
            self.been_painted.add(self.current_pos)
            if ins:
                self.white_panels.add(self.current_pos)
            else:
                self.white_panels.discard(self.current_pos)

            self.next_ins_is_direction = True


robot = Robot()
args, lines = parse_args_and_get_input()
code_immutable = parse_program(lines[0])

if args.part_one:
    IntComputer(code_immutable, robot.get_current_color,
                robot.receive_instruction).run()
    print(len(robot.been_painted))
else:
    robot.white_panels.add(Point(0, 0))
    IntComputer(code_immutable, robot.get_current_color,
                robot.receive_instruction).run()

    paint = ""
    for y in range(8):
        for x in range(80):
            if Point(x, y) in robot.white_panels:
                paint += "#"
Ejemplo n.º 17
0
def test_example2():
    program = parse_program('3,23,3,24,1002,24,10,24,1002,23,-1,23,'
                            '101,5,23,23,1,24,23,23,4,23,99,0,0')
    settings, signal = AmpSequence.find_max_settings(program)
    assert signal == 54321
    assert settings == [0, 1, 2, 3, 4]