-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
54 lines (46 loc) · 1.6 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> u32 {
data.lines().map(get_dims).map(required_paper).sum()
}
fn part_two(data: &str) -> u32 {
data.lines().map(get_dims).map(required_ribbon).sum()
}
fn required_ribbon(dimensions: (u32, u32, u32)) -> u32 {
smallest_perimeter(dimensions) + cubic_volume(dimensions)
}
fn smallest_perimeter(dimensions: (u32, u32, u32)) -> u32 {
let (length, width, height) = dimensions;
let side_1 = 2 * (length + width);
let side_2 = 2 * (width + height);
let side_3 = 2 * (height + length);
let min = std::cmp::min(side_1, side_2);
std::cmp::min(min, side_3)
}
const fn cubic_volume(dimensions: (u32, u32, u32)) -> u32 {
let (length, width, height) = dimensions;
length * width * height
}
fn required_paper(dimensions: (u32, u32, u32)) -> u32 {
let (length, width, height) = dimensions;
let side_1 = length * width;
let side_2 = width * height;
let side_3 = height * length;
let mut min = std::cmp::min(side_1, side_2);
min = std::cmp::min(min, side_3);
2 * side_1 + 2 * side_2 + 2 * side_3 + min
}
fn get_dims(line: &str) -> (u32, u32, u32) {
let mut parts = line.split('x');
let (Some(x), Some(y), Some(z), None) = (parts.next(), parts.next(), parts.next(), parts.next()) else {
unreachable!();
};
let x = x.parse().unwrap();
let y = y.parse().unwrap();
let z = z.parse().unwrap();
(x, y, z)
}