From f4b23383135d02b108832ace26c19e666ba6fa89 Mon Sep 17 00:00:00 2001 From: timeshifter Date: Tue, 23 Jun 2026 21:48:01 +0200 Subject: [PATCH] add day 15 --- day15/Cargo.lock | 7 +++ day15/Cargo.toml | 6 ++ day15/input.txt | 4 ++ day15/src/main.rs | 151 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 day15/Cargo.lock create mode 100644 day15/Cargo.toml create mode 100644 day15/input.txt create mode 100644 day15/src/main.rs diff --git a/day15/Cargo.lock b/day15/Cargo.lock new file mode 100644 index 0000000..a6ac8d2 --- /dev/null +++ b/day15/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day15" +version = "0.1.0" diff --git a/day15/Cargo.toml b/day15/Cargo.toml new file mode 100644 index 0000000..c787d54 --- /dev/null +++ b/day15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day15" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/day15/input.txt b/day15/input.txt new file mode 100644 index 0000000..6f33d01 --- /dev/null +++ b/day15/input.txt @@ -0,0 +1,4 @@ +Sprinkles: capacity 2, durability 0, flavor -2, texture 0, calories 3 +Butterscotch: capacity 0, durability 5, flavor -3, texture 0, calories 3 +Chocolate: capacity 0, durability 0, flavor 5, texture -1, calories 8 +Candy: capacity 0, durability -1, flavor 0, texture 5, calories 8 diff --git a/day15/src/main.rs b/day15/src/main.rs new file mode 100644 index 0000000..6930e8d --- /dev/null +++ b/day15/src/main.rs @@ -0,0 +1,151 @@ +fn main() { + let data: Vec<_> = std::fs::read_to_string("input.txt") + .unwrap() + .lines() + .map(Ingredient::parse) + .collect(); + + part1(&data); + part2(&data); +} + +fn part2(data: &[Ingredient]) { + let mut best = 0; + for a in 0..=100 { + for b in 0..=(100 - a) { + for c in 0..=(100 - b) { + 'middle: for d in 0..=(100 - c) { + if a + b + c + d != 100 { + continue; + } + let cap: i64 = [a, b, c, d] + .into_iter() + .zip(data.iter().map(|i| i.capacity)) + .map(|(l, r)| l * r) + .sum(); + let dur: i64 = [a, b, c, d] + .into_iter() + .zip(data.iter().map(|i| i.durability)) + .map(|(l, r)| l * r) + .sum(); + let flavor: i64 = [a, b, c, d] + .into_iter() + .zip(data.iter().map(|i| i.flavor)) + .map(|(l, r)| l * r) + .sum(); + let texture: i64 = [a, b, c, d] + .into_iter() + .zip(data.iter().map(|i| i.texture)) + .map(|(l, r)| l * r) + .sum(); + let cal: i64 = [a, b, c, d] + .into_iter() + .zip(data.iter().map(|i| i.calories)) + .map(|(l, r)| l * r) + .sum(); + + for item in [cap, dur, flavor, texture, cal] { + if item <= 0 { + continue 'middle; + } + } + if cal != 500 { + continue; + } + + let product = cap * dur * flavor * texture; + if product > best { + best = product; + } + } + } + } + } + println!("{best}") +} + +fn part1(data: &[Ingredient]) { + let mut best = 0; + for a in 0..=100 { + for b in 0..=(100 - a) { + for c in 0..=(100 - b) { + 'middle: for d in 0..=(100 - c) { + if a + b + c + d != 100 { + continue; + } + let cap: i64 = [a, b, c, d] + .into_iter() + .zip(data.iter().map(|i| i.capacity)) + .map(|(l, r)| l * r) + .sum(); + let dur: i64 = [a, b, c, d] + .into_iter() + .zip(data.iter().map(|i| i.durability)) + .map(|(l, r)| l * r) + .sum(); + let flavor: i64 = [a, b, c, d] + .into_iter() + .zip(data.iter().map(|i| i.flavor)) + .map(|(l, r)| l * r) + .sum(); + let texture: i64 = [a, b, c, d] + .into_iter() + .zip(data.iter().map(|i| i.texture)) + .map(|(l, r)| l * r) + .sum(); + + for item in [cap, dur, flavor, texture] { + if item <= 0 { + continue 'middle; + } + } + + let product = cap * dur * flavor * texture; + if product > best { + best = product; + } + } + } + } + } + println!("{best}") +} + +#[derive(Debug)] +struct Ingredient { + pub capacity: i64, + pub durability: i64, + pub flavor: i64, + pub texture: i64, + pub calories: i64, +} + +fn helper(s: &str) -> i64 { + s.chars() + .take_while(|c| *c != ',') + .collect::() + .parse() + .unwrap() +} + +impl Ingredient { + fn parse(s: &str) -> Self { + let mut iter = s.split_whitespace(); + + let capacity = helper(iter.nth(2).unwrap()); + let durability = helper(iter.nth(1).unwrap()); + let flavor = helper(iter.nth(1).unwrap()); + let texture = helper(iter.nth(1).unwrap()); + let calories = helper(iter.nth(1).unwrap()); + + Self { + capacity, + durability, + flavor, + texture, + calories, + } + } +} + +// https://adventofcode.com/2015/day/15