add day 15

This commit is contained in:
timeshifter
2026-06-23 21:48:01 +02:00
parent 95060994cd
commit f4b2338313
4 changed files with 168 additions and 0 deletions
+7
View File
@@ -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"
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "day15"
version = "0.1.0"
edition = "2024"
[dependencies]
+4
View File
@@ -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
+151
View File
@@ -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::<String>()
.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