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::integer::*;
12use crate::util::iter::*;
13use crate::util::parse::*;
14
15type Input = Vec<[u32; 3]>;
16
17pub fn parse(input: &str) -> Input {
18 input
19 .iter_unsigned::<u32>()
20 .chunk::<3>()
21 .map(|[a, b, c]| {
22 // We only care which element is largest; it is faster to partially sort ourselves
23 // than to use sort_unstable.
24 let (a, b) = a.minmax(b);
25 let (b, c) = b.minmax(c);
26 [a, b, c]
27 })
28 .collect()
29}
30
31pub fn part1(input: &Input) -> u32 {
32 input.iter().map(|&[l, w, h]| 2 * (l * w + w * h + h * l) + l * w).sum()
33}
34
35pub fn part2(input: &Input) -> u32 {
36 input.iter().map(|&[l, w, h]| 2 * (l + w) + l * w * h).sum()
37}