diff --git a/src/year2016/day03.rs b/src/year2016/day03.rs index 6cb45dc..39d2768 100644 --- a/src/year2016/day03.rs +++ b/src/year2016/day03.rs @@ -1,13 +1,47 @@ use crate::util::parse::ParseExt; -pub fn part1(input: &str) -> impl std::fmt::Display { - //let vec: Vec> = input.lines().map(|s| s.u32s().collect()); +fn valid_triangle(v: &[u32; 3]) -> bool { + v[0] + v[1] > v[2] && v[1] + v[2] > v[0] && v[0] + v[2] > v[1] +} - "" +pub fn part1(input: &str) -> impl std::fmt::Display { + input + .lines() + .map(|s| { + s.u32s() + .fold(([0u32; 3], 0), |(mut a, i), v| { + a[i] = v; + (a, i + 1) + }) + .0 + }) + .filter(valid_triangle) + .count() } pub fn part2(input: &str) -> impl std::fmt::Display { - "" + fn get3<'a>(lines: &'a mut std::str::Lines<'_>) -> Option<(&'a str, &'a str, &'a str)> { + Some((lines.next()?, lines.next()?, lines.next()?)) + } + + let mut lines = input.lines(); + let mut valid = 0; + + while let Some((l1, l2, l3)) = get3(&mut lines) { + let l1: Vec = l1.u32s().collect(); + let l2: Vec = l2.u32s().collect(); + let l3: Vec = l3.u32s().collect(); + if valid_triangle(&[l1[0], l2[0], l3[0]]) { + valid += 1; + } + if valid_triangle(&[l1[1], l2[1], l3[1]]) { + valid += 1; + } + if valid_triangle(&[l1[2], l2[2], l3[2]]) { + valid += 1; + } + } + valid } #[test]