Beispiel #1
0
def run():
    init_memory = intcode.parse_input_to_memory("d9input.txt")
    ic = intcode.Intcode(init_memory)
    ic.set_input([1])
    ic.run_program()
    print(ic.output)

    ic = intcode.Intcode(init_memory)
    ic.set_input([2])
    ic.run_program()
    print(ic.output)
Beispiel #2
0
def run():
    init_memory = intcode.parse_input_to_memory("d7input.txt")
    powers = []
    for phases in itertools.permutations([0, 1, 2, 3, 4]):
        powers.append(run_amplifiers(init_memory, phases))
    print(max(powers))

    powers = []
    for phases in itertools.permutations([5, 6, 7, 8, 9]):
        powers.append(feedback_amplifiers(init_memory, phases))
    print(max(powers))
Beispiel #3
0
def run():
    init_memory = intcode.parse_input_to_memory("d5input.txt")

    ic1 = intcode.Intcode(init_memory)
    ic1.set_input([1])
    ic1.run_program()
    print(ic1.output)

    ic2 = intcode.Intcode(init_memory)
    ic2.set_input([5])
    ic2.run_program()
    print(ic2.output)
Beispiel #4
0
def run():
    init_memory = intcode.parse_input_to_memory("d2input.txt")

    ic = intcode.Intcode(init_memory)
    ic.set_noun_verb(12, 2)
    print(ic.run_program())

    for input1 in range(100):
        for input2 in range(100):
            ic = intcode.Intcode(init_memory)
            ic.set_noun_verb(input1, input2)
            output = ic.run_program()
            if output == 19690720:
                print(100 * input1 + input2)
                return
            input2 += 1
        input1 += 1
Beispiel #5
0
def run():
    memory = intcode.parse_input_to_memory("d17input.txt")
    memory[0] = 2
    #memory[191] = 9999
    memory[1047] = 9999
    memory[1164] = 9999
    ic = intcode.Intcode(memory)
    ic.input = [
        ord(c) for c in
        "A\nL,6,R,12,L,6,R,12,L,10,L,4,L,6,L,6,R,12,L,6,R,12,L,10,L,4,L,6,L,6,R,12,L,6,L,10,L,10,L,4,L,6,R,12,L,10,L,4,L,6,L,10,L,10,L,4,L,6,L,6,R,12,L,6,L,10,L,10,L,4,L,6,L,6,R,12,L,10,L,4\nL,10,L,4,L,6\nL,6,L,10\ny\n"
    ]
    img = ""
    while not ic.is_halted():
        ic.run_to_output()
        if ic.output == ord("\n"):
            print(img)
            #input()
            img = ""
        else:
            img += chr(ic.output)
    print(ic.output)
    ic.f.close()
Beispiel #6
0
def run():
    memory = intcode.parse_input_to_memory("d15input.txt")
    ic = intcode.Intcode(memory)
    map = {(0, 0): "."}
    x = 0
    y = 0
    dir = "w"
    found_o2 = False
    while True:
        nx = x
        ny = y
        if dir == "w":
            ic.add_input(1)
            ny -= 1
        elif dir == "s":
            ic.add_input(2)
            ny += 1
        elif dir == "a":
            ic.add_input(3)
            nx -= 1
        elif dir == "d":
            ic.add_input(4)
            nx += 1
        ic.run_to_output()
        result = ic.output
        if result == 0:
            map[(nx, ny)] = "#"
            if dir == "w":
                dir = "d"
            elif dir == "d":
                dir = "s"
            elif dir == "s":
                dir = "a"
            elif dir == "a":
                dir = "w"
        elif result == 1:
            x = nx
            y = ny
            map[(x, y)] = "."
            if dir == "w":
                dir = "a"
            elif dir == "a":
                dir = "s"
            elif dir == "s":
                dir = "d"
            elif dir == "d":
                dir = "w"
        elif result == 2:
            x = nx
            y = ny
            print("Found o2!")
            map[(x, y)] = "o"
            found_o2 = True
            o2x = x
            o2y = y
        if x == 0 and y == 0 and found_o2:
            break
    print_map(map, x, y)

    queue = [(0, 0)]
    visited = {(0, 0): 0}
    while len(queue) > 0:
        x, y = queue[0]
        cost = visited[(x, y)]
        queue = queue[1:]
        if map[(x, y)] == "o":
            print(cost)
            break
        if (x + 1, y) not in visited and map[(x + 1, y)] != "#":
            queue.append((x + 1, y))
            visited[(x + 1, y)] = cost + 1
        if (x - 1, y) not in visited and map[(x - 1, y)] != "#":
            queue.append((x - 1, y))
            visited[(x - 1, y)] = cost + 1
        if (x, y - 1) not in visited and map[(x, y - 1)] != "#":
            queue.append((x, y - 1))
            visited[(x, y - 1)] = cost + 1
        if (x, y + 1) not in visited and map[(x, y + 1)] != "#":
            queue.append((x, y + 1))
            visited[(x, y + 1)] = cost + 1

    queue = [(o2x, o2y)]
    oxygenated = {(o2x, o2y): 0}
    max_time = 0
    while len(queue) > 0:
        x, y = queue[0]
        cost = oxygenated[(x, y)]
        max_time = max(max_time, cost)
        queue = queue[1:]
        if (x + 1, y) not in oxygenated and map[(x + 1, y)] != "#":
            queue.append((x + 1, y))
            oxygenated[(x + 1, y)] = cost + 1
        if (x - 1, y) not in oxygenated and map[(x - 1, y)] != "#":
            queue.append((x - 1, y))
            oxygenated[(x - 1, y)] = cost + 1
        if (x, y - 1) not in oxygenated and map[(x, y - 1)] != "#":
            queue.append((x, y - 1))
            oxygenated[(x, y - 1)] = cost + 1
        if (x, y + 1) not in oxygenated and map[(x, y + 1)] != "#":
            queue.append((x, y + 1))
            oxygenated[(x, y + 1)] = cost + 1
    print(max_time)
Beispiel #7
0
def run():
    memory = intcode.parse_input_to_memory("d13input.txt")
    memory[0] = 2
    ic = intcode.Intcode(memory)
    screen = {}
    blocks = 0
    max_x = 0
    max_y = 0
    score = 0
    paddle_x = 0
    ball_x = 0
    while not ic.is_halted():
        ic.run_to_output()
        if ic.needs_input:
            for y in range(max_y+1):
                line = ""
                for x in range(max_x+1):
                    if (x, y) in screen:
                        tile = screen[(x, y)]
                        if tile == 0:
                            line += " "
                        elif tile == 1:
                            line += "|"
                        elif tile == 2:
                            line += "#"
                        elif tile == 3:
                            line += "-"
                        elif tile == 4:
                            line += "o"
                    else:
                        line += " "
                #print(line)
            #print(ball_x, paddle_x)
            #screen = {}
            if ball_x < paddle_x:
                #print("left")
                ic.add_input(-1)
            elif ball_x > paddle_x:
                #print("right")
                ic.add_input(1)
            else:
                #print("neutral")
                ic.add_input(0)
            #input()
            continue
        x = ic.output
        if x > max_x:
            max_x = x
        ic.run_to_output()
        y = ic.output
        if y > max_y:
            max_y = y
        ic.run_to_output()
        block = ic.output
        #print(x, y, block)
        if x != -1:
            screen[(x, y)] = block
        if x == -1:
            score = block
            print(score)
        if block == 3:
            paddle_x = x
        elif block == 4:
            ball_x = x
    print(score)
Beispiel #8
0
def run():
    init_memory = intcode.parse_input_to_memory("d11input.txt")

    ic = intcode.Intcode(init_memory)
    paint = defaultdict(int)
    dir = 0
    ic.set_input([])
    pos = (0, 0)
    while not ic.is_halted():
        ic.input.append(paint[pos])
        ic.run_to_output()
        if ic.is_halted():
            break
        paint_col = ic.output
        paint[pos] = paint_col
        ic.run_to_output()
        if ic.is_halted():
            break
        turn = ic.output
        dir = add_dir(dir, turn)
        if dir == 0:
            pos = (pos[0], pos[1] - 1)
        elif dir == 1:
            pos = (pos[0] + 1, pos[1])
        elif dir == 2:
            pos = (pos[0], pos[1] + 1)
        elif dir == 3:
            pos = (pos[0] - 1, pos[1])
        else:
            assert (False)
    print(len(paint))

    ic = intcode.Intcode(init_memory)
    paint = defaultdict(int)
    paint[(0, 0)] = 1
    dir = 0
    ic.set_input([])
    pos = (0, 0)
    while not ic.is_halted():
        ic.input.append(paint[pos])
        ic.run_to_output()
        if ic.is_halted():
            break
        paint_col = ic.output
        paint[pos] = paint_col
        ic.run_to_output()
        if ic.is_halted():
            break
        turn = ic.output
        dir = add_dir(dir, turn)
        if dir == 0:
            pos = (pos[0], pos[1] - 1)
        elif dir == 1:
            pos = (pos[0] + 1, pos[1])
        elif dir == 2:
            pos = (pos[0], pos[1] + 1)
        elif dir == 3:
            pos = (pos[0] - 1, pos[1])
        else:
            assert (False)

    minx = 999999
    miny = 999999
    maxx = 0
    maxy = 0
    for k in paint:
        if k[0] < minx:
            minx = k[0]
        if k[0] > maxx:
            maxx = k[0]
        if k[1] < miny:
            miny = k[1]
        if k[1] > maxy:
            maxy = k[1]
    for y in range(miny, maxy + 1):
        line = ""
        for x in range(minx, maxx + 1):
            line += "." if paint[(x, y)] == 0 else "#"
        print(line)