From 720aa224c5a65506192a1ee21383d05bc8198ffd Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Fri, 13 Dec 2019 18:01:45 -0800 Subject: [PATCH] Day 1. --- .gitignore | 2 ++ Cargo.toml | 10 ++++++ inputs/day1 | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 69 ++++++++++++++++++++++++++++++++++++ 4 files changed, 181 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 inputs/day1 create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1e7caa9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +target/ diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2bdacd2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "aoc" +version = "0.1.0" +authors = ["awick"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = "^2.33.0" diff --git a/inputs/day1 b/inputs/day1 new file mode 100644 index 0000000..eb73ad9 --- /dev/null +++ b/inputs/day1 @@ -0,0 +1,100 @@ +129880 +115705 +118585 +124631 +81050 +138183 +61173 +95354 +130788 +89082 +75554 +110104 +140528 +71783 +125889 +126602 +73089 +76822 +51774 +85940 +81004 +149584 +145921 +105570 +142370 +80823 +147779 +115651 +70250 +67763 +128192 +51298 +134963 +73510 +90976 +141216 +65134 +140468 +143998 +101711 +88477 +53335 +138328 +141186 +149804 +64950 +53107 +54648 +97557 +85927 +125038 +80514 +64912 +140591 +114229 +57089 +123464 +127572 +137169 +146550 +51138 +115504 +128034 +147244 +108107 +101205 +51498 +136829 +140171 +59441 +144489 +139384 +145841 +96771 +116821 +88599 +126780 +65012 +67621 +129699 +149639 +97590 +147527 +117462 +146709 +60527 +107643 +92956 +72177 +92285 +62475 +63099 +66904 +77268 +62945 +134364 +106924 +117842 +130016 +123712 diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..688f216 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,69 @@ +use clap::{App,Arg,SubCommand}; + +fn is_number(s: String) -> Result<(), String> { + match u64::from_str_radix(&s, 10) { + Ok(_) => Ok(()), + Err(e) => Err(e.to_string()), + } +} + +fn calculate_base_fuel(mass: u64) -> u64 { + let div3 = mass / 3; + + if div3 >= 2 { + div3 - 2 + } else { + 0 + } +} + +fn calculate_fuel(mass: u64) -> u64 { + let mut res = calculate_base_fuel(mass); + let mut last_round = res; + + loop { + let new_round = calculate_base_fuel(last_round); + if new_round == 0 { + return res; + } else { + res += new_round; + last_round = new_round; + } + } +} + +fn main() { + let matches = App::new("My Advent of Code Thing") + .version("1.0") + .author("Adam Wick ") + .about("Runs advent of code programs") + .subcommand(SubCommand::with_name("fuel") + .about("runs the fuel computation from day1") + .arg(Arg::with_name("NUM") + .help("The mass of the ship") + .multiple(true) + .validator(is_number))) + .get_matches(); + + if let Some(problem1) = matches.subcommand_matches("fuel") { + let mut total = 0; + + match problem1.values_of("NUM") { + None => + println!("ERROR: No values to compute fuel for!"), + Some(masses) => { + for mass_str in masses { + let mass = u64::from_str_radix(&mass_str, 10).unwrap(); + let fuel = calculate_fuel(mass); + println!("Mass {}: {} fuel", mass, fuel); + total += fuel; + } + + println!("TOTAL FUEL: {}", total); + std::process::exit(0); + } + } + } + + println!("Failed to run a reasonable command."); +}