-
Notifications
You must be signed in to change notification settings - Fork 10
/
day02.rs
34 lines (30 loc) · 907 Bytes
/
day02.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
//! # I Was Told There Would Be No Math
//!
//! To extract the numbers when parsing the input we use our utility [`iter_unsigned`] and [`chunk`]
//! functions.
//!
//! Sorting the dimensions in ascending order makes calculating the smallest side or smallest
//! perimeter straightforward.
//!
//! [`iter_unsigned`]: crate::util::parse
//! [`chunk`]: crate::util::iter
use crate::util::iter::*;
use crate::util::parse::*;
type Gift = [u32; 3];
pub fn parse(input: &str) -> Vec<Gift> {
input
.iter_unsigned()
.chunk::<3>()
.map(|chunk| {
let mut gift = chunk;
gift.sort_unstable();
gift
})
.collect()
}
pub fn part1(input: &[Gift]) -> u32 {
input.iter().map(|[l, w, h]| 2 * (l * w + w * h + h * l) + l * w).sum()
}
pub fn part2(input: &[Gift]) -> u32 {
input.iter().map(|[l, w, h]| 2 * (l + w) + (l * w * h)).sum()
}