コード例 #1
0
ファイル: 11.py プロジェクト: Scarygami/aoc2019
def paint_hull(inputfile, hull={}):
    """Launches the emergency hull painting robot with the specified Intcode source file

    Parameters
    ----------

    inputfile: str
        Path/Filename of Intcode source code

    hull : dict<(int,int): int>
        Initial state of the hull
    """

    robot_pos = (0, 0)
    robot_dir = (0, -1)
    machine = IntcodeVM(inputfile, silent=True)
    machine.run()

    while machine.waiting:
        color, turn = machine.resume([hull.get(robot_pos, BLACK)])
        hull[robot_pos] = color
        robot_dir = TURNS[robot_dir][turn]
        robot_pos = (robot_pos[0] + robot_dir[0], robot_pos[1] + robot_dir[1])

    return hull
コード例 #2
0
def play_game(inputfile, screen):
    source = IntcodeVM.read_intcode(inputfile)

    # Insert coin...
    source[0] = 2

    # Boot up arcade machine
    machine = IntcodeVM(source, silent=True)
    grid = {}
    score = 0
    total_bricks = 0
    max_y = 0
    move = None

    # Game loop
    while machine.waiting:
        if move is None:
            outputs = machine.run()
        else:
            outputs = machine.resume([move])

        for o in range(0, len(outputs), 3):
            x, y, tile = outputs[o:o + 3]
            if x == -1 and y == 0:
                score = tile
            else:
                screen.print_at(SYMBOLS[tile], x * 3, y, COLORS[tile])
                grid[(x, y)] = tile

        if total_bricks == 0:
            total_bricks = sum(1 for pos in grid if grid[pos] == BLOCK)

        if max_y == 0:
            max_y = max(grid.keys())[1]

        screen.print_at(
            "Blocks left: %s / %s     " %
            (sum(1 for pos in grid if grid[pos] == 2), total_bricks), 5,
            max_y + 1)
        screen.print_at("Score: %s" % score, 5, max_y + 2)
        screen.move(0, 0)
        screen.refresh()
        sleep(0.01)

        # Breakout AI!!
        paddle = [key for key in grid if grid[key] == PADDLE][0]
        ball = [key for key in grid if grid[key] == BALL][0]
        if paddle[0] < ball[0]:
            move = 1
        elif paddle[0] > ball[0]:
            move = -1
        else:
            move = 0

    screen.print_at("All done, press ENTER to exit!", 5, max_y // 2)
    screen.refresh()
    input()
コード例 #3
0
def feedback_loop(inputfile):
    max_thruster = 0
    code = IntcodeVM.read_intcode(inputfile)
    for phases in permutations([5, 6, 7, 8, 9]):
        done = False

        #Initialise machines
        machines = []
        for phase in phases:
            machine = IntcodeVM(code)
            machine.run([phase])
            machines.append(machine)

        last_output = 0
        while not done:
            for machine in machines:
                last_output = machine.resume([last_output])[-1]
                if not machine.waiting:
                    done = True

        if last_output > max_thruster:
            max_thruster = last_output

    return max_thruster
コード例 #4
0
ファイル: 25.py プロジェクト: Scarygami/aoc2019
items = [
    "food ration", "weather machine", "antenna", "space law space brochure",
    "jam", "semiconductor", "planetoid", "monolith"
]

machine.run("\n".join(walkthrough) + "\n")

# Brute forcing all item combinations until the right weight is reached.
for l in range(len(items)):
    for selected in combinations(items, l):
        steps = []
        for item in selected:
            steps.append("take %s" % item)
        steps.append("east")

        outputs = machine.resume("\n".join(steps) + "\n")
        outputs = "".join([chr(c) for c in outputs])
        if outputs.find("lighter") >= 0:
            print("Too heavy:", selected)
        elif outputs.find("heavier") >= 0:
            print("Too light:", selected)
        else:
            print(outputs)
            exit()

        steps = []
        for item in selected:
            steps.append("drop %s" % item)
        machine.resume("\n".join(steps) + "\n")