From c337791ad8a14d7948cc2cb79cb98822b51497ce Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sun, 1 Dec 2019 15:00:31 +0100 Subject: [PATCH] Day 01 --- Cargo.toml | 10 ++++++++++ README.md | 1 + src/common.rs | 6 ++++++ src/day01.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/common.rs create mode 100644 src/day01.rs create mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c7438f7 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "advent_of_code_2019" +version = "0.1.0" +authors = ["Greg Burri "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.8" \ No newline at end of file diff --git a/README.md b/README.md index 927a594..d438ab1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # AdventOfCode2019 + https://adventofcode.com/2019 diff --git a/src/common.rs b/src/common.rs new file mode 100644 index 0000000..4893112 --- /dev/null +++ b/src/common.rs @@ -0,0 +1,6 @@ +use std::fs; +use std::path::Path; + +pub fn read_list_of_numbers>(file: P) -> Vec { + fs::read_to_string(file).unwrap().split("\n").map(|line| line.parse::().unwrap()).collect() +} \ No newline at end of file diff --git a/src/day01.rs b/src/day01.rs new file mode 100644 index 0000000..1039494 --- /dev/null +++ b/src/day01.rs @@ -0,0 +1,43 @@ +pub fn sum_mass_to_fuel(masses: &[i32]) -> i32 { + masses.iter().fold(0, |sum, mass| sum + mass_to_fuel(*mass)) +} + +pub fn sum_mass_to_fuel_2(masses: &[i32]) -> i32 { + masses.iter().fold(0, |sum, mass| sum + mass_to_fuel_2(*mass)) +} + +pub fn mass_to_fuel(mass: i32) -> i32 { + mass / 3 - 2 +} + +pub fn mass_to_fuel_2(mass: i32) -> i32 { + let mut sum = 0; + let mut current_mass = mass; + loop { + let fuel = mass_to_fuel(current_mass); + if fuel <= 0 { break } + current_mass = fuel; + sum += fuel; + } + sum +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn simple_cases() { + assert_eq!(mass_to_fuel(12), 2); + assert_eq!(mass_to_fuel(14), 2); + assert_eq!(mass_to_fuel(1969), 654); + assert_eq!(mass_to_fuel(100756), 33583); + } + + #[test] + fn simple_cases_2() { + assert_eq!(mass_to_fuel_2(14), 2); + assert_eq!(mass_to_fuel_2(1969), 966); + assert_eq!(mass_to_fuel_2(100756), 50346); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..509a1e5 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,32 @@ +use std::env; + +mod day01; +mod common; + +fn day01() { + let masses = common::read_list_of_numbers("data/day01.input"); + println!("Day01 part1: {}", day01::sum_mass_to_fuel(&masses)); + println!(" part2: {}", day01::sum_mass_to_fuel_2(&masses)); +} + +fn do_day(n: i32) { + match n { + 1 => day01 (), + _ => panic!("Unknown day: {}", n) + } +} + +fn main() { + println!("https://adventofcode.com/2019"); + + let args: Vec = env::args().skip(1).collect(); + + if args.iter().count() == 0 { + println!("ASD"); + } else { + for arg in args { + do_day(arg.parse::().unwrap()); + //println!("{}", arg); + } + } +} -- 2.45.2