Ejemplo n.º 1
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
Ejemplo 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)
Ejemplo n.º 3
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
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
def main():
    memory = read()
    src = (0, 0)
    dest, p = bfs(src, memory)
    count = 0
    curr = dest
    while curr != src:
        count += 1
        curr = p[curr]
    print(count)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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
Ejemplo n.º 8
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)
Ejemplo n.º 9
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
Ejemplo n.º 10
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()
Ejemplo n.º 11
0
def main():
    memory = read()
    dest, walls = bfs(memory)
    count = -1
    next_q = queue.Queue()
    next_q.put(dest)
    oxygen = set()
    oxygen.add(dest)
    while not next_q.empty():
        count += 1
        curr_q = next_q
        next_q = queue.Queue()
        while not curr_q.empty():
            u = curr_q.get()
            neighbors = [(u[0], u[1] + 1), (u[0], u[1] - 1), (u[0] - 1, u[1]),
                         (u[0] + 1, u[1])]
            for neighbor in neighbors:
                if neighbor not in walls and neighbor not in oxygen:
                    next_q.put(neighbor)
                    oxygen.add(neighbor)

    print(count)
Ejemplo n.º 12
0
def main():
    memory = read()
    intcode = IntCodeComputer(memory)
    intcode.run()
    print(intcode.memory[0])  # output