Initial computer reading.

This commit is contained in:
2019-12-13 18:33:17 -08:00
parent 720aa224c5
commit 8a510052b5
2 changed files with 77 additions and 1 deletions

View File

@@ -1,4 +1,6 @@
use clap::{App,Arg,SubCommand};
use std::fs;
use std::str;
fn is_number(s: String) -> Result<(), String> {
match u64::from_str_radix(&s, 10) {
@@ -7,6 +9,14 @@ fn is_number(s: String) -> Result<(), String> {
}
}
fn is_file(s: String) -> Result<(), String> {
match fs::metadata(&s) {
Err(e) => Err(e.to_string()),
Ok(md) if md.is_file() => Ok(()),
_ => Err(format!("{} is not a file.", s))
}
}
fn calculate_base_fuel(mass: u64) -> u64 {
let div3 = mass / 3;
@@ -32,6 +42,48 @@ fn calculate_fuel(mass: u64) -> u64 {
}
}
struct Computer {
memory: Vec<u64>,
position: usize
}
impl Computer {
fn load(path: &str, position: usize) -> Computer {
let mut memory = vec![];
let byte_buffer = fs::read(path).unwrap();
let char_buffer = str::from_utf8(&byte_buffer).unwrap();
let mut current = 0;
for c in char_buffer.chars() {
match c {
',' => {
memory.push(current);
current = 0;
}
_ if c.is_digit(10) => {
let val = c.to_digit(10).unwrap() as u64;
current = (current * 10) + val;
}
_ if c.is_whitespace() => {
}
_ => {
panic!("Unrecognized character: '{}'", c);
}
}
}
memory.push(current);
Computer{ memory, position }
}
fn show(&self) {
for (idx, val) in self.memory.iter().enumerate() {
println!("{:08}: {}", idx, val);
}
println!("POSITION: {}", self.position);
}
}
fn main() {
let matches = App::new("My Advent of Code Thing")
.version("1.0")
@@ -42,7 +94,22 @@ fn main() {
.arg(Arg::with_name("NUM")
.help("The mass of the ship")
.multiple(true)
.validator(is_number)))
.validator(is_number))
)
.subcommand(SubCommand::with_name("compute")
.about("run the given computer")
.arg(Arg::with_name("START_POSITION")
.short("p")
.long("start-position")
.help("The starting position to execute from.")
.default_value("0")
.validator(is_number))
.arg(Arg::with_name("COMPUTER")
.index(1)
.help("The computer to run.")
.required(true)
.validator(is_file))
)
.get_matches();
if let Some(problem1) = matches.subcommand_matches("fuel") {
@@ -65,5 +132,13 @@ fn main() {
}
}
if let Some(problem2) = matches.subcommand_matches("compute") {
let start_pos_str = problem2.value_of("START_POSITION").unwrap();
let start_pos = usize::from_str_radix(&start_pos_str, 10).unwrap();
let mut computer = Computer::load(problem2.value_of("COMPUTER").unwrap(), start_pos);
println!("Initial Computer:");
computer.show();
}
println!("Failed to run a reasonable command.");
}