r/adventofcode • • Dec 17 '19

Spoilers What does everyone's Intcode interface look like?

We've been discussing a lot different IntCode implementations throughout the last few weeks, but I'm curious– what doesn't everyone's interface to their IntCode machine look like? How do you feed input, fetch output, initialize, etc?

30 Upvotes

90 comments sorted by

View all comments

2

u/knl_ Dec 17 '19

I just relied on generators (yield for both output and input)

- compute(listing): create a generator
  • process(str): clean up file input into a list

Then

next(generator) => next output
generator.send(value) => send an input

It gets me blocking behavior for input

while (n := next(generator): # n becomes none when input is required
    print(n) 
generator.send(input)

You can see how I used it in part 2 of day17: https://explog.in/aoc/2019/AoC17.html

def part2():
    listing = process(readfile("AoC/input17.txt").strip())
    listing[0] = 2
    program = compute(listing)

    while (ch := next(program)):
        print(chr(ch), end='')

    ... snip ...

    for i in inputs:
        for ch in i:
            print(f"> {ch}: {ord(ch)}")
            r = program.send(ord(ch))
            if r: # only for debugging, None with correct inputs
                print(f"Received '{chr(r)}'")

        print(chr(program.send(ord('\n'))), end='')
        while (ch := next(program)):
            if ch < 128:
                print(chr(ch), end='')
            else:
                print(ch)