Day 15 part 1.

This commit is contained in:
2024-01-14 11:23:01 +01:00
parent 2d3c49c491
commit 63552704fd
2 changed files with 88 additions and 0 deletions

87
src/day15.rs Normal file
View File

@@ -0,0 +1,87 @@
use std::collections::HashSet;
use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day15)]
fn parse(input: &str) -> Vec<Ingredient> {
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("<EXAMPLE>")), "<RESULT>");
// }
//
// #[test]
// fn part2_example() {
// assert_eq!(part2(&parse("<EXAMPLE>")), "<RESULT>");
// }
// }

View File

@@ -7,5 +7,6 @@ mod day11;
mod day12; mod day12;
mod day13; mod day13;
mod day14; mod day14;
mod day15;
aoc_lib! { year = 2015 } aoc_lib! { year = 2015 }