2019: intcode: prepare for day09

- Implement opcode 9
 - Support infinite memory
This commit is contained in:
Antoine Martin 2019-12-09 16:25:17 +01:00
parent 21b9b2d18f
commit 088f203f61
2 changed files with 88 additions and 28 deletions

View file

@ -12,6 +12,7 @@ pub fn parse_memory(s: &str) -> Result<Vec<i64>> {
.collect() .collect()
} }
#[derive(Debug)]
enum Opcode { enum Opcode {
Add(Parameter, Parameter, Parameter), Add(Parameter, Parameter, Parameter),
Multiply(Parameter, Parameter, Parameter), Multiply(Parameter, Parameter, Parameter),
@ -21,6 +22,7 @@ enum Opcode {
JumpFalse(Parameter, Parameter), JumpFalse(Parameter, Parameter),
LessThan(Parameter, Parameter, Parameter), LessThan(Parameter, Parameter, Parameter),
Equals(Parameter, Parameter, Parameter), Equals(Parameter, Parameter, Parameter),
AdjustRelBase(Parameter),
Halt, Halt,
} }
@ -32,6 +34,7 @@ pub struct Intcode {
ip: usize, ip: usize,
next_input: usize, next_input: usize,
wait_input: bool, wait_input: bool,
relative_base: usize,
} }
impl Intcode { impl Intcode {
@ -49,6 +52,7 @@ impl Intcode {
ip: 0, ip: 0,
next_input: 0, next_input: 0,
wait_input: false, wait_input: false,
relative_base: 0,
} }
} }
@ -134,6 +138,11 @@ impl Intcode {
Ok(Opcode::Equals(op1, op2, dst)) Ok(Opcode::Equals(op1, op2, dst))
} }
} }
9 => {
let offset = Parameter::new(mode1, self.memory.get(self.ip + 1).copied())?;
Ok(Opcode::AdjustRelBase(offset))
}
99 => Ok(Opcode::Halt), 99 => Ok(Opcode::Halt),
_ => Err(err!("unknown opcode: {}", opcode)), _ => Err(err!("unknown opcode: {}", opcode)),
} }
@ -149,18 +158,18 @@ impl Intcode {
match opcode { match opcode {
Opcode::Add(op1, op2, dst) => { Opcode::Add(op1, op2, dst) => {
let val1 = op1.get(&self.memory)?; let val1 = op1.get(&mut self.memory, self.relative_base)?;
let val2 = op2.get(&self.memory)?; let val2 = op2.get(&mut self.memory, self.relative_base)?;
dst.set(&mut self.memory, val1 + val2)?; dst.set(val1 + val2, &mut self.memory, self.relative_base)?;
self.ip += 4; self.ip += 4;
} }
Opcode::Multiply(op1, op2, dst) => { Opcode::Multiply(op1, op2, dst) => {
let val1 = op1.get(&self.memory)?; let val1 = op1.get(&mut self.memory, self.relative_base)?;
let val2 = op2.get(&self.memory)?; let val2 = op2.get(&mut self.memory, self.relative_base)?;
dst.set(&mut self.memory, val1 * val2)?; dst.set(val1 * val2, &mut self.memory, self.relative_base)?;
self.ip += 4; self.ip += 4;
} }
@ -174,19 +183,19 @@ impl Intcode {
} else { } else {
break Err(err!("tried to read input but it was empty")); break Err(err!("tried to read input but it was empty"));
}; };
dst.set(&mut self.memory, input)?; dst.set(input, &mut self.memory, self.relative_base)?;
self.ip += 2; self.ip += 2;
} }
Opcode::Output(op) => { Opcode::Output(op) => {
let val = op.get(&self.memory)?; let val = op.get(&mut self.memory, self.relative_base)?;
self.output.push(val); self.output.push(val);
self.ip += 2; self.ip += 2;
} }
Opcode::JumpTrue(test, dst) => { Opcode::JumpTrue(test, dst) => {
let val = test.get(&self.memory)?; let val = test.get(&mut self.memory, self.relative_base)?;
let dst = dst.get(&self.memory)?; let dst = dst.get(&mut self.memory, self.relative_base)?;
if dst < 0 { if dst < 0 {
return Err(err!("dst must be a valid address: {}", dst)); return Err(err!("dst must be a valid address: {}", dst));
} }
@ -198,8 +207,8 @@ impl Intcode {
} }
} }
Opcode::JumpFalse(test, dst) => { Opcode::JumpFalse(test, dst) => {
let val = test.get(&self.memory)?; let val = test.get(&mut self.memory, self.relative_base)?;
let dst = dst.get(&self.memory)?; let dst = dst.get(&mut self.memory, self.relative_base)?;
if dst < 0 { if dst < 0 {
return Err(err!("dst must be a valid address: {}", dst)); return Err(err!("dst must be a valid address: {}", dst));
} }
@ -211,23 +220,29 @@ impl Intcode {
} }
} }
Opcode::LessThan(op1, op2, dst) => { Opcode::LessThan(op1, op2, dst) => {
let val1 = op1.get(&self.memory)?; let val1 = op1.get(&mut self.memory, self.relative_base)?;
let val2 = op2.get(&self.memory)?; let val2 = op2.get(&mut self.memory, self.relative_base)?;
let res = if val1 < val2 { 1 } else { 0 }; let res = if val1 < val2 { 1 } else { 0 };
dst.set(&mut self.memory, res)?; dst.set(res, &mut self.memory, self.relative_base)?;
self.ip += 4; self.ip += 4;
} }
Opcode::Equals(op1, op2, dst) => { Opcode::Equals(op1, op2, dst) => {
let val1 = op1.get(&self.memory)?; let val1 = op1.get(&mut self.memory, self.relative_base)?;
let val2 = op2.get(&self.memory)?; let val2 = op2.get(&mut self.memory, self.relative_base)?;
let res = if val1 == val2 { 1 } else { 0 }; let res = if val1 == val2 { 1 } else { 0 };
dst.set(&mut self.memory, res)?; dst.set(res, &mut self.memory, self.relative_base)?;
self.ip += 4; self.ip += 4;
} }
Opcode::AdjustRelBase(offset) => {
let offset = offset.get(&mut self.memory, self.relative_base)?;
self.relative_base = self.relative_base.wrapping_add(offset as usize);
self.ip += 2;
}
Opcode::Halt => break Ok(true), Opcode::Halt => break Ok(true),
} }
} }

View file

@ -1,9 +1,11 @@
use aoc::err; use aoc::err;
use aoc::Result; use aoc::Result;
#[derive(Debug)]
pub enum Parameter { pub enum Parameter {
Position(usize), Position(usize),
Immediate(i64), Immediate(i64),
Relative(i64),
} }
impl Parameter { impl Parameter {
@ -21,30 +23,73 @@ impl Parameter {
} }
} }
1 => Ok(Parameter::Immediate(val)), 1 => Ok(Parameter::Immediate(val)),
2 => Ok(Parameter::Relative(val)),
_ => Err(err!("wrong mode for parameter: {}", mode)), _ => Err(err!("wrong mode for parameter: {}", mode)),
} }
} }
pub fn get(&self, memory: &[i64]) -> Result<i64> { pub fn get(&self, memory: &mut Vec<i64>, relative_base: usize) -> Result<i64> {
match self { match self {
Parameter::Position(address) => match memory.get(*address) { Parameter::Position(address) => {
Some(val) => Ok(*val), let cell = memory.get(*address);
None => Err(err!("read out of bounds at address {}", address)),
}, match cell {
Some(val) => Ok(*val),
// resize memory if tried to read beyond current memory size
None => {
memory.resize_with(*address + 1, Default::default);
Ok(memory[*address])
}
}
}
Parameter::Immediate(value) => Ok(*value), Parameter::Immediate(value) => Ok(*value),
Parameter::Relative(offset) => {
let address = relative_base.wrapping_add(*offset as usize);
let cell = memory.get(address);
match cell {
Some(val) => Ok(*val),
// resize memory if tried to read beyond current memory size
None => {
memory.resize_with(address + 1, Default::default);
Ok(memory[address])
}
}
}
} }
} }
pub fn set(&self, memory: &mut [i64], value: i64) -> Result<()> { pub fn set(&self, value: i64, memory: &mut Vec<i64>, relative_base: usize) -> Result<()> {
match self { match self {
Parameter::Position(address) => { Parameter::Position(address) => {
let cell = memory let cell = memory.get_mut(*address);
.get_mut(*address)
.ok_or_else(|| err!("memory write out of bounds"))?; match cell {
*cell = value; Some(cell) => *cell = value,
// resize memory if tried to write beyond current memory size
None => {
memory.resize_with(*address + 1, Default::default);
memory[*address] = value;
}
}
Ok(()) Ok(())
} }
Parameter::Immediate(_) => Err(err!("cannot write to immediate parameter")), Parameter::Immediate(_) => Err(err!("cannot write to immediate parameter")),
Parameter::Relative(offset) => {
let address = relative_base.wrapping_add(*offset as usize);
let cell = memory.get_mut(address);
match cell {
Some(cell) => *cell = value,
// resize memory if tried to write beyond current memory size
None => {
memory.resize_with(address + 1, Default::default);
memory[address] = value;
}
}
Ok(())
}
} }
} }
} }