From 73e4d50c51464d6b7c6d10e35d53f465c439a05d Mon Sep 17 00:00:00 2001 From: Johannes Date: Sat, 21 Dec 2019 23:08:35 +0100 Subject: [PATCH] easier loading of IntCodeComputer program and single step function --- src/tasks/day05.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/tasks/day05.rs b/src/tasks/day05.rs index 554372c..85aea50 100644 --- a/src/tasks/day05.rs +++ b/src/tasks/day05.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::path::Path; pub type RAM = HashMap; @@ -14,6 +15,15 @@ pub fn run() { task2(ram.clone()); } +pub fn load_ram>(path: P) -> RAM { + let input = std::fs::read_to_string(path).unwrap(); + input + .split(",") + .enumerate() + .map(|(i, s)| (i, s.parse::().unwrap())) + .collect() +} + pub struct IntCodeComputer { input_storage: Vec, output_storage: Vec, @@ -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");