Esempio n. 1
0
def bfs(memory):
    walls = set()
    visited = set()
    q = queue.Queue()
    intcode = IntCodeComputer(memory)
    q.put(((0, 0), intcode))

    while not q.empty():
        u, intcode = q.get()
        neighbors = [(u[0], u[1] + 1), (u[0], u[1] - 1), (u[0] - 1, u[1]),
                     (u[0] + 1, u[1])]
        for idx, neighbor in enumerate(neighbors):
            if neighbor not in visited:
                visited.add(neighbor)
                copy = intcode.copy()
                copy.read(idx + 1)
                copy.run()
                while not copy.output.empty():
                    status = copy.output.get()
                    if status == 0:
                        walls.add(neighbor)
                    elif status == 2:
                        dest = neighbor
                        q.put((neighbor, copy))
                    elif status == 1:
                        q.put((neighbor, copy))
    return (dest, walls)
Esempio n. 2
0
def main():
    memory = read()
    intcode = IntCodeComputer(memory)
    intcode.run()
    curr = [0, 0]
    scaffolds = set()
    while not intcode.output.empty():
        value = intcode.output.get()
        # print(chr(value), end="")
        if value == 35:
            scaffolds.add(tuple(curr))
        if value == 10:
            curr[1] += 1
            curr[0] = 0
        else:
            curr[0] += 1

    alignment_params = 0
    for scaffold in scaffolds:
        if (scaffold[0] - 1, scaffold[1]) not in scaffolds:
            continue
        if (scaffold[0] + 1, scaffold[1]) not in scaffolds:
            continue
        if (scaffold[0], scaffold[1] - 1) not in scaffolds:
            continue
        if (scaffold[0], scaffold[1] + 1) not in scaffolds:
            continue
        alignment_params += (scaffold[0] * scaffold[1])
    print(alignment_params)
Esempio n. 3
0
def main():
    memory = read()
    intcode = IntCodeComputer(memory)
    intcode.run()
    i = 0
    counter = 0
    while not intcode.output.empty():
        value = intcode.output.get()
        if i % 3 == 2 and value == 2:
            counter += 1
        i += 1
    print(counter)
Esempio n. 4
0
def main():
    memory = read()
    perms = list(itertools.permutations(range(5, 10)))
    max_signal = -10e9

    for perm in perms:
        amplifers = []
        isFirst = True
        halted = False

        for phase in perm:
            amplifier = IntCodeComputer(memory)
            amplifier.read(phase)
            amplifers.append(amplifier)

        output = 0
        while True:
            for amplifier in amplifers:
                amplifier.read(output)
                amplifier.run()
                if amplifier.output.empty():
                    halted = True
                    break
                output = amplifier.output.get()
            if halted:
                break
            final_output = output
        max_signal = max(final_output, max_signal)
    print(max_signal)  # output
Esempio n. 5
0
def main():
    memory = read()
    memory[0] = 2
    intcode = IntCodeComputer(memory)
    routine = "A,A,B,C,B,C,B,C,B,A\n"
    function_A = "L,10,L,8,R,8,L,8,R,6\n"
    function_B = "R,6,R,8,R,8\n"
    function_C = "R,6,R,6,L,8,L,10\n"
    continuous = "n\n"
    read_input(intcode,
               routine + function_A + function_B + function_C + continuous)
Esempio n. 6
0
def main():
    memory = read()
    perms = list(itertools.permutations(range(5)))
    max_signal = -10e9
    for perm in perms:
        output = 0
        for idx, phase in enumerate(perm):
            amplifier = IntCodeComputer(memory)
            amplifier.read(phase)
            amplifier.read(output)
            amplifier.run()
            output = amplifier.output.get()
        max_signal = max(output, max_signal)
    print(max_signal)  # output
Esempio n. 7
0
def main():
    memory = read()
    intcode = IntCodeComputer(memory)
    white_tiles = set()
    painted = set()
    curr = [0, 0]
    turn_to_paint = True
    face = 0
    directions = ['U', 'R', 'D', 'L']
    while not intcode.completed:
        intcode.run()
        while not intcode.output.empty():
            value = intcode.output.get()
            if turn_to_paint:
                if value == 1:
                    white_tiles.add(tuple(curr))
                elif value == 0 and tuple(curr) in white_tiles:
                    white_tiles.remove(tuple(curr))
                painted.add(tuple(curr))
            else:
                if value == 1:
                    face = (face + 5) % 4
                elif value == 0:
                    face = (face + 3) % 4
                update(curr, directions[face])
            turn_to_paint = not turn_to_paint
        intcode.read(int(tuple(curr) in white_tiles))

    print(len(painted))  # output
Esempio n. 8
0
def main():
    memory = read()
    memory[0] = 2
    intcode = IntCodeComputer(memory)
    ball_x = -1
    paddle_x = -1
    score = -1
    while not intcode.completed:
        intcode.run()
        while not intcode.output.empty():
            x = intcode.output.get()
            y = intcode.output.get()
            tile_id = intcode.output.get()
            if x == -1 and y == 0:
                score = tile_id
            if tile_id == 3:
                paddle_x = x
            elif tile_id == 4:
                ball_x = x
        if paddle_x < ball_x:
            ipt = 1
        elif paddle_x > ball_x:
            ipt = -1
        else:
            ipt = 0
        intcode.read(ipt)
    print(score)
Esempio n. 9
0
def main():
    memory = read()
    for i in range(100):
        for j in range(100):
            intcode = IntCodeComputer(memory)
            intcode.memory[1] = i
            intcode.memory[2] = j
            intcode.run()
            if intcode.memory[0] == 19690720:  # additional input
                print(100 * i + j)  # output
                return
Esempio n. 10
0
def bfs(src, memory):
    p = dict()
    q = queue.Queue()
    q.put((src, IntCodeComputer(memory)))

    while not q.empty():
        u, intcode = q.get()
        neighbors = [(u[0], u[1] + 1), (u[0], u[1] - 1), (u[0] - 1, u[1]),
                     (u[0] + 1, u[1])]
        for idx, neighbor in enumerate(neighbors):
            if neighbor not in p:
                p[neighbor] = u
                copy = intcode.copy()
                copy.read(idx + 1)
                copy.run()
                while not copy.output.empty():
                    status = copy.output.get()
                    if status == 2:
                        return (neighbor, p)
                    elif status == 1:
                        q.put((neighbor, copy))
Esempio n. 11
0
def main():
    memory = read()
    intcode = IntCodeComputer(memory)
    white_tiles = set()
    curr = [0, 0]
    white_tiles.add(tuple(curr))
    turn_to_paint = True
    face = 0
    directions = ['U', 'R', 'D', 'L']
    min_x = 10e9
    min_y = 10e9
    max_x = -10e9
    max_y = -10e9
    while not intcode.completed:
        intcode.run()
        while not intcode.output.empty():
            value = intcode.output.get()
            if turn_to_paint:
                if value == 1:
                    white_tiles.add(tuple(curr))
                elif value == 0 and tuple(curr) in white_tiles:
                    white_tiles.remove(tuple(curr))
                min_x = min(min_x, curr[0])
                min_y = min(min_y, curr[1])
                max_x = max(max_x, curr[0])
                max_y = max(max_y, curr[1])
            else:
                if value == 1:
                    face = (face + 5) % 4
                elif value == 0:
                    face = (face + 3) % 4
                update(curr, directions[face])
            turn_to_paint = not turn_to_paint
        intcode.read(int(tuple(curr) in white_tiles))

    for j in range(max_y, min_y - 1, -1):
        for i in range(min_x, max_x + 1):
            draw((i, j) in white_tiles)
        print()
Esempio n. 12
0
def main():
    memory = read()
    intcode = IntCodeComputer(memory)
    intcode.run()
    print(intcode.memory[0])  # output