From 00000320056add7c3c54d7febb601599d0da0246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Fri, 9 Dec 2022 19:26:13 +0100 Subject: [PATCH] solution: day17 --- src/bin/17.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/bin/17.rs diff --git a/src/bin/17.rs b/src/bin/17.rs new file mode 100644 index 000000000..3d8a1b5 --- /dev/null +++ b/src/bin/17.rs @@ -0,0 +1,50 @@ +use aoc::helpers::to_vec; +use itertools::Itertools; + +pub fn part_one(input: &str) -> Option { + let containers = to_vec::(input, '\n'); + Some( + (0..containers.len()) + .map(|x| { + containers + .iter() + .combinations(x) + .map(|y| y.iter().map(|w| *w).sum::()) + .filter(|&z| z == 150) + .count() + }) + .sum::() as u32, + ) +} +pub fn part_two(input: &str) -> Option { + let containers = to_vec::(input, '\n'); + let v = (1..=containers.len()) + .map(|x| { + containers + .iter() + .combinations(x) + .map(|y| (y.len(), y.iter().map(|w| *w).sum::())) + .filter(|(_, z)| z == &150) + .map(|(t, _)| t) + .collect_vec() + }) + .filter(|x| x.len() > 0) + .map(|x| { + let m = x.iter().min().unwrap(); + (*m, x.iter().filter(|&y| y == m).count()) + }) + .collect_vec(); + let m = v.iter().map(|(a, _)| a).min().unwrap(); + Some( + v.iter() + .filter(|(a, _)| a == m) + .map(|(_, b)| *b) + .sum::() as u32, + ) +} + +fn main() { + let input = &aoc::read_file("inputs", 17); + aoc::solve!(1, part_one, input); + aoc::solve!(2, part_two, input); +}