Split intcode mod

master
parent 6c445ab130
commit 4596b08f56
  1. 71
      src/intcode/mod.rs
  2. 66
      src/intcode/ops.rs

@ -1,3 +1,5 @@
mod ops;
pub struct IntCode {
addr_ptr: usize,
memory: Vec<isize>,
@ -148,75 +150,6 @@ impl Op {
}
}
mod ops {
use super::{Value, ResultAction};
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 mul(argv: &Vec<Value>) -> ResultAction {
ResultAction::Write {
address: argv[2].address.unwrap(),
value: (argv[0].value * argv[1].value) as isize
}
}
pub fn exit(_argv: &Vec<Value>) -> ResultAction {
ResultAction::Terminate
}
pub fn cin(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(),
}
}
pub fn cout(argv: &Vec<Value>) -> ResultAction {
println!("{}", argv[0].value);
ResultAction::None
}
pub fn jump_if_true(argv: &Vec<Value>) -> ResultAction {
if argv[0].value != 0 {
ResultAction::Jump { address: argv[1].value as usize }
} else {
ResultAction::None
}
}
pub fn jump_if_false(argv: &Vec<Value>) -> ResultAction {
if argv[0].value == 0 {
ResultAction::Jump { address: argv[1].value as usize }
} else {
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 eq(argv: &Vec<Value>) -> ResultAction {
ResultAction::Write {
address: argv[2].address.unwrap(),
value: if argv[0].value == argv[1].value { 1 } else { 0 }
}
}
}
#[cfg(test)]
mod test {

@ -0,0 +1,66 @@
use super::{Value, ResultAction};
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 mul(argv: &Vec<Value>) -> ResultAction {
ResultAction::Write {
address: argv[2].address.unwrap(),
value: (argv[0].value * argv[1].value) as isize
}
}
pub fn exit(_argv: &Vec<Value>) -> ResultAction {
ResultAction::Terminate
}
pub fn cin(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(),
}
}
pub fn cout(argv: &Vec<Value>) -> ResultAction {
println!("{}", argv[0].value);
ResultAction::None
}
pub fn jump_if_true(argv: &Vec<Value>) -> ResultAction {
if argv[0].value != 0 {
ResultAction::Jump { address: argv[1].value as usize }
} else {
ResultAction::None
}
}
pub fn jump_if_false(argv: &Vec<Value>) -> ResultAction {
if argv[0].value == 0 {
ResultAction::Jump { address: argv[1].value as usize }
} else {
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 eq(argv: &Vec<Value>) -> ResultAction {
ResultAction::Write {
address: argv[2].address.unwrap(),
value: if argv[0].value == argv[1].value { 1 } else { 0 }
}
}
Loading…
Cancel
Save