diff --git a/aoc2019/src/day05.rs b/aoc2019/src/day05.rs index 525fc39..5365234 100644 --- a/aoc2019/src/day05.rs +++ b/aoc2019/src/day05.rs @@ -97,6 +97,7 @@ struct Intcode { input: Vec, output: Vec, ip: usize, + next_input: usize, } impl Intcode { @@ -112,6 +113,7 @@ impl Intcode { input: Vec::new(), output: Vec::new(), ip: 0, + next_input: 0, }) } @@ -228,10 +230,13 @@ impl Intcode { self.ip += 4; } Opcode::Input(dst) => { - let input = self - .input - .pop() - .ok_or_else(|| err!("tried to read input but it was empty"))?; + let input = if self.next_input < self.input.len() { + let res = self.input[self.next_input]; + self.next_input += 1; + Ok(res) + } else { + Err(err!("tried to read input but it was empty")) + }?; dst.set(&mut self.memory, input)?; self.ip += 2;