diff options
Diffstat (limited to '2019/rust/day01/src/main.rs')
-rw-r--r-- | 2019/rust/day01/src/main.rs | 38 |
1 files changed, 38 insertions, 0 deletions
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); +} |