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::iter::*;
9use crate::util::parse::*;
10
11pub fn parse(input: &str) -> Vec<u32> {
12    input.iter_unsigned().collect()
13}
14
15pub fn part1(input: &[u32]) -> usize {
16    count(input.iter().copied())
17}
18
19pub fn part2(input: &[u32]) -> usize {
20    let first = count(input.iter().copied().step_by(3));
21    let second = count(input.iter().copied().skip(1).step_by(3));
22    let third = count(input.iter().copied().skip(2).step_by(3));
23    first + second + third
24}
25
26fn count(iter: impl Iterator<Item = u32>) -> usize {
27    iter.chunk::<3>().filter(|&[a, b, c]| a + b > c && a + c > b && b + c > a).count()
28}