示例#1
0
def part1(screen, program):
    curses.curs_set(False)
    com = IntcodeComputer(program)
    it = com.run()
    start_pos = (25, 26)
    pos = start_pos
    grid = {pos: '.'}
    direction = 1
    while len(grid) < 1659:  # todo: stop when repeating path
        grid = get_nbs(pos, grid, com, it)
        wall_on_left_side = grid[get_position(pos,
                                              turn_left[direction])] == '#'
        if not wall_on_left_side:
            direction = turn_left[direction]
        pos, res, grid = move(pos, direction, grid, com, it)
        if res == 0:  # hit wall
            direction = turn_right[direction]
        draw(screen, grid, pos, direction)
    end_pos = next(k for k, v in grid.items() if v == '!')
    steps_min = bfs(start_pos, end_pos, grid)
    screen.addstr(2, 0,
                  f'min steps from {start_pos} to {end_pos}: {steps_min}')
    steps_oxygen = fill_oxygen_steps_required(grid, end_pos, screen)
    screen.addstr(3, 0, f'steps to fill area with oxygen: {steps_oxygen}')
    screen.getch()
示例#2
0
def main(program):
    pos = (0, 0)
    direction = Direction.UP
    painted = defaultdict(int)
    painted[pos] = 1

    computer = IntcodeComputer(program)
    com_iter = computer.run()
    try:
        while True:
            computer.inputs.append(painted[pos])

            color = next(com_iter)
            painted[pos] = color

            turn_dir = next(com_iter)
            direction = turn(direction, turn_dir)
            pos = move(pos, direction)
    except StopIteration:
        pass

    for y in range(20):
        for x in range(80):
            pos = (x, y)
            value = '#' if pos in painted and painted[pos] == 1 else ' '
            print(value, end='')
        print()
示例#3
0
def main(program, perms):
    max_output = 0
    for phases in [''.join(p) for p in permutations(perms)]:
        phases = [int(p) for p in list(phases)]
        is_first_instruction = True
        computers = []
        computers_to_iters = {}
        for phase in phases:
            c = IntcodeComputer(program)
            c.inputs.append(phase)
            computers.append(c)
            computers_to_iters[c] = c.run()
        try:
            while True:
                for i, phase in enumerate(phases):
                    c = computers[i]
                    c.inputs.append(0 if is_first_instruction else last_output)
                    is_first_instruction = False

                    last_output = next(computers_to_iters[c])
                    max_output = max(last_output, max_output)
        except StopIteration:
            pass

    print(max_output)
示例#4
0
def main(program):
    c1 = IntcodeComputer(program)
    c1.inputs.append(1)
    print(f'part1: {next(c1.run())}')

    c2 = IntcodeComputer(program)
    c2.inputs.append(2)
    print(f'part2: {next(c2.run())}')
示例#5
0
def part1(program):
    computer = IntcodeComputer(program)
    computer.program[1] = 12
    computer.program[2] = 2
    it = computer.run()
    try:
        while True:
            next(it)
    except StopIteration:
        output = computer.program[0]
        print(f'part1: {output}')
示例#6
0
def main(program, indata):
    computer = IntcodeComputer(program)
    computer.inputs.append(indata)
    it = computer.run()

    last_output = None
    try:
        while True:
            last_output = next(it)
    except StopIteration:
        print(last_output)
示例#7
0
def part1(program):
    computer = IntcodeComputer(program)
    it = computer.run()

    try:
        block_count = 0
        while True:
            _, _, tile = next(it), next(it), Tile(next(it))
            if tile == Tile.BLOCK:
                block_count += 1
    except StopIteration:
        pass

    print(f'part1: {block_count}')
示例#8
0
def game(screen, program):
    curses.curs_set(False)

    program[0] = 2
    computer = IntcodeComputer(program)
    it = computer.run()

    try:
        count = 0
        ball_x = None
        paddle_x = None

        while True:
            x, y, output = next(it), next(it), next(it)

            if x == -1 and y == 0:
                screen.addstr(21, 0, f'Score: {output}')
            else:
                tile = Tile(output)
                if tile == Tile.EMPTY:
                    char = ' '
                elif tile == Tile.WALL:
                    char = '#'
                elif tile == Tile.BLOCK:
                    char = '@'
                elif tile == Tile.PADDLE:
                    char = '='
                    paddle_x = x
                elif tile == Tile.BALL:
                    char = '.'
                    ball_x = x
                screen.addstr(y, x, char)

                if ball_x is not None and paddle_x is not None:
                    if paddle_x < ball_x:
                        joystick = 1
                    elif paddle_x > ball_x:
                        joystick = -1
                    else:
                        joystick = 0
                    computer.inputs = [joystick]

            screen.refresh()
            sleep(0 if count < 760 else 0.01)
            count += 1

    except StopIteration:
        pass
示例#9
0
def part2(program):
    expected = 19690720
    for noun in range(100):
        for verb in range(100):
            computer = IntcodeComputer(program)
            computer.program[1] = noun
            computer.program[2] = verb
            it = computer.run()

            try:
                while True:
                    next(it)
            except StopIteration:
                output = computer.program[0]
                if output == expected:
                    print(f'part2: {100 * noun + verb}')
示例#10
0
def part2(program):
    program[0] = 2
    computer = IntcodeComputer(program)
    computer.inputs = get_movement_pattern()
    it = computer.run()
    try:
        while True:
            output = next(it)
            if output == 10:
                print()
            elif output > 255:
                print(output)
            else:
                print(chr(output), end='')
    except StopIteration:
        pass
示例#11
0
def part1(program):
    computer = IntcodeComputer(program)
    it = computer.run()

    # !(A and B and C) and D
    instructions = [
        'OR A J',
        'AND B J',
        'AND C J',
        'NOT J J',
        'AND D J',
        'WALK']
    for instruction in instructions:
        for char in instruction:
            computer.inputs.append(ord(char))
        computer.inputs.append(10)

    run_until_stop(it)
示例#12
0
def part1(program):
    computer = IntcodeComputer(program)
    it = computer.run()
    rows = get_camera_feed(it)

    intersections = set()
    scaffold = ord('#')
    for y, line in enumerate(rows):
        for x, char in enumerate(line):
            try:
                if char == scaffold and \
                    line[x-1]    == scaffold and line[x+1]    == scaffold and \
                    rows[y-1][x] == scaffold and rows[y+1][x] == scaffold:
                        intersections.add((x, y))
            except IndexError:
                pass

    print(f'Part 1: {sum([x * y for x, y in intersections])}')
示例#13
0
def part2(program):
    computer = IntcodeComputer(program)
    it = computer.run()

    # (!(A and B and C) and D) and (E or H)
    instructions = [
        'OR A J',
        'AND B J',
        'AND C J',
        'NOT J J',
        'AND D J',
        'OR E T',
        'OR H T',
        'AND T J',
        'RUN']
    for instruction in instructions:
        for char in instruction:
            computer.inputs.append(ord(char))
        computer.inputs.append(10)

    run_until_stop(it)
示例#14
0
def part1_and_2(program):
    computer_count = 50
    computers = []
    iters = []

    for i in range(computer_count):
        c = IntcodeComputer(program)
        c.inputs.append(i)
        computers.append(c)
        iters.append(c.run())

    first_y_sent_from_nat = None
    y_sent_from_nat = set()
    while True:
        for i in range(computer_count):
            c = computers[i]
            it = iters[i]

            while True:
                if not c.inputs:
                    c.inputs.append(-1)
                try:
                    a, x, y = next(it), next(it), next(it)
                    if a == 255:
                        if not first_y_sent_from_nat:
                            first_y_sent_from_nat = y
                        if all(not c1.inputs for c1 in computers):
                            if y in y_sent_from_nat:
                                print(f'Part 1: {first_y_sent_from_nat}')
                                print(f'Part 2: {y}')
                                return
                            computers[0].inputs.extend([x, y])
                            y_sent_from_nat.add(y)
                    else:
                        computers[a].inputs.extend([x, y])
                except IndexError:  # No input available
                    iters[i] = c.run(
                    )  # Retry same instruction next iteration for computer (input will be provied)
                    break
示例#15
0
def scan_space(program, limit):
    space = {}
    x = 0
    y = 0
    prev_output_for_row = None

    while y < limit:
        computer = IntcodeComputer(program)
        it = computer.run()

        try:
            computer.inputs.append(x)
            computer.inputs.append(y)
            output = next(it)
            space[(x, y)] = output

            end_of_beam_for_row = output == 0 and prev_output_for_row == 1
            if end_of_beam_for_row:
                beam_start_for_row = min([
                    pos[0] for pos, beam in space.items()
                    if beam and pos[1] == y
                ])
                x = beam_start_for_row - 1
                y += 1
                prev_output_for_row = None
            else:
                prev_output_for_row = output
                x += 1
                if x == limit:
                    x = 0
                    y += 1
                    prev_output_for_row = None
        except StopIteration:
            pass

    return space