示例#1
0
def run_13a():
    runner = IntCodeRunner(memory=initial_instructions, input=[], reverse=True)
    runner.run()
    output = runner.output
    tiles, _ = update_game(output)
    print_state(tiles)
    print(len([tile for tile in tiles.values() if tile == 2]))
示例#2
0
def run_17a():
    runner = IntCodeRunner(
        memory=initial_instructions,
        reverse=True
    )
    output = runner.run()
    str = ""
    x, y = 0, 0
    map = dict()
    for value in output:
        if value == 10:
            print(str)
            str = ""
            x += 1
            y = 0
        else:
            char = chr(value)
            str += char
            map[(x, y)] = char
            y += 1
    intersections = []
    for (x, y), value in map.items():
        if value == "#":
            neighbours = [map.get((x+1, y), ""), map.get((x-1, y), ""), map.get((x, y+1), ""), map.get((x, y-1), "")]
            scaffold_neighbours = [neighbour for neighbour in neighbours if neighbour == "#"]
            if len(scaffold_neighbours) > 2:
                intersections.append((x, y))
    print(sum([x*y for (x, y) in intersections]))
    return
示例#3
0
def run_13b():
    initial_instructions[0] = 2
    runner = IntCodeRunner(memory=initial_instructions, reverse=True)
    input = []
    tiles = {}
    score = -1
    while runner.status != 99:
        runner.output = []
        output = runner.run(input)
        tiles, score = update_game(output, tiles)
        print_state(tiles)
        input = [get_ball_paddle_differential(tiles)]
    print(score)
示例#4
0
def run_15a():
    map = dict()
    position = (0, 0)
    runner = IntCodeRunner(memory=initial_instructions, reverse=True)
    i = 1
    is_ended = False
    while not is_ended and i <= 5000:
        x, y = position
        north = (x, y + 1)
        south = (x, y - 1)
        east = (x + 1, y)
        west = (x - 1, y)
        directions = {
            1: (north, map.get(north, 0)),
            2: (south, map.get(south, 0)),
            3: (west, map.get(west, 0)),
            4: (east, map.get(east, 0))
        }
        available_directions = {
            key: (direction, value)
            for key, (direction, value) in directions.items() if value != -1
        }
        best_direction = sorted(
            available_directions,
            key=lambda key: available_directions[key][1])[0]
        best_position, _ = directions[best_direction]
        print(best_position)

        output = runner.run([best_direction]).pop()
        if output == 0:
            map[best_position] = -1
        else:
            map[position] = i
            position = best_position
            if output == 2:
                map[position] = -2
                print(f"Oxygen found : {position}")
        i += 1
        is_ended = len([(i, j) for i in range(-20, 19) for j in range(-18, 21)
                        if (i, j) != (1, -1) and (i, j) not in map]) == 0
    return map
示例#5
0
def run_17b():
    initial_instructions[0] = 2
    print(initial_instructions)
    A = "R,6,L,10,R,8,R,8\n"
    B = "R,12,L,8,L,10\n"
    C = "R,12,L,10,R,6,L,10\n"
    M = "A,B,A,C,B,C,A,B,A,C\n"
    runner = IntCodeRunner(
        memory=initial_instructions,
        reverse=True
    )
    runner.run(convert_instruction(M))
    runner.run(convert_instruction(A))
    runner.run(convert_instruction(B))
    runner.run(convert_instruction(C))
    runner.run(convert_instruction("n\n"))
    print(runner.output.pop())
示例#6
0
def run_11a(input=0):
    robot_position = (0, 0)
    pointing = "N"
    paintings = {}
    runner = IntCodeRunner(memory=initial_instructions,
                           input=[input],
                           reverse=True)
    while runner.status != 99:
        runner.run()
        painting, turn = runner.output
        runner.output = []
        paintings[robot_position] = painting
        pointing = get_absolute_robot_direction(pointing, turn)
        robot_position = get_new_robot_position(robot_position, pointing)
        runner.input = [paintings.get(robot_position, 0)]
    print(len(paintings.keys()))
    return paintings
示例#7
0
def new_runner():
    return IntCodeRunner(
        memory=[instruction for instruction in initial_instructions],
        verbose=False,
        reverse=False)