easier loading of IntCodeComputer program and single step function

This commit is contained in:
Johannes
2019-12-21 23:08:35 +01:00
parent ddd215c9a2
commit 73e4d50c51

View File

@@ -1,4 +1,5 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::path::Path;
pub type RAM = HashMap<usize, i128>; pub type RAM = HashMap<usize, i128>;
@@ -14,6 +15,15 @@ pub fn run() {
task2(ram.clone()); 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 { pub struct IntCodeComputer {
input_storage: Vec<i128>, input_storage: Vec<i128>,
output_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) { pub fn run_until_end(&mut self) {
if !self.run_until_input_empty() { if !self.run_until_input_empty() {
panic!("There wasn't enough input given to run until the program halted"); panic!("There wasn't enough input given to run until the program halted");