From 5c4e69b85996965d098972edb0b92dd7fb37483c Mon Sep 17 00:00:00 2001 From: cos Date: Sun, 1 Dec 2019 14:32:16 +0100 Subject: Initial commit of solutions for adventofcode.com With rust implementation for day01 of 2019. --- 2019/rust/Cargo.toml | 4 ++++ 2019/rust/day01/Cargo.toml | 7 +++++++ 2019/rust/day01/src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 2019/rust/Cargo.toml create mode 100644 2019/rust/day01/Cargo.toml create mode 100644 2019/rust/day01/src/main.rs (limited to '2019/rust') 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 "] +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 { + let stdin = io::stdin(); + let nums: Vec = stdin.lock().lines().map(|x| x.unwrap().parse().unwrap()).collect(); + nums +} + +fn first_puzzle(payloads:&Vec) -> i32 { + let fuel = payloads.iter().fold(0, |fuel, mass| fuel + mass/3-2); + + fuel +} + +fn second_puzzle(payloads:&Vec) -> 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); +} -- cgit v1.2.3