Rename stuff in intcode

This commit is contained in:
kageru 2019-12-11 18:59:20 +01:00
parent b468b66ed2
commit 85475a3186
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 16 additions and 11 deletions

View File

@ -1,13 +1,18 @@
pub struct Amplifier {
pub struct IntComputer {
pub pos: i64,
pub tape: Vec<i64>,
pub params: Vec<i64>,
relative_base: i64,
}
impl Amplifier {
pub enum IntComputerResult {
Halt,
Output(i64),
}
impl IntComputer {
pub fn new(tape: Vec<i64>, pos: i64, params: Vec<i64>) -> Self {
Self {
IntComputer {
pos,
tape,
params,
@ -15,15 +20,15 @@ impl Amplifier {
}
}
pub fn run(&mut self) -> Result<i64, i64> {
pub fn run(&mut self) -> IntComputerResult {
loop {
match self.decode_next() {
Some(op) => {
if let Some(o) = self.execute(op) {
return Err(o);
return IntComputerResult::Output(o);
}
}
None => return Ok(self.params.pop().unwrap()),
None => return IntComputerResult::Halt,
}
}
}

View File

@ -1,9 +1,9 @@
use amplifier::*;
use intcomputer::*;
use itertools::Itertools;
use std::convert::TryFrom;
use std::io::{self, BufRead};
use std::ops::Range;
mod amplifier;
mod intcomputer;
/**
* Construct amplifiers and run all of them for a single given input tape
@ -14,14 +14,14 @@ mod amplifier;
pub fn run_for_input(input: &Vec<i64>, acc: &mut i64, amp_phases: Vec<i64>) -> i64 {
let mut amplifiers: Vec<_> = amp_phases
.into_iter()
.map(|amp| Amplifier::new(input.clone(), 0, vec![amp]))
.map(|amp| IntComputer::new(input.clone(), 0, vec![amp]))
.collect();
for state in (0..amplifiers.len()).cycle() {
let amplifier = amplifiers.get_mut(state).unwrap();
amplifier.params.insert(0, *acc);
match amplifier.run() {
Err(output) => *acc = output,
Ok(_) => break,
IntComputerResult::Output(output) => *acc = output,
IntComputerResult::Halt => break,
}
}
*acc