2019: day05: allow for multiple inputs

This commit is contained in:
Antoine Martin 2019-12-07 17:37:53 +01:00
parent 5246fce2ef
commit d44dca92a0

View file

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