-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
50 lines (45 loc) · 1.07 KB
/
lib.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
42
43
44
45
46
47
48
49
50
pub fn part1(input: Option<&str>) -> String {
input
.unwrap_or(include_str!("../input.txt"))
.lines()
.map(|num| (num.parse::<usize>().unwrap() / 3) - 2)
.sum::<usize>()
.to_string()
}
pub fn part2(input: Option<&str>) -> String {
input
.unwrap_or(include_str!("../input.txt"))
.lines()
.map(|num| {
let mut fuel = 0;
let mut mass = num.parse::<isize>().unwrap();
loop {
mass = (mass / 3) - 2;
if mass <= 0 {
break;
}
fuel += mass;
}
fuel
})
.sum::<isize>()
.to_string()
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn part_1_works() {
assert_eq!(
part1(Some(include_str!("../example01.txt"))),
"34241".to_string()
);
}
#[test]
fn part_2_works() {
assert_eq!(
part2(Some(include_str!("../example01.txt"))),
"51316".to_string()
);
}
}