Ejemplo n.º 1
0
def part2(file):
    debug = True
    buffer = deque()

    def chr_output(value):
        buffer.append(chr(value))

    def chr_input():
        return ord(buffer.popleft())

    program = parse_program(file)
    computer = Computer(program, int_input, chr_output)
    computer.run()
    view = list(map(str.strip, ''.join(buffer).strip().split('\n')))
    height = len(view)
    width = len(view[0])

    robot = find_robot(view)
    steps = build_steps(robot, view)
    actions, procedures = compact(steps)[-1]
    actions = ','.join('ABC'[i] for i in actions) + '\n'

    buffer.clear()
    buffer.extend(actions)
    for procedure in procedures:
        buffer.extend(procedure + '\n')
    buffer.extend(('y' if debug else 'n') + '\n')

    map_buffer = [[]]
    map_buffer_offset = height + 7

    def map_output(value):
        nonlocal map_buffer
        nonlocal map_buffer_offset

        if value > 255:
            print(value)
            return

        c = chr(value)

        if map_buffer_offset > 0:
            sys.stdout.write(c)
            if c == '\n':
                map_buffer_offset -= 1
            return

        map_buffer[-1].append(c)

        if c == '\n':
            map_buffer.append([])
            return

        if len(map_buffer) > height:
            cur_buffer, map_buffer = map_buffer[:-1], map_buffer[-1:]
            sys.stdout.write(''.join(''.join(s) for s in cur_buffer))

    computer = Computer(program, chr_input, map_output)
    computer.set(0, 2)
    computer.run()
Ejemplo n.º 2
0
def part2(file):
    N = 50
    nat = 255
    network = {}
    network[nat] = deque()
    program = parse_program(file)
    computers = [NetworkComputer(n, program, network) for n in range(N)]
    idle_loop_count = 1000
    idle_counter = 0
    instr_count = 0
    answer = []
    while computers:
        if all(not network[n] if n in network else True for n in range(N)):
            idle_counter += 1
        else:
            idle_counter = 0

        if idle_counter >= idle_loop_count and network[nat]:
            idle_counter = 0
            x, y = network[nat].pop()
            network[nat].clear()
            network[0].append((x, y))
            if answer and answer[-1] == y:
                break
            answer.append(y)

        instr_count += 1
        for comp in computers:
            if comp.stopped():
                computers = filter(lambda c: not c.stopped(), computers)
                continue
            comp.step()

    answer = answer[-1]
    print(f"Answer: {answer}")
Ejemplo n.º 3
0
def part1(file):
    chr_input = ChrInput()
    chr_input.extend("NOT C J\n")
    chr_input.extend("NOT D T\n")
    chr_input.extend("NOT T T\n")
    chr_input.extend("AND T J\n")
    chr_input.extend("NOT A T\n")
    chr_input.extend("OR T J\n")
    chr_input.extend("WALK\n")

    program = parse_program(file)
    computer = Computer(program, chr_input, chr_output)
    computer.run()
Ejemplo n.º 4
0
def part1(file):
    N = 50
    program = parse_program(file)
    network = {}
    computers = [NetworkComputer(n, program, network) for n in range(N)]
    target = 255
    while target not in network:
        for comp in computers:
            if comp.stopped():
                continue
            comp.step()
    x, y = network[target].popleft()
    print(f"Answer: {y}")
Ejemplo n.º 5
0
def part1(file):
    buffer = []

    def chr_output(value):
        buffer.append(chr(value))

    program = parse_program(file)
    computer = Computer(program, int_input, chr_output)
    computer.run()

    view = ''.join(buffer).strip().split('\n')
    intersections = find_intersections(view)
    alignment = sum(x * y for x, y in intersections)
    print(f"Answer: {alignment}")
Ejemplo n.º 6
0
def day8_2(data):
    program = computer.parse_program(data)
    idx = -1
    while True:
        idx += 1
        old_inst = program[idx][0]
        if old_inst == "jmp":
            program[idx][0] = "nop"
        elif old_inst == "nop":
            program[idx][0] = "jmp"
        else:
            continue

        acc, infinite = day8_run_program(program)
        if not infinite:
            return acc

        program[idx][0] = old_inst
    return None
Ejemplo n.º 7
0
def part2(file):
    chr_input = ChrInput()
    chr_input.extend("NOT A J\n")
    chr_input.extend("NOT B T\n")
    chr_input.extend("NOT T T\n")
    chr_input.extend("AND C T\n")
    chr_input.extend("NOT T T\n")
    chr_input.extend("AND D T\n")
    chr_input.extend("AND H T\n")
    chr_input.extend("OR T J\n")
    chr_input.extend("NOT B T\n")
    chr_input.extend("AND D T\n")
    chr_input.extend("AND H T\n")
    chr_input.extend("OR T J\n")
    chr_input.extend("RUN\n")

    program = parse_program(file)
    computer = Computer(program, chr_input, chr_output)
    computer.run()
Ejemplo n.º 8
0
def part1(file):
    counter = 0
    coord_generator = gen_coord(50, 50)

    def coord_input():
        value = next(coord_generator)
        return value

    def count_output(value):
        nonlocal counter
        counter += value

    program = parse_program(file)
    while True:
        computer = Computer(program, coord_input, count_output)
        try:
            computer.run()
        except StopIteration:
            break
    print(f"Answer: {counter}")
Ejemplo n.º 9
0
 def test_day2_example1(self):
     ic = IntComputer(parse_program('1,9,10,3,2,3,11,0,99,30,40,50'))
     ic()
     self.assertEqual(ic.memory[0], 3500)
Ejemplo n.º 10
0
def day8_1(data):
    program = computer.parse_program(data)
    return day8_run_program(program)[0]
Ejemplo n.º 11
0
def part1(file):
    program = parse_program(file)
    computer = Computer(program, cmd_input, char_output)
    computer.run()