From 63552704fd005fa338b66dd4572df2dec6325bf8 Mon Sep 17 00:00:00 2001 From: Johannes Date: Sun, 14 Jan 2024 11:23:01 +0100 Subject: [PATCH] Day 15 part 1. --- src/day15.rs | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 88 insertions(+) create mode 100644 src/day15.rs diff --git a/src/day15.rs b/src/day15.rs new file mode 100644 index 0000000..4a37c12 --- /dev/null +++ b/src/day15.rs @@ -0,0 +1,87 @@ +use std::collections::HashSet; + +use aoc_runner_derive::{aoc, aoc_generator}; + +#[aoc_generator(day15)] +fn parse(input: &str) -> Vec { + input.lines() + .map(|line| { + // Sprinkles: capacity 5, durability -1, flavor 0, texture 0, calories 5 + let parts: Vec<&str> = line.split(" ").map(|part| part.trim_end_matches(",")).collect(); + let capacity = parts[2].parse().unwrap(); + let durability = parts[4].parse().unwrap(); + let flavor = parts[6].parse().unwrap(); + let texture = parts[8].parse().unwrap(); + let calories = parts[10].parse().unwrap(); + Ingredient { + capacity, + durability, + flavor, + texture, + calories, + } + }) + .collect() +} + +#[aoc(day15, part1)] +fn part1(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; + } + 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() +} + +// #[aoc(day15, part2)] +// fn part2(input: &[Ingredient]) -> String { +// todo!() +// } + +struct Ingredient { + capacity: i32, + durability: i32, + flavor: i32, + 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 diff --git a/src/lib.rs b/src/lib.rs index 5118051..6367266 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,5 +7,6 @@ mod day11; mod day12; mod day13; mod day14; +mod day15; aoc_lib! { year = 2015 }