Пример #1
0
def p2(lines):
    seed = list(lines[0])
    seed[0] = 2
    computer = Computer(seed)
    runnable = computer.run_output()

    # solved manually by looking at the view generated at part 1
    # after writing the whole sequence, find repeating sequences

    fa = ['R', '12', 'L', '10', 'R', '12']
    fb = ['L', '8', 'R', '10', 'R', '6']
    fc = ['R', '12', 'L', '10', 'R', '10', 'L', '8']
    mainseq = ['A', 'B', 'A', 'C', 'B', 'C', 'B', 'C', 'A', 'C']

    cinput = '\n'.join([
        ','.join(mainseq),
        ','.join(fa),
        ','.join(fb),
        ','.join(fc),
    ]) + '\nn\n'

    def fetch():
        for c in cinput:
            yield ord(c)

    computer.input = fetch()

    for o in runnable:
        if o > 255:
            return o
Пример #2
0
def p2(lines):
    screen = dict()
    score = 0

    def fetch():
        while True:
            ball = next((k for k, v in screen.items() if v == 4), None)
            paddle = next((k for k, v in screen.items() if v == 3), None)
            if not ball or not paddle:
                yield 0
            if ball[0] < paddle[0]:
                yield -1
            elif ball[0] > paddle[0]:
                yield 1
            else:
                yield 0

    seed = list(lines[0])
    seed[0] = 2
    computer = Computer(seed)
    computer.input = fetch()
    runnable = computer.run_output()

    try:
        while True:
            x, y, tile_id = next(runnable), next(runnable), next(runnable)
            if x == -1 and y == 0:
                score = tile_id
            else:
                screen[(x, y)] = tile_id
    except StopIteration:
        pass

    return score
Пример #3
0
def p1(lines):
    # All of the panels are currently black
    panels = defaultdict(int)
    painted = set()
    pos = ((0, 0), 0)

    def fetch():
        while True:
            # provide 0 if the robot is over a black panel
            # or 1 if the robot is over a white panel
            yield panels[pos[0]]

    computer = Computer(lines[0])
    computer.input = fetch()
    runnable = computer.run_output()

    try:
        while True:
            panels[pos[0]] = next(runnable)
            painted.add(pos[0])
            turn = next(runnable)
            d = (pos[1] + (3 if turn == 0 else 1)) % 4
            pos = ((pos[0][0] + directions[d][0],
                    pos[0][1] + directions[d][1]), d)
    except StopIteration:
        pass

    return len(painted)
Пример #4
0
def p1(lines):
    screen = dict()
    computer = Computer(lines[0])
    runnable = computer.run_output()

    try:
        while True:
            x, y, tile_id = next(runnable), next(runnable), next(runnable)
            screen[(x, y)] = tile_id
    except StopIteration:
        pass

    return sum(t == 2 for t in screen.values())
Пример #5
0
def compute2(lines, permutation):
    inputs = {}
    runnable = {}

    def fetch(idx):
        return (inputs[idx][i] for i in itertools.count())

    for idx, phase in enumerate(permutation):
        c = Computer(lines[0])
        c.input = fetch(idx)
        inputs[idx] = [phase]
        runnable[idx] = c.run_output()
    inputs[0].append(0)

    while True:
        for n, r in runnable.items():
            try:
                out = next(r)
            except StopIteration:
                return out
            inputs[(n + 1) % 5].append(out)
Пример #6
0
def p1(lines):
    computer = Computer(lines[0])
    runnable = computer.run_output()

    view = [l for l in ''.join(chr(out) for out in runnable).split('\n') if l]
    # print('\n'.join(view))

    intersections = []
    for y in range(1, len(view) - 1):

        for x in range(1, len(view[0]) - 1):
            if view[y][x] == '.':
                continue

            if any(view[y + j][x + i] == '.'
                   for (i, j) in itertools.product([-1, 0, 1], repeat=2)
                   if i == 0 or j == 0):
                continue
            intersections.append((y, x))

    return sum(x * y for (x, y) in intersections)
Пример #7
0
def p2(lines):
    # The rest of the panels are still black,
    # but it looks like the robot was expecting to start on a white panel, not a black one.
    panels = defaultdict(int)
    panels[(0, 0)] = 1
    pos = ((0, 0), 0)

    def fetch():
        while True:
            # provide 0 if the robot is over a black panel
            # or 1 if the robot is over a white panel
            yield panels[pos[0]]

    computer = Computer(lines[0])
    computer.input = fetch()
    runnable = computer.run_output()

    try:
        while True:
            panels[pos[0]] = next(runnable)
            turn = next(runnable)
            d = (pos[1] + (3 if turn == 0 else 1)) % 4
            pos = ((pos[0][0] + directions[d][0],
                    pos[0][1] + directions[d][1]), d)
    except StopIteration:
        pass

    xs = sorted(x for x, _ in panels.keys())
    ys = sorted(y for _, y in panels.keys())
    minx, maxx, miny, maxy = min(xs), max(xs), min(ys), max(ys)

    return '\n' + '\n'.join([
        ''.join([
            '█' if panels[(x, y)] == 1 else ' ' for x in range(minx, maxx + 1)
        ]) for y in range(maxy, miny - 1, -1)
    ])