Ejemplo n.º 1
0
def main():
    raw_program = open("input.txt").readline()
    program = list(map(int, raw_program.split(",")))

    program[0] = 2
    print("Added 2 quarters to the machine")

    debug = Queue()
    setDebugStream(lambda *args: debug.put(args))
    debugThread = Thread(name="Debug Printer",
                         target=debugPrinter,
                         args=[debug],
                         daemon=True)
    debugThread.start()

    field = defaultdict(int)
    score = [0]

    commands = Queue()
    a = IntCode(
        program,
        name="game",
        input=joystickReader(field, score, commands),
        output=commands.put,
    )
    control = a.run_as_thread()

    painter = Thread(name="game painter",
                     target=gamePainter,
                     args=(commands, field, score))
    painter.start()

    control.join()
    painter.join()
Ejemplo n.º 2
0
def spawnThreads(phases, program):
    queues = [Queue() for _ in phases]
    queues.append(queues[0])  # for wraparound
    names = [chr(ord("A") + i) for i in range(len(phases))]

    debug = Queue()
    setDebugStream(lambda *args: debug.put(args))
    Thread(name="Debug Printer", target=debugPrinter, args=[debug]).start()
    threads = []
    for phase, input, output, name in zip(phases, queues, queues[1:], names):
        input.put(phase)

        a = IntCode(program, input=input.get, output=output.put, name=name)
        thread = Thread(name=name, target=a.run)
        threads.append(thread)
        thread.start()
        print("Spawned", name, "with", phase)
    # initial input
    queues[0].put(0)

    for thread in threads:
        print("joining", thread.getName())
        thread.join()

    got = queues[0].get()
    print("Joined all for perm", phases, "and got", got)
    return got
Ejemplo n.º 3
0
def main():
    raw_program = open("input.txt").readline()
    program = list(map(int, raw_program.split(",")))

    debug = Queue()
    setDebugStream(lambda *args: debug.put(args))
    Thread(name="Debug Printer", target=debugPrinter, args=[debug], daemon=True).start()

    queries = Queue()
    commands = Queue()
    a = IntCode(program, name="control", input=queries.get, output=commands.put)
    control = a.run_as_thread()
    field = {}
    Thread(name="hull painter", target=hullPainter, args=(queries, commands, field)).start()
    control.join()
    pprint(field)
    print(len(field))
Ejemplo n.º 4
0
def main():
    raw_program = open("input.txt").readline()
    program = list(map(int, raw_program.split(",")))

    debug = Queue()
    setDebugStream(lambda *args: debug.put(args))
    debugThread = Thread(name="Debug Printer", target=debugPrinter, args=[debug], daemon=True)
    debugThread.start()

    queries = Queue()
    commands = Queue()
    a = IntCode(program, name="game", input=queries.get, output=commands.put)
    control = a.run_as_thread()

    painter = Thread(name="game painter", target=gamePainter, args=(queries, commands))
    painter.start()

    control.join()
    painter.join()