aoc/year2015/
day02.rs

1//! # I Was Told There Would Be No Math
2//!
3//! To extract the numbers when parsing the input we use our utility [`iter_unsigned`] and [`chunk`]
4//! functions.
5//!
6//! Sorting the dimensions in ascending order makes calculating the smallest side or smallest
7//! perimeter straightforward.
8//!
9//! [`iter_unsigned`]: crate::util::parse
10//! [`chunk`]: crate::util::iter
11use crate::util::iter::*;
12use crate::util::parse::*;
13
14type Input = Vec<[u32; 3]>;
15
16pub fn parse(input: &str) -> Input {
17    input
18        .iter_unsigned()
19        .chunk::<3>()
20        .map(|mut chunk| {
21            chunk.sort_unstable();
22            chunk
23        })
24        .collect()
25}
26
27pub fn part1(input: &Input) -> u32 {
28    input.iter().map(|[l, w, h]| 2 * (l * w + w * h + h * l) + l * w).sum()
29}
30
31pub fn part2(input: &Input) -> u32 {
32    input.iter().map(|[l, w, h]| 2 * (l + w) + (l * w * h)).sum()
33}