Refactor intcode

master
parent f007bee168
commit 4fe0491ff1
  1. 14
      src/intcode/mod.rs
  2. 66
      src/intcode/ops.rs

@ -33,7 +33,7 @@ enum ParamMode {
Imediate
}
type OpCodeFn = fn(&Vec<Value>) -> ResultAction;
type OpCodeFn = fn(&mut IntCode, &Vec<Value>) -> ResultAction;
impl ParamMode {
fn from_int(mode: usize) -> Self {
@ -75,22 +75,22 @@ impl IntCode {
self.memory[self.addr_ptr - 1]
}
fn read(&self, address: usize) -> isize {
pub fn read(&self, address: usize) -> isize {
self.memory[address]
}
fn write(&mut self, address: usize, value: isize) {
pub fn write(&mut self, address: usize, value: isize) {
self.memory[address] = value;
}
fn jump(&mut self, address: usize) {
pub fn jump(&mut self, address: usize) {
self.addr_ptr = address;
}
pub fn run(&mut self) -> isize {
loop {
let op = Op::from_intcode(self);
let res = op.run();
let res = op.run(self);
match res {
ResultAction::Write { address, value } => self.write(address, value),
@ -145,8 +145,8 @@ impl Op {
}
}
fn run(&self) -> ResultAction {
(self.function)(&self.argv)
fn run(&self, mut intcode: &mut IntCode) -> ResultAction {
(self.function)(&mut intcode, &self.argv)
}
}

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

Loading…
Cancel
Save