Example #1
0
def get_tractor_pull(x, y):
    global memory

    drone = IntCode(memory, 0)
    drone.write_input(x)
    drone.write_input(y)
    drone.run()
    return int(drone.read_output())
Example #2
0
def run_test(phases: Tuple, init_strength: int):

    strength = str(init_strength)

    # Loop through phases given
    for phase in phases:

        intcode = IntCode(memory, 0)
        intcode.write_input(phase)
        intcode.write_input(strength)
        intcode.run()
        strength = int(intcode.read_output())

    # Return the output strength
    return int(strength)
Example #3
0
    return -1


if __name__ == "__main__":

    memory = []
    with open(root_path / "input", "r") as f:
        for line in f.readlines():
            memory.extend([int(x) for x in line.split(",")])

    # Create 50 IntCode computers
    network = []
    for i in range(50):
        icpc = IntCode(memory[:], 0)
        icpc.write_input(str(i))
        network.append(icpc)

    # Now we can kick them all off
    finished = False
    current = 0
    while not finished:
        print(f"Running #{current}")
        network[current].run()
        dest = network[current].read_output()
        if dest != "":
            network[current].run()
            x = network[current].read_output()
            network[current].run()
            y = network[current].read_output()
            print(f"  Paused, output is {dest}, {x}, {y}")
Example #4
0
    # Read input
    memory = []
    with open("day21/input.txt") as f:
        for line in f.readlines():
            memory.extend([int(x) for x in line.split(",")])

    # Part 1
    # spring_prog = "OR A J\nAND B J\nAND C J\nNOT J J\nAND D J\nWALK\n"
    # Part 2
    spring_prog = "NOT H J\nOR C J\nAND B J\nAND A J\nNOT J J\nAND D J\nRUN\n"
    output_chars = []

    springbot = IntCode(memory, 0)
    for ch in spring_prog:
        springbot.write_input(str(ord(ch)))

    springbot.run()
    output = int(springbot.read_output())

    while not springbot.is_halted() and output < 256:
        output_chars.append(chr(output))
        springbot.run()
        if not springbot.is_halted():
            output = int(springbot.read_output())

    if output > 255:
        print(f"Damage: {output}")
    else:
        print(f"Hull: {''.join(output_chars)}")
Example #5
0
                        intersections += (x + 1) * (y + 1)

    for line in scaffold:
        print(f"{''.join(line)}")
    print(f"\nIntersections: {intersections}")

    main_routine = "A,B,B,A,C,B,C,C,B,A\n"
    routine_a = "R,10,R,8,L,10,L,10\n"
    routine_b = "R,8,L,6,L,6\n"
    routine_c = "L,10,R,10,L,6\n"
    video = "n\n"

    memory[0] = 2
    intcode2 = IntCode(memory, 0)
    for char in [str(ord(ch)) for ch in main_routine]:
        intcode2.write_input(char)
    for char in [str(ord(ch)) for ch in routine_a]:
        intcode2.write_input(char)
    for char in [str(ord(ch)) for ch in routine_b]:
        intcode2.write_input(char)
    for char in [str(ord(ch)) for ch in routine_c]:
        intcode2.write_input(char)
    for char in [str(ord(ch)) for ch in video]:
        intcode2.write_input(char)

    intcode2.run()
    while not intcode2.is_halted():
        last_output = intcode2.read_output()
        intcode2.run()

    print(f"Dust collected: {last_output}")
Example #6
0
    # We start at (0,0) facing up
    x, y = 0, 0
    direction = 1  # left=0, up=1, right=2, down=3
    hull = defaultdict(int)

    xmove = [-1, 0, 1, 0]
    ymove = [0, -1, 0, 1]

    # Part 2
    hull[(0, 0)] = 1
    xmin, xmax, ymin, ymax = 0, 0, 0, 0

    # Run and get two pieces of output
    while not robot.is_halted():
        # Provide our current color
        robot.write_input(hull[x, y])

        # Run until we get the new color output
        robot.run()
        if robot.is_halted():
            break
        color = int(robot.read_output())

        # Run again until we get the new direction
        robot.run()
        turn = (int(robot.read_output()) * 2) - 1

        # Paint the hull
        hull[(x, y)] = color

        # Turn
Example #7
0
from intcode import IntCode

if __name__ == "__main__":

    # Read input
    memory = []
    with open("day09/input.txt") as f:
        for line in f.readlines():
            memory.extend([int(x) for x in line.split(",")])

    # Create new IntCode instance
    intcode = IntCode(memory, 0)
    # Part 1
    # intcode.write_input("1")
    # Part 2
    intcode.write_input("2")

    while not intcode.is_halted():
        intcode.run()
        print(f"{intcode.read_output()}")