diff options
author | cos <cos> | 2019-12-01 14:32:16 +0100 |
---|---|---|
committer | cos <cos> | 2019-12-01 14:35:35 +0100 |
commit | 5c4e69b85996965d098972edb0b92dd7fb37483c (patch) | |
tree | 4969cfe99d576ed6567dc65a80750090d4401af0 | |
download | adventofcode-5c4e69b85996965d098972edb0b92dd7fb37483c.zip |
Initial commit of solutions for adventofcode.com
With rust implementation for day01 of 2019.
-rw-r--r-- | 2019/rust/Cargo.toml | 4 | ||||
-rw-r--r-- | 2019/rust/day01/Cargo.toml | 7 | ||||
-rw-r--r-- | 2019/rust/day01/src/main.rs | 38 |
3 files changed, 49 insertions, 0 deletions
diff --git a/2019/rust/Cargo.toml b/2019/rust/Cargo.toml new file mode 100644 index 0000000..f2bad2a --- /dev/null +++ b/2019/rust/Cargo.toml @@ -0,0 +1,4 @@ +[workspace] +members = [ + "day01", +] diff --git a/2019/rust/day01/Cargo.toml b/2019/rust/day01/Cargo.toml new file mode 100644 index 0000000..02d08dc --- /dev/null +++ b/2019/rust/day01/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day01" +version = "0.1.0" +authors = ["cos <cos>"] +edition = "2018" + +[dependencies] diff --git a/2019/rust/day01/src/main.rs b/2019/rust/day01/src/main.rs new file mode 100644 index 0000000..c984e80 --- /dev/null +++ b/2019/rust/day01/src/main.rs @@ -0,0 +1,38 @@ +use std::io::{self, BufRead}; + +fn read_input() -> Vec<i32> { + let stdin = io::stdin(); + let nums: Vec<i32> = stdin.lock().lines().map(|x| x.unwrap().parse().unwrap()).collect(); + nums +} + +fn first_puzzle(payloads:&Vec<i32>) -> i32 { + let fuel = payloads.iter().fold(0, |fuel, mass| fuel + mass/3-2); + + fuel +} + +fn second_puzzle(payloads:&Vec<i32>) -> i32 { + fn fuel_required(mass:&i32) -> i32 { + let fuel = mass/3-2; + + if fuel > 0 { + fuel + fuel_required(&fuel) + } else { + 0 + } + } + + let fuel = payloads.iter().fold(0, |fuel, mass| fuel + fuel_required(mass)); + fuel +} + +fn main() { + let payloads = read_input(); + + let first = first_puzzle(&payloads); + println!("first: {}", first); + + let second = second_puzzle(&payloads); + println!("second: {}", second); +} |