Esempio n. 1
0
def solve(d):
    size = 1000
    hit = set()
    extremes = [[0, 0] for _ in range(size)]
    # seen_wide = False
    # wide_width = 200

    for y in range(size):
        for x in range(y, 2 * y):
            computer = Computer(d, x)

            while True:
                retcode, retval = computer.step()

                if retcode == 0 and retval == 3:
                    computer.set_input(y)
                    break

            retcode, retval = computer.get_output()

            if retval == 1:
                # if (y, x - wide_width) in hit:
                #     seen_wide = True
                hit.add((y, x))
                # if seen_wide and (y - 99) in hit:
                if (y - 99, x + 99) in hit:
                    return 10000 * x + y - 99
                if extremes[y][0] == 0:
                    extremes[y][0] = x
                    if (y - 99, x + 99) in hit:
                        print(y, x)
                extremes[y][1] = x

    return len(hit)
Esempio n. 2
0
def solve(data, inp):
    painted = defaultdict(int)
    y = 0
    x = 0
    output_count = 0
    directions = ((-1, 0), (0, 1), (1, 0), (0, -1))
    facing = 0
    computer = Computer(data, inp)
    retval, retcode = 0, 0

    while retcode != -1:
        retcode, retval = computer.step()

        if retcode == 0:
            computer.set_input(painted[(y, x)])
            continue

        if output_count % 2 == 0:
            painted[(y, x)] = retval
        else:
            if retval == 0:
                facing = (facing - 1) % 4
            else:
                facing = (facing + 1) % 4

            y += directions[facing][0]
            x += directions[facing][1]

        computer.set_input(painted[(y, x)])

        output_count += 1

    return len(painted)
Esempio n. 3
0
def solve(d):
    inp = """OR E J
OR H J
AND D J
OR A T
AND B T
AND C T
NOT T T
AND T J
RUN
"""
    p = 0
    steps = 0

    computer = Computer(d, ord(inp[p]))

    while True:
        steps += 1
        retcode, retval = computer.step()

        if retcode == -1:
            break

        if retcode == 0 and retval == 3:
            p += 1
            if p < len(inp):
                computer.set_input(ord(inp[p]))

        if retcode == 1 and retval > 255:
            return retval

    return steps
Esempio n. 4
0
def solve(d):
    items, paths = get_data()
    program = get_program()
    computer = Computer(d, ord(program[0]))
    p = 1

    while True:
        retcode, retval = computer.step()

        if retcode == 1:
            print(chr(retval), end='')
            if (chr(retval)) == '?':
                print('')
                inp = input() + '\n'
                if not program:
                    program = inp
                    computer.set_input(ord(program[0]))
                    p = 1
        elif retcode == 0 and retval == 3:
            if p < len(program):
                computer.set_input(ord(program[p]))
                p += 1
            else:
                program = ''
        elif retcode == -1:
            return
Esempio n. 5
0
def run_network():
    with open('day23_input.txt') as infile:
        program = list(map(int, infile.read().split(',')))
    computers = {}
    programs = {}
    for address in range(50):
        computer = Computer(program)
        computer.set_input([address])
        computers[address] = computer
        programs[address] = computer.run_program()
    messages = {address: [] for address in range(50)}
    nat_value = None
    nat_y_sent = set()
    while True:
        for address, program in programs.items():
            output = next(program)
            if output is not None:
                messages[address].append(output)
                if len(messages[address]) == 3:
                    destination, x, y = messages.pop(address)
                    if destination == 255:
                        nat_value = (x, y)
                    else:
                        computers[destination].set_input((x, y))
                    messages[address] = []
            idle = all(
                len(computer.inputs) == 0 for computer in computers.values())
            if idle and nat_value:
                computers[0].set_input(nat_value)
                y = nat_value[1]
                if y in nat_y_sent:
                    return y
                nat_y_sent.add(y)
                nat_value = None
Esempio n. 6
0
def solve(d):
    exploreputer = Computer(d, 1)

    grid, height, width = get_grid(exploreputer)
    moves = get_moves(grid, height, width)

    movement, funca, funcb, funcc = programify(infuse_numbers(moves))

    inputs = []

    for c in movement:
        inputs.append(ord(c))

    inputs.append(10)

    for c in funca:
        inputs.append(ord(c))

    inputs.append(10)

    for c in funcb:
        inputs.append(ord(c))

    inputs.append(10)

    for c in funcc:
        inputs.append(ord(c))

    inputs.append(10)

    inputs.append(ord('n'))
    inputs.append(10)

    moveputer = Computer(d, inputs[0], 2)
    inppos = 1
    retcode, retval = 0, 0
    dust = 0

    while retcode != -1:
        retcode, retval = moveputer.step()

        if retcode == 0 and retval == 3:
            if inppos < len(inputs):
                moveputer.set_input(inputs[inppos])
            inppos += 1

        if retcode == 1:
            dust = retval

    return dust
Esempio n. 7
0
def main():
    with open('data.txt', 'r') as f:
        program = f.read()
    program = [int(i) for i in program.strip().split(',')]
    computer = Computer(program)
    computer.start()
    while True:
        while computer.has_output():
            print(chr(computer.get_output()), end='')
        command = input()
        if command == 'exit':
            break
        command = list(command)
        while command:
            computer.set_input(ord(command.pop(0)))
        computer.set_input(ord('\n'))
    computer.terminate()
Esempio n. 8
0
def solve(d):
    count = 0

    for x in range(50):
        for y in range(50):
            computer = Computer(d, x)

            while True:
                retcode, retval = computer.step()

                if retcode == 0 and retval == 3:
                    computer.set_input(y)
                    break

            retcode, retval = computer.get_output()

            if retval == 1:
                count += 1

    return count
Esempio n. 9
0
def solve(d):
    computer = Computer(d, 0, 2)
    count = 0
    retcode = 0
    loops = 0
    lowest = 100
    highest = -100
    out = [-1, -1, -1]
    score = 0
    ballx = 0
    padx = 0

    cells = {}
    
    while retcode != -1:
        retcode, retval = computer.step()
        if retcode != 1:
            continue
   
        if out[:2] == [-1, 0] and loops % 3 == 2:
            score = retval
        else:
            out[loops % 3] = retval

        if loops % 3 == 2:                
            cells[(out[1], out[0])] = out[2]
            if retval == 3:
                padx = out[0]
            if retval == 4:
                ballx = out[0]
        else:
            lowest = min(lowest, retval)
            highest = max(highest, retval)

        computer.set_input(0 if ballx == padx else -1 if ballx < padx else 1)
        
        loops += 1

    return score
Esempio n. 10
0
 def _get_value_from_robot(self, point):
     computer = Computer(self.program)
     computer.start()
     computer.set_input(point.x)
     computer.set_input(point.y)
     return computer.get_output()