diff --git a/src/bin/03.rs b/src/bin/03.rs new file mode 100644 index 0000000000..a9f7149 --- /dev/null +++ b/src/bin/03.rs @@ -0,0 +1,62 @@ +use itertools::Itertools; + +pub fn part_one(input: &str) -> Option { + Some( + input + .lines() + .map(|x| { + x.split_whitespace() + .map(|x| x.parse::().unwrap()) + .collect_vec() + }) + .map(|x| [x[0], x[1], x[2]]) + .filter(|&[a, b, c]| a + b > c && a + c > b && c + b > a) + .count(), + ) +} +pub fn part_two(input: &str) -> Option { + let mut triangles = Vec::new(); + + for chunk in input + .lines() + .map(|x| { + x.split_whitespace() + .map(|x| x.parse::().unwrap()) + .collect_vec() + }) + .chunks(3) + .into_iter() + { + let (l1, l2, l3) = chunk.collect_tuple().unwrap(); + + for i in 0..=2 { + triangles.push([l1[i], l2[i], l3[i]]); + } + } + + Some( + triangles + .into_iter() + .filter(|&[a, b, c]| a + b > c && a + c > b && c + b > a) + .count(), + ) +} +fn main() { + let input = &aoc::read_file("inputs", 3); + aoc::solve!(1, part_one, input); + aoc::solve!(2, part_two, input); +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_part_one() { + let input = aoc::read_file("examples", 3); + assert_eq!(part_one(&input), Some(2)); + } + #[test] + fn test_part_two() { + let input = aoc::read_file("examples", 3); + assert_eq!(part_two(&input), Some(1)); + } +} diff --git a/src/examples/03.txt b/src/examples/03.txt new file mode 100644 index 0000000000..32cd3d2 --- /dev/null +++ b/src/examples/03.txt @@ -0,0 +1,3 @@ +5 10 25 +2 2 3 +6 13 15