From 22fcfdae4f3dea1340fed67f091fd416eae35279 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Sun, 1 Dec 2019 16:03:34 +0100 Subject: [PATCH] 2019: day01: part 1 --- Cargo.lock | 4 ++ Cargo.toml | 1 + aoc2019/Cargo.toml | 9 ++++ aoc2019/input/day01.txt | 100 ++++++++++++++++++++++++++++++++++++++++ aoc2019/src/day01.rs | 42 +++++++++++++++++ aoc2019/src/lib.rs | 8 ++++ aoc2019/src/main.rs | 29 ++++++++++++ 7 files changed, 193 insertions(+) create mode 100644 aoc2019/Cargo.toml create mode 100644 aoc2019/input/day01.txt create mode 100644 aoc2019/src/day01.rs create mode 100644 aoc2019/src/lib.rs create mode 100644 aoc2019/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 49c24ab..4d33b98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,3 +4,7 @@ name = "aoc2018" version = "0.1.0" +[[package]] +name = "aoc2019" +version = "0.1.0" + diff --git a/Cargo.toml b/Cargo.toml index 2e6289c..b8bf296 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,5 @@ [workspace] members = [ "aoc2018", + "aoc2019", ] diff --git a/aoc2019/Cargo.toml b/aoc2019/Cargo.toml new file mode 100644 index 0000000..488af98 --- /dev/null +++ b/aoc2019/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "aoc2019" +version = "0.1.0" +authors = ["Antoine Martin "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/aoc2019/input/day01.txt b/aoc2019/input/day01.txt new file mode 100644 index 0000000..0b4982a --- /dev/null +++ b/aoc2019/input/day01.txt @@ -0,0 +1,100 @@ +102111 +81442 +104967 +146349 +97956 +148336 +55752 +110518 +136080 +79361 +101709 +71796 +72543 +88058 +87187 +67778 +75471 +135632 +101309 +67060 +87048 +64775 +52160 +119184 +55284 +83289 +94809 +78017 +117854 +76469 +94504 +92372 +133595 +107269 +57182 +85578 +83777 +91167 +86119 +114263 +60369 +72562 +69544 +92699 +135045 +147230 +83105 +143488 +56494 +63019 +112366 +128119 +58293 +132542 +93266 +76032 +111651 +127490 +107633 +61260 +120876 +116254 +101151 +128722 +111623 +103470 +53224 +93922 +87624 +131864 +82431 +90465 +148373 +90119 +123744 +118713 +143700 +113581 +140687 +119516 +149497 +72281 +108641 +111605 +148274 +69326 +116571 +56540 +87595 +55068 +120602 +56557 +125534 +133162 +51828 +117883 +94637 +54577 +135114 +83866 diff --git a/aoc2019/src/day01.rs b/aoc2019/src/day01.rs new file mode 100644 index 0000000..4404875 --- /dev/null +++ b/aoc2019/src/day01.rs @@ -0,0 +1,42 @@ +use super::Result; + +const INPUT: &str = include_str!("../input/day01.txt"); + +pub fn run() -> Result<()> { + println!("part 1: {}", part1(INPUT)?); + + Ok(()) +} + +fn fuel_needed(module_weight: u64) -> u64 { + (module_weight / 3) - 2 +} + +fn part1(input: &str) -> Result { + input + .lines() + .map(|line| line.parse::()) + .map(|w| match w { + Ok(w) => Ok(fuel_needed(w)), + Err(e) => Err(Box::from(e)), + }) + .sum() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_provided() { + assert_eq!(fuel_needed(12), 2); + assert_eq!(fuel_needed(14), 2); + assert_eq!(fuel_needed(1969), 654); + assert_eq!(fuel_needed(100756), 33583); + } + + #[test] + fn part1_real() { + assert_eq!(part1(INPUT).unwrap(), 3268951); + } +} diff --git a/aoc2019/src/lib.rs b/aoc2019/src/lib.rs new file mode 100644 index 0000000..656543d --- /dev/null +++ b/aoc2019/src/lib.rs @@ -0,0 +1,8 @@ +pub mod day01; + +pub type Result = std::result::Result>; + +#[macro_export] +macro_rules! err { + ($($string:expr),+) => (Box::::from(format!($($string),+))) +} diff --git a/aoc2019/src/main.rs b/aoc2019/src/main.rs new file mode 100644 index 0000000..305f2ad --- /dev/null +++ b/aoc2019/src/main.rs @@ -0,0 +1,29 @@ +use std::env; + +use aoc2019::day01; + +use aoc2019::Result; + +fn main() -> Result<()> { + let days: &[fn() -> Result<()>] = &[day01::run]; + + let mut args = env::args(); + args.next(); + + match args.next() { + Some(arg) => { + let day: usize = arg.parse().expect("Please provide a day number"); + days[day - 1]().expect("error running day specified"); + } + None => { + for (i, day) in days.iter().enumerate() { + let i = i + 1; + println!("day{}: ", i); + day().unwrap_or_else(|e| panic!("error running day {}: {}", i, e)); + println!(); + } + } + } + + Ok(()) +}