From: Greg Burri Date: Sun, 1 Dec 2019 14:00:31 +0000 (+0100) Subject: Day 01 X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=0903dfa88f88f9c8fcab9a8c3716e02e4899cc73;p=advent_of_code_2019.git Day 01 --- 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/data/day01.input b/data/day01.input new file mode 100644 index 0000000..03a8af3 --- /dev/null +++ b/data/day01.input @@ -0,0 +1,100 @@ +124846 +99745 +110203 +140165 +110228 +65706 +128481 +75921 +57331 +72951 +133413 +99524 +79546 +54653 +55166 +66215 +147696 +91054 +64752 +76311 +139572 +61110 +65846 +121489 +147534 +66591 +109963 +83412 +138965 +70102 +128844 +141002 +77655 +68539 +128687 +70559 +140747 +51397 +117550 +91515 +60960 +133280 +83244 +106644 +100333 +67608 +118120 +60024 +115547 +136229 +108403 +128776 +109599 +111189 +98538 +129715 +116630 +120772 +80105 +52489 +130247 +144003 +85226 +83769 +137921 +54737 +126406 +108756 +149633 +138201 +78980 +126909 +125768 +86214 +54873 +97723 +92677 +120405 +143317 +102981 +142668 +100398 +67258 +126583 +114611 +102525 +115205 +78329 +140703 +136978 +94465 +129510 +81039 +141997 +120643 +55377 +89966 +113672 +112665 +51323 \ No newline at end of file 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); + } + } +}