summaryrefslogtreecommitdiff
path: root/2019
diff options
context:
space:
mode:
authorcos <cos>2019-12-01 17:08:45 +0100
committercos <cos>2019-12-01 17:08:45 +0100
commit6a396b84b0d665607df98452a6d694835d87b5c6 (patch)
tree6ad8f09691c36db31e1bfd661b576f7e093c6f0f /2019
parent5c4e69b85996965d098972edb0b92dd7fb37483c (diff)
downloadadventofcode-6a396b84b0d665607df98452a6d694835d87b5c6.zip
Compactify/clean-up code
Diffstat (limited to '2019')
-rw-r--r--2019/rust/day01/src/main.rs16
1 files changed, 4 insertions, 12 deletions
diff --git a/2019/rust/day01/src/main.rs b/2019/rust/day01/src/main.rs
index c984e80..3eb12f7 100644
--- a/2019/rust/day01/src/main.rs
+++ b/2019/rust/day01/src/main.rs
@@ -2,29 +2,21 @@ 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
+ stdin.lock().lines().map(|line| line.unwrap().parse().unwrap()).collect()
}
fn first_puzzle(payloads:&Vec<i32>) -> i32 {
- let fuel = payloads.iter().fold(0, |fuel, mass| fuel + mass/3-2);
-
- fuel
+ payloads.iter().map(|mass| mass/3-2).sum()
}
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
- }
+ if fuel > 0 { fuel + fuel_required(&fuel) } else { 0 }
}
- let fuel = payloads.iter().fold(0, |fuel, mass| fuel + fuel_required(mass));
- fuel
+ payloads.iter().map(|mass| fuel_required(mass)).sum()
}
fn main() {