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 Gift = [u32; 3];
15
16pub fn parse(input: &str) -> Vec<Gift> {
17    input
18        .iter_unsigned()
19        .chunk::<3>()
20        .map(|chunk| {
21            let mut gift = chunk;
22            gift.sort_unstable();
23            gift
24        })
25        .collect()
26}
27
28pub fn part1(input: &[Gift]) -> u32 {
29    input.iter().map(|[l, w, h]| 2 * (l * w + w * h + h * l) + l * w).sum()
30}
31
32pub fn part2(input: &[Gift]) -> u32 {
33    input.iter().map(|[l, w, h]| 2 * (l + w) + (l * w * h)).sum()
34}