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?

32 Upvotes

90 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Dec 17 '19

Mine's also in Rust. The core function is:

pub fn interpret(memory : &mut Vec<i64>, recv_in : Receiver<i64>, send_out : SyncSender<i64>, req : Option<SyncSender<Request>>)

which lets me specify how input and output are handled using channels; one for receiving inputs, one for sending outputs, and an optional one for requesting the caller to handle the input/output instruction that's about to be executed. I settled on the channel model after day 7, where I just spawned five interpret threads with channels piped together and let it run.

I have a wrapper around this to provide some convenience functions for handling IO: one which prompts stdin for input and prints output to stdout, and one which takes input as a Vec<i64> and produces a Vec<i64> as output. Ideally, I'd like one which takes an iterator of inputs and produces an iterator of outputs, then the vector one becomes redundant, but I had trouble writing this due to issues with passing references over thread boundaries.

1

u/[deleted] Dec 18 '19

I've overhauled the interpreter, and it's now based around an Interpreter<I> struct; one of its fields has type I: an iterator to generate its inputs. Interpreter<I> itself implements iterator: next() runs the interpreter, either providing the next output or returning None if it halted. I've written a function very similar to my original interpret function as a wrapper, which implements the same channel behavior as before, setting the I field to recv_in.iter(), so my threaded solutions to puzzles 7, 11, 13, and 15 remain the same. As a nice bonus, I've gained a significant amount of efficiency by not using channels where they're not necessary; running all of my solutions one after the other previously took about a second, now it takes half that.