summaryrefslogtreecommitdiff
path: root/2019/rust
diff options
context:
space:
mode:
authorcos <cos>2019-12-01 14:32:16 +0100
committercos <cos>2019-12-01 14:35:35 +0100
commit5c4e69b85996965d098972edb0b92dd7fb37483c (patch)
tree4969cfe99d576ed6567dc65a80750090d4401af0 /2019/rust
downloadadventofcode-5c4e69b85996965d098972edb0b92dd7fb37483c.zip
Initial commit of solutions for adventofcode.com
With rust implementation for day01 of 2019.
Diffstat (limited to '2019/rust')
-rw-r--r--2019/rust/Cargo.toml4
-rw-r--r--2019/rust/day01/Cargo.toml7
-rw-r--r--2019/rust/day01/src/main.rs38
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);
+}