blob: c984e80835d2f16f6fc6ca2848e0bde132537133 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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);
}
|