-
Notifications
You must be signed in to change notification settings - Fork 9
/
day01.rs
34 lines (31 loc) · 1 KB
/
day01.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
//! # The Tyranny of the Rocket Equation
//!
//! The title of the problem is a reference to the
//! [real life equation](https://en.wikipedia.org/wiki/Tsiolkovsky_rocket_equation).
use crate::util::parse::*;
/// The [`iter_unsigned`] utility method extracts and parses numbers from surrounding text.
///
/// [`iter_unsigned`]: crate::util::parse
pub fn parse(input: &str) -> Vec<u32> {
input.iter_unsigned().collect()
}
/// Calculate fuel requirements following the formula.
pub fn part1(input: &[u32]) -> u32 {
input.iter().map(|mass| mass / 3 - 2).sum()
}
/// Calculate the fuel requirements taking into account that fuel needs more fuel to lift it.
/// Mass of 8 or below results in zero or negative fuel so we can stop.
pub fn part2(input: &[u32]) -> u32 {
input
.iter()
.copied()
.map(|mut mass| {
let mut fuel = 0;
while mass > 8 {
mass = mass / 3 - 2;
fuel += mass;
}
fuel
})
.sum()
}