-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
41 lines (35 loc) · 827 Bytes
/
main.rs
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
39
40
41
use std::fs;
fn get_input(fp: &str) -> Vec<usize> {
fs::read_to_string(fp)
.expect("Cannot read file")
.split_whitespace()
.map(|x| x.parse().unwrap())
.collect()
}
fn get_fuel(x: usize) -> usize {
let fuel = x / 3;
if fuel > 2 {
fuel - 2
} else {
0
}
}
fn get_full_fuel(x: usize) -> usize {
let fuel = get_fuel(x);
if fuel > 0 {
fuel + get_full_fuel(fuel)
} else {
fuel
}
}
fn part_one(modules: &[usize]) -> usize {
modules.iter().map(|&x| get_fuel(x)).sum()
}
fn part_two(modules: &[usize]) -> usize {
modules.iter().map(|&x| get_full_fuel(x)).sum()
}
fn main() {
let modules = get_input("../input.txt");
println!("Part one: {}", part_one(&modules));
println!("Part two: {}", part_two(&modules));
}