From 6a396b84b0d665607df98452a6d694835d87b5c6 Mon Sep 17 00:00:00 2001 From: cos Date: Sun, 1 Dec 2019 17:08:45 +0100 Subject: Compactify/clean-up code --- 2019/rust/day01/src/main.rs | 16 ++++------------ 1 file 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 { let stdin = io::stdin(); - let nums: Vec = 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 { - 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 { 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() { -- cgit v1.2.3