easier loading of IntCodeComputer program and single step function
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
|
||||
pub type RAM = HashMap<usize, i128>;
|
||||
|
||||
@@ -14,6 +15,15 @@ pub fn run() {
|
||||
task2(ram.clone());
|
||||
}
|
||||
|
||||
pub fn load_ram<P: AsRef<Path>>(path: P) -> RAM {
|
||||
let input = std::fs::read_to_string(path).unwrap();
|
||||
input
|
||||
.split(",")
|
||||
.enumerate()
|
||||
.map(|(i, s)| (i, s.parse::<i128>().unwrap()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub struct IntCodeComputer {
|
||||
input_storage: Vec<i128>,
|
||||
output_storage: Vec<i128>,
|
||||
@@ -169,6 +179,13 @@ impl IntCodeComputer {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_single(&mut self, input: i128) -> i128 {
|
||||
self.set_input(&[input]);
|
||||
self.clear_output();
|
||||
self.run_until_input_empty();
|
||||
*self.get_output().first().unwrap()
|
||||
}
|
||||
|
||||
pub fn run_until_end(&mut self) {
|
||||
if !self.run_until_input_empty() {
|
||||
panic!("There wasn't enough input given to run until the program halted");
|
||||
|
||||
Reference in New Issue
Block a user