aoc/year2016/
day03.rs

1//! # Squares With Three Sides
2//!
3//! We rely on the [`iter`] and [`parse`] utility modules to extract integers from surrounding
4//! text then group together in chunks of three.
5//!
6//! [`iter`]: crate::util::iter
7//! [`parse`]: crate::util::parse
8use crate::util::integer::*;
9use crate::util::iter::*;
10use crate::util::parse::*;
11
12pub fn parse(input: &str) -> Vec<u32> {
13    input.iter_unsigned().collect()
14}
15
16pub fn part1(input: &[u32]) -> usize {
17    count(input.iter().copied())
18}
19
20pub fn part2(input: &[u32]) -> usize {
21    (0..3).map(|skip| count(input.iter().copied().skip(skip).step_by(3))).sum()
22}
23
24fn count(iter: impl Iterator<Item = u32>) -> usize {
25    iter.chunk::<3>()
26        .filter(|&[a, b, c]| {
27            // It is faster to manually sort out the largest element and do one compare
28            let (a, b) = a.minmax(b);
29            let (b, c) = b.minmax(c);
30            a + b > c
31        })
32        .count()
33}