From 8a510052b5f8817fcbf9e07981360ce28ded00f8 Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Fri, 13 Dec 2019 18:33:17 -0800 Subject: [PATCH] Initial computer reading. --- inputs/day2 | 1 + src/main.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 inputs/day2 diff --git a/inputs/day2 b/inputs/day2 new file mode 100644 index 0000000..aed4846 --- /dev/null +++ b/inputs/day2 @@ -0,0 +1 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,1,19,9,23,1,23,9,27,1,10,27,31,1,13,31,35,1,35,10,39,2,39,9,43,1,43,13,47,1,5,47,51,1,6,51,55,1,13,55,59,1,59,6,63,1,63,10,67,2,67,6,71,1,71,5,75,2,75,10,79,1,79,6,83,1,83,5,87,1,87,6,91,1,91,13,95,1,95,6,99,2,99,10,103,1,103,6,107,2,6,107,111,1,13,111,115,2,115,10,119,1,119,5,123,2,10,123,127,2,127,9,131,1,5,131,135,2,10,135,139,2,139,9,143,1,143,2,147,1,5,147,0,99,2,0,14,0 diff --git a/src/main.rs b/src/main.rs index 688f216..8a9cd2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, + 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."); }