From 00000400a9a22a6e626a662df44c87ddb5a62881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Tue, 13 Dec 2022 23:22:59 +0100 Subject: [PATCH] solution: day24 --- src/bin/24.rs | 44 ++++++++++++++++++++++++++++++++++++++++++ src/test_inputs/24.txt | 10 ++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/bin/24.rs create mode 100644 src/test_inputs/24.txt diff --git a/src/bin/24.rs b/src/bin/24.rs new file mode 100644 index 000000000..b6cfbc7 --- /dev/null +++ b/src/bin/24.rs @@ -0,0 +1,44 @@ +use aoc::helpers::to_vec; +use itertools::Itertools; + +fn balance(gifts: &[u64], parts: u64) -> Option { + let part = gifts.iter().sum::() / parts; + + // greedily search for smallest + for i in 1..gifts.len() { + for comb in gifts.iter().combinations(i) { + if comb.iter().copied().sum::() == part { + return Some(comb.iter().copied().product()); + } + } + } + None +} + +pub fn part_one(input: &str) -> Option { + let gifts = to_vec(input, '\n'); + balance(&gifts, 3) +} +pub fn part_two(input: &str) -> Option { + let gifts = to_vec(input, '\n'); + balance(&gifts, 4) +} +fn main() { + let input = &aoc::read_file("inputs", 24); + 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("test_inputs", 24); + assert_eq!(part_one(&input.trim()), Some(99)); + } + #[test] + fn test_part_two() { + let input = aoc::read_file("test_inputs", 24); + assert_eq!(part_two(&input.trim()), Some(44)); + } +} diff --git a/src/test_inputs/24.txt b/src/test_inputs/24.txt new file mode 100644 index 000000000..f4909b4 --- /dev/null +++ b/src/test_inputs/24.txt @@ -0,0 +1,10 @@ +1 +2 +3 +4 +5 +7 +8 +9 +10 +11