summaryrefslogtreecommitdiff
path: root/2019/rust/day01/src/main.rs
blob: 3eb12f76aa1a2b91696355df83fe1b62d86c6c41 (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
use std::io::{self, BufRead};

fn read_input() -> Vec<i32> {
    let stdin = io::stdin();
    stdin.lock().lines().map(|line| line.unwrap().parse().unwrap()).collect()
}

fn first_puzzle(payloads:&Vec<i32>) -> i32 {
    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 }
    }

    payloads.iter().map(|mass| fuel_required(mass)).sum()
}

fn main() {
    let payloads = read_input();

    let first = first_puzzle(&payloads);
    println!("first: {}", first);

    let second = second_puzzle(&payloads);
    println!("second: {}", second);
}