add day 15
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user