Day 01
authorGreg Burri <greg.burri@gmail.com>
Sun, 1 Dec 2019 14:00:31 +0000 (15:00 +0100)
committerGreg Burri <greg.burri@gmail.com>
Sun, 1 Dec 2019 14:00:31 +0000 (15:00 +0100)
Cargo.toml [new file with mode: 0644]
README.md
src/common.rs [new file with mode: 0644]
src/day01.rs [new file with mode: 0644]
src/main.rs [new file with mode: 0644]

diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644 (file)
index 0000000..c7438f7
--- /dev/null
@@ -0,0 +1,10 @@
+[package]
+name = "advent_of_code_2019"
+version = "0.1.0"
+authors = ["Greg Burri <greg.burri@gmail.com>"]
+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
index 927a594..d438ab1 100644 (file)
--- 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 (file)
index 0000000..4893112
--- /dev/null
@@ -0,0 +1,6 @@
+use std::fs;\r
+use std::path::Path;\r
+\r
+pub fn read_list_of_numbers<P: AsRef<Path>>(file: P) -> Vec<i32> {\r
+    fs::read_to_string(file).unwrap().split("\n").map(|line| line.parse::<i32>().unwrap()).collect()\r
+}
\ No newline at end of file
diff --git a/src/day01.rs b/src/day01.rs
new file mode 100644 (file)
index 0000000..1039494
--- /dev/null
@@ -0,0 +1,43 @@
+pub fn sum_mass_to_fuel(masses: &[i32]) -> i32 {\r
+    masses.iter().fold(0, |sum, mass| sum + mass_to_fuel(*mass))\r
+}\r
+\r
+pub fn sum_mass_to_fuel_2(masses: &[i32]) -> i32 {\r
+    masses.iter().fold(0, |sum, mass| sum + mass_to_fuel_2(*mass))\r
+}\r
+\r
+pub fn mass_to_fuel(mass: i32) -> i32 {\r
+    mass / 3 - 2\r
+}\r
+\r
+pub fn mass_to_fuel_2(mass: i32) -> i32 {\r
+    let mut sum = 0;\r
+    let mut current_mass = mass;\r
+    loop {\r
+        let fuel = mass_to_fuel(current_mass);\r
+        if fuel <= 0 { break }\r
+        current_mass = fuel;\r
+        sum += fuel;\r
+    }\r
+    sum\r
+}\r
+\r
+#[cfg(test)]\r
+mod tests {\r
+    use super::*;\r
+\r
+    #[test]\r
+    fn simple_cases() {\r
+        assert_eq!(mass_to_fuel(12), 2);\r
+        assert_eq!(mass_to_fuel(14), 2);\r
+        assert_eq!(mass_to_fuel(1969), 654);\r
+        assert_eq!(mass_to_fuel(100756), 33583);\r
+    }\r
+\r
+    #[test]\r
+    fn simple_cases_2() {\r
+        assert_eq!(mass_to_fuel_2(14), 2);\r
+        assert_eq!(mass_to_fuel_2(1969), 966);\r
+        assert_eq!(mass_to_fuel_2(100756), 50346);\r
+    }\r
+}
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
new file mode 100644 (file)
index 0000000..509a1e5
--- /dev/null
@@ -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<String> = env::args().skip(1).collect();
+
+    if args.iter().count() == 0 {
+        println!("ASD");
+    } else {
+        for arg in args {
+            do_day(arg.parse::<i32>().unwrap());
+            //println!("{}", arg);
+        }
+    }
+}