コード例 #1
0
def program_springbot(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))

    p = IntCode(raw_program)
    done = p.run(True)
    print('done?', done)
    render(p.outputs)

    instructions = [
        "NOT A T",
        "NOT B J",
        "OR T J",
        "NOT C T",
        "OR T J",
        "AND D J",
        "WALK"
    ]
    instructions_encoded = []
    for instruction in instructions:
        instructions_encoded += [ord(c) for c in instruction+"\n"]

    p.inputs = instructions_encoded
    done = p.run(True)
    print('done?', done)
    return render(p.outputs)
コード例 #2
0
ファイル: part1.py プロジェクト: tjvick/advent-of-code
def do_the_thing(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))

    total_affected = 0
    for y in range(50):
        for x in range(50):
            p = IntCode(raw_program)
            p.inputs = [x, y]
            out, done = p.run_io([x, y])
            total_affected += out

    return total_affected
コード例 #3
0
def do_the_thing(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))
    raw_program[0] = 2

    p = IntCode(raw_program)
    p.inputs = []

    done = False
    while not done:
        done = p.run(True)
        # if not done:
        #     print('Still Playing...')
        instructions = p.outputs

        n_blocks = 0
        # screen = np.full([20, 50], ' ', dtype=object)

        current_score = 0
        ball_pos = 0
        paddle_pos = 0
        ix = 0
        while ix < len(instructions):
            ic = instructions[ix]
            ir = instructions[ix + 1]
            tile_id = instructions[ix + 2]
            if tile_id == 2:
                n_blocks += 1
            ix += 3
            if tile_id == 4:
                ball_pos = ic
            if tile_id == 3:
                paddle_pos = ic

            if ic == -1 and ir == 0:
                current_score = tile_id
            # else:
            # screen[ir, ic] = paint(tile_id)

        # draw(screen)
        # print(current_score)

        if not done:
            joystick_command = 0
            if ball_pos > paddle_pos:
                joystick_command = 1
            elif ball_pos < paddle_pos:
                joystick_command = -1
            p.inputs.append(joystick_command)

    return current_score
コード例 #4
0
ファイル: part2.py プロジェクト: tjvick/advent-of-code
def program_springbot(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))

    p = IntCode(raw_program)
    p.run(True)

    instructions = [
        "NOT T T", "AND A T", "AND B T", "AND C T", "NOT T T", "AND D T",
        "OR E J", "OR H J", "AND T J", "RUN"
    ]

    instructions_encoded = []
    for instruction in instructions:
        instructions_encoded += [ord(c) for c in instruction + "\n"]

    p.inputs = instructions_encoded
    p.run(True)
    return render(p.outputs)
コード例 #5
0
ファイル: part1.py プロジェクト: tjvick/advent-of-code
def do_the_thing(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))

    p = IntCode(raw_program)

    p.inputs = []
    _ = p.run()
    instructions = p.outputs

    n_blocks = 0
    ix = 0
    while ix < len(instructions):
        tile_id = instructions[ix + 2]
        if tile_id == 2:
            n_blocks += 1
        ix += 3

    return n_blocks
コード例 #6
0
def do_the_thing(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))
    p = IntCode(raw_program)

    grid = dict()
    position = (0, 0)
    grid[position] = 1
    facing = 0
    done = False
    while not done:
        see = grid[position] if position in grid else 0

        p.inputs = [see]
        done = p.run(True)

        color = p.outputs.pop(0)
        turn_direction = p.outputs.pop(0)

        # Paint
        grid[position] = color

        # Turn
        turn = -1 if turn_direction == 0 else 1
        facing = (facing + turn) % 4

        # Move
        if facing == 0:
            position = (position[0], position[1]-1)
        elif facing == 1:
            position = (position[0]+1, position[1])
        elif facing == 2:
            position = (position[0], position[1]+1)
        elif facing == 3:
            position = (position[0]-1, position[1])

        if done:
            print('done')

    paint(grid)