示例#1
0
def test_relative_mode():
    subject = Interpreter([109, 5, 109, -4, 204, 3, 99])

    subject.run()

    assert subject._rbo == 1
    assert subject.stdout.get() == 204
示例#2
0
def solve():
    subject = Interpreter.from_file('input.txt')
    subject.put(1)
    subject.run()
    print('Part 1:', subject.get())

    subject = Interpreter.from_file('input.txt')
    subject.put(2)

    with Timer() as t:
        subject.run()
    print('Part 2:', subject.get(), f'finished in {t.time()} sec')
示例#3
0
    def part_2():
        # Part 2
        interpreter = Interpreter.from_file('input.txt')
        interpreter[0] = 2

        interpreter.start()
        game = {}
        canvas = Canvas(game, interpreter.stdin)
        score = 0

        def run():
            while not (interpreter.finished and interpreter.stdout.empty()):
                x = interpreter.get()
                y = interpreter.get()
                tile_type = interpreter.get()

                if x == -1:
                    score = tile_type
                    print('Score', score)

                vec = Vector(x, y)
                game[vec] = tile_type

        Thread(target=run, daemon=True).start()

        arcade.run()
示例#4
0
def solve(file):
    interpreter = Interpreter.from_file(file)
    interpreter.run()

    field = ''
    while data := interpreter.get():
        field += chr(data)
示例#5
0
def test_day_9_quine_test(capsys):
    program = [
        109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0,
        99
    ]
    subject = Interpreter(program)

    subject.run()
    # subject.DEBUG = True
    # with capsys.disabled():
    #     print()
    #     print(f'  MEM| (IC: {subject._ic}, RBO: {subject._rbo})', subject[:])
    #     while not subject.finished:
    #         subject.step()
    #         print(f'  MEM| (IC: {subject._ic}, RBO: {subject._rbo})', subject[:])

    assert list(subject.stream()) == program
示例#6
0
def new_inter(address):
    inter = Interpreter.from_file('input.txt')
    inter.stdin = NetQueue()
    # inter.DEBUG = True
    inter._log_prefix = str(address)

    inter.put(address)
    inter.start()
    return inter
示例#7
0
    def part_1():
        interpreter = Interpreter.from_file('input.txt')
        interpreter.start()

        game = {}
        while not interpreter.finished:
            x = interpreter.get()
            y = interpreter.get()
            tile_type = interpreter.get()

            game[Vector(x, y)] = tile_type
        print(len([x for x in game.values() if x == 2]))
示例#8
0
def test_set():
    subject = Interpreter([3, 0, 99])
    subject.put(5)

    subject.run()

    assert subject[0] == 5
示例#9
0
    def execute(combination):
        # create amplifier
        _in = [
            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
        ]
        amplifiers = [
            Interpreter.from_file('input.txt'),
            Interpreter.from_file('input.txt'),
            Interpreter.from_file('input.txt'),
            Interpreter.from_file('input.txt'),
            Interpreter.from_file('input.txt'),

            # test data
            # Interpreter(_in[:]),
            # Interpreter(_in[:]),
            # Interpreter(_in[:]),
            # Interpreter(_in[:]),
            # Interpreter(_in[:]),
        ]

        # connect them
        for x in range(5):
            amplifiers[x - 1].stdout = amplifiers[x].stdin

        # fill phase input
        for phase, amp in zip(combination, amplifiers):
            amp.stdin.put(phase)

        # Add initial value
        amplifiers[0].put(0)

        for thread in [amp.start() for amp in amplifiers]:
            thread.join()

        return next(amplifiers[-1].stream())
示例#10
0
def solve():
    COLORS = {
        0: arcade.color.BLACK,
        1: arcade.color.WHITE,
    }

    # for
    # 0 paint black
    # 1 paint white
    # 0 left, 1 right

    robot = Robot(Interpreter.from_file('input.txt'))
    canvas = Canvas(robot)

    def rob_controller():
        robot.start()
        ship = {
            Vector(0, 0): 1  # Part 2
        }

        while not robot.finished():
            robot.put(ship.get(robot.pos, 0))

            color = robot.get()
            turn = robot.get()

            # Painting

            canvas.new_paintings.append((robot.pos, COLORS[color]))
            ship[robot.pos] = color

            # Turn
            if turn == 0:
                robot.turn_left()
            elif turn == 1:
                robot.turn_right()

            robot.move()
            sleep(canvas.delay)

        print(len(ship))
        canvas.draw_sprites = False

    Thread(target=rob_controller, daemon=True).start()
    arcade.run()
示例#11
0
def solve():
    # Part 1
    # interpreter = Interpreter.from_file('input.txt')
    #
    # interpreter.put(1)
    # interpreter.run()
    #
    # for out in interpreter.stream():
    #     print(out)

    # Part 2
    interpreter = Interpreter.from_file('input.txt')

    interpreter.put(5)
    interpreter.run()

    for out in interpreter.stream():
        print(out)
示例#12
0
def solve(file):
    inter = Interpreter.from_file('input.txt')
    inter.start()

    with open(file) as f:
        instructions = f.readlines()

    for instruction in instructions:
        instruction = instruction.strip()
        if len(instruction) == 0 or instruction[0] == '#':
            continue

        for char in instruction:
            inter.put(ord(char))
        inter.put(10)

    while not inter.finished or not inter.stdout.empty():

        value = inter.get()
        if value is not None:
            if value <= 128:
                print(chr(value), end='')
            else:
                print(value)
示例#13
0
def run(canvas: Canvas):
    robot = Interpreter.from_file('input.txt')
    robot.start()

    cur_pos = Vector(0, 0)
    canvas.set(cur_pos, 1)
    # cur_dir = 'N'
    ship = dict()
    algo = Algo()
    oxygen_pos = None

    long_road = []

    while True:
        # walk
        old_cd = algo.cd
        cmd, vec = algo.next(ship, cur_pos)
        if cmd is None:
            continue
        next_pos = cur_pos + vec

        # manual
        # key = canvas.get_key_event()
        # while not (cmd := key_map.get(key)):
        #     pass
        # next_pos = cur_pos + dir_map[cmd]

        # sleep(0.1)
        robot.put(cmd)

        # process output
        result = robot.get()
        if result == 0:
            # hit wall, reset direction of algo
            ship[next_pos] = WALL
            canvas.add(next_pos)
            algo.cd = old_cd
        elif result in (1, 2):
            # moved
            canvas.set(next_pos, 1)
            canvas.remove(cur_pos)
            cur_pos = next_pos

            if oxygen_pos is None:  # record path
                if next_pos in set(long_road):
                    start_index = long_road.index(next_pos)
                    long_road = long_road[:start_index]

                long_road.append(next_pos)

        if result == 2:
            # found oxygen
            print(f'Found oxygen at {next_pos} {len(long_road)}')
            if oxygen_pos:
                break
            oxygen_pos = next_pos

    # calc distance
    # graph = SetGraph({key for key, value in ship.items() if value == WALL})
    # robot_path = a_star_search(graph, Vector(0, 0), oxygen_pos)
    # print(len(robot_path))
    print(long_road)

    with open('./mace.json', 'wt') as f:
        json.dump([(key.x, key.y) for key, value in ship.items() if value == WALL], f)
示例#14
0
def test_day_9_more_tests(program, result):
    subject = Interpreter(program)
    subject.run()
    assert subject.get() == result
示例#15
0
 def execute(phase, _in):
     interpreter = Interpreter.from_file('input.txt')
     interpreter.put(phase)
     interpreter.put(_in)
     interpreter.run()
     return list(interpreter.stream())[0]
示例#16
0
def test_day_5_jump_test(program, param, result):
    subject = Interpreter(program)
    subject.put(param)
    subject.run()
    assert subject.get() == result
示例#17
0
def test_read_from_somewhere():
    subject = Interpreter([4, 100, 99])

    subject.run()

    assert subject.stdout.get() == 0
示例#18
0
def test_add():
    subject = Interpreter([1, 0, 0, 0, 99])

    subject.run()

    assert subject[0] == 2
示例#19
0
def new_inter():
    inter = Interpreter.from_file('input.txt')
    return inter
示例#20
0
def test_out():
    subject = Interpreter([4, 0, 99])

    subject.run()

    assert subject.stdout.get() == 4
示例#21
0
def test_multiply_param_modes():
    subject = Interpreter([1002, 4, 3, 4, 33])

    subject.run()

    assert subject[:] == (1002, 4, 3, 4, 99)
示例#22
0
def test_multiply():
    subject = Interpreter([2, 3, 0, 3, 99])

    subject.run()

    assert subject[3] == 6
示例#23
0
def receiver(package_queue: Queue, inter: Interpreter):
    while not inter.finished:
        dest, x, y = inter.get(), inter.get(), inter.get()
        package_queue.put(Packet(dest, x, y))