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
data/day01.input [new file with mode: 0644]
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/data/day01.input b/data/day01.input
new file mode 100644 (file)
index 0000000..03a8af3
--- /dev/null
@@ -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 (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);
+        }
+    }
+}