From 7aa9f359c83e545f3c09b6a2fb7af783689b3574 Mon Sep 17 00:00:00 2001 From: Johannes Date: Sun, 14 Jan 2024 11:25:36 +0100 Subject: [PATCH] Day 15 part 2. --- src/day15.rs | 55 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/src/day15.rs b/src/day15.rs index 4a37c12..6517d9d 100644 --- a/src/day15.rs +++ b/src/day15.rs @@ -58,10 +58,42 @@ fn part1(input: &[Ingredient]) -> i32 { scores.into_iter().max().unwrap() } -// #[aoc(day15, part2)] -// fn part2(input: &[Ingredient]) -> String { -// todo!() -// } +#[aoc(day15, part2)] +fn part2(input: &[Ingredient]) -> i32 { + let mut scores = HashSet::new(); + for a in 0i32..=100 { + for b in 0i32..=(100 - a) { + for c in 0i32..=(100 - a - b) { + for d in 0i32..=(100 - a - b - c) { + if a + b + c + d != 100 { + continue; + } + if a * input[0].calories + b * input[1].calories + c * input[2].calories + d * input[3].calories != 500 { + continue; + } + let mut capacity = a * input[0].capacity + b * input[1].capacity + c * input[2].capacity + d * input[3].capacity; + let mut durability = a * input[0].durability + b * input[1].durability + c * input[2].durability + d * input[3].durability; + let mut flavor = a * input[0].flavor + b * input[1].flavor + c * input[2].flavor + d * input[3].flavor; + let mut texture = a * input[0].texture + b * input[1].texture + c * input[2].texture + d * input[3].texture; + if capacity < 0 { + capacity = 0; + } + if durability < 0 { + durability = 0; + } + if flavor < 0 { + flavor = 0; + } + if texture < 0 { + texture = 0; + } + scores.insert(capacity * durability * flavor * texture); + } + } + } + } + scores.into_iter().max().unwrap() +} struct Ingredient { capacity: i32, @@ -70,18 +102,3 @@ struct Ingredient { texture: i32, calories: i32, } -// -// #[cfg(test)] -// mod tests { -// use super::*; -// -// #[test] -// fn part1_example() { -// assert_eq!(part1(&parse("")), ""); -// } -// -// #[test] -// fn part2_example() { -// assert_eq!(part2(&parse("")), ""); -// } -// } \ No newline at end of file