From e063377590c4c2d19c5b5ab474ec7cf44e05ae3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20K=C5=82ak?= Date: Wed, 4 Dec 2019 20:56:23 +0100 Subject: [PATCH] Day 15 --- day15/Cargo.toml | 11 +++++ day15/input | 4 ++ day15/src/main.rs | 103 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 day15/Cargo.toml create mode 100644 day15/input create mode 100644 day15/src/main.rs diff --git a/day15/Cargo.toml b/day15/Cargo.toml new file mode 100644 index 0000000..8bfedcd --- /dev/null +++ b/day15/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day15" +version = "0.1.0" +authors = ["Tomasz Kłak "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +regex = "1" +lazy_static = "1.4" diff --git a/day15/input b/day15/input new file mode 100644 index 0000000..634c6a5 --- /dev/null +++ b/day15/input @@ -0,0 +1,4 @@ +Frosting: capacity 4, durability -2, flavor 0, texture 0, calories 5 +Candy: capacity 0, durability 5, flavor -1, texture 0, calories 8 +Butterscotch: capacity -1, durability 0, flavor 5, texture 0, calories 6 +Sugar: capacity 0, durability 0, flavor -2, texture 2, calories 1 diff --git a/day15/src/main.rs b/day15/src/main.rs new file mode 100644 index 0000000..334273f --- /dev/null +++ b/day15/src/main.rs @@ -0,0 +1,103 @@ +#[macro_use] +extern crate lazy_static; +use regex::Regex; +use std::io::{BufRead,BufReader}; +use std::collections::HashMap; + +lazy_static! { + static ref RE : Regex = + Regex::new(r"(.+): capacity (.+), durability (.+), flavor (.+), texture (.+), calories (.+)").unwrap(); +} + +#[derive(Debug)] +struct Ingredient { + cap: i64, + dur: i64, + flav: i64, + tex: i64, + cal: i64 +} + +fn parse(s: &str) -> (String, Ingredient) { + let c = RE.captures(s).unwrap(); + (c[1].to_owned(), Ingredient{ + cap: c[2].parse().unwrap(), + dur: c[3].parse().unwrap(), + flav: c[4].parse().unwrap(), + tex: c[5].parse().unwrap(), + cal: c[6].parse().unwrap(), + }) +} + +fn trunc(v: i64) -> i64 { + if v < 0 { 0 } else { v } +} + +fn sum(v: Vec<(i64, &(String, Ingredient))>) -> i64 { + let mut cap = 0; + let mut dur = 0; + let mut flav = 0; + let mut tex = 0; + for (n, (_, i)) in v.iter() { + cap += n * i.cap; + dur += n * i.dur; + flav += n * i.flav; + tex += n * i.tex; + } + cap = trunc(cap); + dur = trunc(dur); + flav = trunc(flav); + tex = trunc(tex); + cap * dur * flav * tex +} + +fn sum2(v: Vec<(i64, &(String, Ingredient))>) -> i64 { + let mut cap = 0; + let mut dur = 0; + let mut flav = 0; + let mut tex = 0; + let mut cal = 0; + for (n, (_, i)) in v.iter() { + cap += n * i.cap; + dur += n * i.dur; + flav += n * i.flav; + tex += n * i.tex; + cal += n * i.cal; + } + if cal != 500 { + return 0; + } + cap = trunc(cap); + dur = trunc(dur); + flav = trunc(flav); + tex = trunc(tex); + cap * dur * flav * tex +} + +fn main() { + let i : Vec<(_, _)> = BufReader::new(std::io::stdin()) + .lines() + .map(|r| parse(&r.unwrap())) + .collect(); + assert_eq!(4, i.len()); + let mut max = 0; + let mut max_with_cap = 0; + for a in 0..=100 { + for b in 0..=(100-a) { + for c in 0..=(100-(a+b)) { + let d = 100 - (a+b+c); + assert_eq!(100,a+b+c+d); + let s = sum(vec![(a, &i[0]), (b, &i[1]), (c, &i[2]), (d, &i[3])]); + if s > max { + max = s; + } + let s = sum2(vec![(a, &i[0]), (b, &i[1]), (c, &i[2]), (d, &i[3])]); + if s > max_with_cap { + max_with_cap = s; + } + } + } + } + println!("{}", max); + println!("{}", max_with_cap); +} -- 2.45.2