-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
120 lines (106 loc) · 3.16 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::iter::repeat_n;
use common::Answer;
use itertools::Itertools;
type IntType = i64;
#[derive(Debug, Clone, Copy)]
struct Ingredient {
capacity: IntType,
durability: IntType,
flavor: IntType,
texture: IntType,
calories: IntType,
}
impl Ingredient {
fn mul(&self, amount: IntType) -> (IntType, IntType, IntType, IntType, IntType) {
(
self.capacity * amount,
self.durability * amount,
self.flavor * amount,
self.texture * amount,
self.calories * amount,
)
}
}
fn parse(s: &str) -> impl Iterator<Item = Ingredient> + '_ {
s.lines().map(|l| {
let mut parts = l.split_whitespace();
let capacity = parts
.nth(2)
.and_then(|s| s.trim_matches(',').parse().ok())
.unwrap();
let durability = parts
.nth(1)
.and_then(|s| s.trim_matches(',').parse().ok())
.unwrap();
let flavor = parts
.nth(1)
.and_then(|s| s.trim_matches(',').parse().ok())
.unwrap();
let texture = parts
.nth(1)
.and_then(|s| s.trim_matches(',').parse().ok())
.unwrap();
let calories = parts.nth(1).and_then(|s| s.parse().ok()).unwrap();
Ingredient {
capacity,
durability,
flavor,
texture,
calories,
}
})
}
fn find_recipes(ingredients: &[Ingredient]) -> impl Iterator<Item = (IntType, IntType)> + '_ {
repeat_n(0..=100, ingredients.len())
.multi_cartesian_product()
.filter(|v| v.iter().sum::<IntType>() == 100)
.map(|v| {
v.iter()
.zip(ingredients.iter())
.map(|(&amount, ingredient)| ingredient.mul(amount))
.fold(
(0, 0, 0, 0, 0),
|(ac, ad, af, at, acal), (c, d, f, t, cal)| {
(ac + c, ad + d, af + f, at + t, acal + cal)
},
)
})
.map(|(c, d, f, t, cal)| {
let c = if c < 0 { 0 } else { c };
let d = if d < 0 { 0 } else { d };
let f = if f < 0 { 0 } else { f };
let t = if t < 0 { 0 } else { t };
(c * d * f * t, cal)
})
}
pub fn step1(s: &str) -> Answer {
let ingredients = parse(s).collect::<Vec<_>>();
find_recipes(&ingredients)
.map(|(i, _)| i)
.max()
.unwrap()
.into()
}
pub fn step2(s: &str) -> Answer {
let ingredients = parse(s).collect::<Vec<_>>();
find_recipes(&ingredients)
.filter(|&(_, cal)| cal == 500)
.map(|(i, _)| i)
.max()
.unwrap()
.into()
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = r#"Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8
Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3"#;
#[test]
fn step1_finds_correct_answer() {
assert_eq!(step1(INPUT), Answer::Signed(62842880));
}
#[test]
fn step2_finds_correct_answer() {
assert_eq!(step2(INPUT), Answer::Signed(57600000));
}
}