Intcode refactoring

master
parent 4fe0491ff1
commit e6fd7d3db0
  1. 35
      src/intcode/mod.rs
  2. 30
      src/intcode/ops.rs

@ -3,6 +3,7 @@ mod ops;
pub struct IntCode {
addr_ptr: usize,
memory: Vec<isize>,
running: bool,
}
pub struct Value {
@ -16,24 +17,12 @@ struct Op {
function: OpCodeFn,
}
pub enum ResultAction {
None,
Terminate,
Write {
address: usize,
value: isize,
},
Jump {
address: usize,
}
}
enum ParamMode {
Position,
Imediate
}
type OpCodeFn = fn(&mut IntCode, &Vec<Value>) -> ResultAction;
type OpCodeFn = fn(&mut IntCode, &Vec<Value>);
impl ParamMode {
fn from_int(mode: usize) -> Self {
@ -49,7 +38,8 @@ impl IntCode {
pub fn from_vec(v: Vec<isize>) -> Self {
IntCode {
memory: v,
addr_ptr: 0
addr_ptr: 0,
running: true,
}
}
@ -87,17 +77,14 @@ impl IntCode {
self.addr_ptr = address;
}
pub fn terminate(&mut self) {
self.running = false;
}
pub fn run(&mut self) -> isize {
loop {
while self.running {
let op = Op::from_intcode(self);
let res = op.run(self);
match res {
ResultAction::Write { address, value } => self.write(address, value),
ResultAction::Jump { address } => self.jump(address),
ResultAction::None => (),
ResultAction::Terminate => break,
}
op.run(self);
}
self.read(0)
@ -145,7 +132,7 @@ impl Op {
}
}
fn run(&self, mut intcode: &mut IntCode) -> ResultAction {
fn run(&self, mut intcode: &mut IntCode) {
(self.function)(&mut intcode, &self.argv)
}
}

@ -1,60 +1,52 @@
use super::{Value, ResultAction, IntCode};
use super::{Value, IntCode};
use std::io;
pub fn add(intcode: &mut IntCode, argv: &Vec<Value>) -> ResultAction {
pub fn add(intcode: &mut IntCode, argv: &Vec<Value>) {
intcode.write(argv[2].address.unwrap(), argv[0].value + argv[1].value);
ResultAction::None
}
pub fn mul(intcode: &mut IntCode, argv: &Vec<Value>) -> ResultAction {
pub fn mul(intcode: &mut IntCode, argv: &Vec<Value>) {
intcode.write(argv[2].address.unwrap(), argv[0].value * argv[1].value);
ResultAction::None
}
pub fn exit(intcode: &mut IntCode, _argv: &Vec<Value>) -> ResultAction {
ResultAction::Terminate
pub fn exit(intcode: &mut IntCode, _argv: &Vec<Value>) {
intcode.terminate();
}
pub fn cin(intcode: &mut IntCode, argv: &Vec<Value>) -> ResultAction {
pub fn cin(intcode: &mut IntCode, argv: &Vec<Value>) {
let mut value = String::new();
let _ = io::stdin()
.read_line(&mut value)
.expect("Unable to read stdin");
intcode.write(argv[0].address.unwrap(), value.trim().parse().unwrap());
ResultAction::None
}
pub fn cout(_intcode: &mut IntCode, argv: &Vec<Value>) -> ResultAction {
pub fn cout(_intcode: &mut IntCode, argv: &Vec<Value>) {
println!("{}", argv[0].value);
ResultAction::None
}
pub fn jump_if_true(intcode: &mut IntCode, argv: &Vec<Value>) -> ResultAction {
pub fn jump_if_true(intcode: &mut IntCode, argv: &Vec<Value>) {
if argv[0].value != 0 {
intcode.jump(argv[1].value as usize)
}
ResultAction::None
}
pub fn jump_if_false(intcode: &mut IntCode, argv: &Vec<Value>) -> ResultAction {
pub fn jump_if_false(intcode: &mut IntCode, argv: &Vec<Value>) {
if argv[0].value == 0 {
intcode.jump(argv[1].value as usize)
}
ResultAction::None
}
pub fn lt(intcode: &mut IntCode, argv: &Vec<Value>) -> ResultAction {
pub fn lt(intcode: &mut IntCode, argv: &Vec<Value>) {
intcode.write(
argv[2].address.unwrap(),
if argv[0].value < argv[1].value { 1 } else { 0 }
);
ResultAction::None
}
pub fn eq(intcode: &mut IntCode, argv: &Vec<Value>) -> ResultAction {
pub fn eq(intcode: &mut IntCode, argv: &Vec<Value>) {
intcode.write(
argv[2].address.unwrap(),
if argv[0].value == argv[1].value { 1 } else { 0 }
);
ResultAction::None
}

Loading…
Cancel
Save