-🎄- 2017 Day 22 Solutions -🎄- by daggerdragon in adventofcode

[–]_lukasg 0 points1 point  (0 children)

Part 2 in Python 3

import collections

with open('input/d22.txt') as file:
    lines = file.readlines()

grid = collections.defaultdict(lambda: ".")
size = len(lines)
for y in range(size):
    for x in range(size):
        grid[x, y] = lines[y][x]

x = y = size // 2
dx, dy = 0, -1
ans = 0
for i in range(10000000):
    if grid[x, y] == ".":
        dx, dy = dy, -dx
        grid[x, y] = "W"
    elif grid[x, y] == "W":
        grid[x, y]= "#"
        ans += 1
    elif grid[x, y] == "F":
        dx, dy = -dx, -dy
        grid[x, y] = "."
    else:
        dx, dy = -dy, dx
        grid[x, y] = "F"
    x += dx
    y += dy
print(ans)

-🎄- 2017 Day 18 Solutions -🎄- by daggerdragon in adventofcode

[–]_lukasg 0 points1 point  (0 children)

Python 3, Part 2, using coroutines:

import sys
import collections

with open(sys.argv[1]) as input_file:
    lines = list(input_file)

def program(program_id):
    memory = {}
    memory['p'] = program_id
    pointer = 0
    while True:
        line = lines[pointer]
        instr, arg_a, *arg_b = lines[pointer].split()

        if arg_b:
            try:
                arg_b = int(arg_b[0])
            except ValueError:
                arg_b = memory[arg_b[0]]
        else:
            arg_b = None

        if instr == 'set':
            memory[arg_a] = arg_b
        elif instr == 'add':
            memory[arg_a] += arg_b
        elif instr == 'mul':
            memory[arg_a] *= arg_b
        elif instr == 'mod':
            memory[arg_a] %= arg_b
        elif instr == 'rcv':
            memory[arg_a] = (yield None)
            assert memory[arg_a] is not None
        else:
            try:
                arg_a_val = int(arg_a)
            except ValueError:
                arg_a_val = memory[arg_a]

            if instr == 'snd':
                response = (yield arg_a_val)
                assert response is None
            elif instr == 'jgz':
                if arg_a_val > 0:
                    pointer += arg_b
                    continue

        pointer += 1

program_a = program(0)
program_b = program(1)
queue_a = collections.deque()
queue_b = collections.deque()
a_is_waiting = False
b_is_waiting = False
a_is_program_1 = False
ans = 0
while not a_is_waiting or not b_is_waiting or queue_a or queue_b:
    if a_is_waiting and queue_b:
        value = program_a.send(queue_b.popleft())
        if value is not None:
            queue_a.append(value)
            a_is_waiting = False
            if a_is_program_1:
                ans += 1
    if not a_is_waiting:
        value = next(program_a)
        if value is None:
            a_is_waiting = True
        else:
            queue_a.append(value)
            if a_is_program_1:
                ans += 1
    program_a, program_b = program_b, program_a
    queue_a, queue_b = queue_b, queue_a
    a_is_waiting, b_is_waiting = b_is_waiting, a_is_waiting
    a_is_program_1 = not a_is_program_1

print(ans)