-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_19.rs
277 lines (239 loc) · 8.49 KB
/
day_19.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
///
/// # day_19.rs
/// Code for the day 19 of the Advent of Code challenge year 2024
///
// Imports ============================================================================== Imports
use std::{collections::HashMap, str::FromStr};
// Variables =========================================================================== Variables
const INPUT: &str = include_str!("../../data/inputs/day_19.txt");
struct TowelGenerator {
available_towels: Vec<String>,
desired_designs: Vec<String>,
}
impl FromStr for TowelGenerator {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parts = s.trim().split("\n\n");
let available_towels = parts
.next()
.unwrap()
.split(", ")
.map(|s| s.to_string())
.collect();
let desired_designs = parts
.next()
.unwrap()
.lines()
.map(|l| l.to_string())
.collect();
Ok(TowelGenerator {
available_towels,
desired_designs,
})
}
}
impl TowelGenerator {
///
/// # `is_design_possible`
/// Check if a given towel design is possible with the available towels
///
/// ## Arguments
/// * `design` - The design to check
///
/// ## Returns
/// * `bool` - Whether the design is possible
fn is_design_possible(&self, design: &str) -> bool {
self.can_make_pattern(design, 0)
}
///
/// # `can_make_pattern`
/// Check if a given pattern can be made with the available towels starting at a given position.
///
/// ## Algorithm
/// Recursively try to match the pattern starting at the current position with each available towel pattern.
/// If a pattern fits, recursively try to match the rest of the pattern.
///
/// ## Arguments
/// * `remaining` - The remaining pattern to match
/// * `start_pos` - The position to start matching from
///
/// ## Returns
/// * `bool` - Whether the pattern can be made
fn can_make_pattern(&self, remaining: &str, start_pos: usize) -> bool {
// Base case: if we've used up all characters, the pattern is possible
if start_pos >= remaining.len() {
return true;
}
// Try each available towel pattern at the current position
for pattern in &self.available_towels {
if remaining[start_pos..].starts_with(pattern) {
// If this pattern fits at the current position, recursively try to match the rest
if self.can_make_pattern(remaining, start_pos + pattern.len()) {
return true;
}
}
}
false
}
///
/// # `count_possible_designs`
/// Count the number of possible designs from the desired designs list.
///
/// ## Returns
/// * `usize` - The number of possible designs
fn count_possible_designs(&self) -> usize {
self.desired_designs
.iter()
.filter(|design| self.is_design_possible(design))
.count()
}
///
/// # `count_arrangements`
/// Count the number of possible arrangements for a given design.
///
/// ## Arguments
/// * `design` - The design to count arrangements for
///
/// ## Returns
/// * `usize` - The number of possible arrangements
fn count_arrangements(&self, design: &str) -> usize {
let mut already_computed = HashMap::new();
let max_pattern_len = self.available_towels.iter().map(|v| v.len()).max().unwrap();
self.find_arrangements(design, &mut already_computed, max_pattern_len)
}
///
/// # `find_arrangements`
/// Find the number of possible arrangements for a given pattern.
///
/// ## Algorithm
/// Simple recursive algorithm to find all possible arrangements of a pattern with a memoization hashmap.
///
/// ## Arguments
/// * `pattern` - The pattern to find arrangements for
/// * `memo` - A memoization hashmap to store already computed values
/// * `max_len` - The maximum length of a pattern
///
/// ## Returns
/// * `usize` - The number of possible arrangements
fn find_arrangements(
&self,
pattern: &str,
memo: &mut HashMap<String, usize>,
max_len: usize,
) -> usize {
let mut combinations = 0;
if memo.contains_key(pattern) {
return *memo.get(pattern).unwrap();
}
if pattern.is_empty() {
return 1;
}
for i in 1..=max_len.min(pattern.len()) {
if self.available_towels.contains(&&pattern[..i].into()) {
let subcount = self.find_arrangements(&pattern[i..], memo, max_len);
combinations += subcount;
}
}
memo.insert(pattern.into(), combinations);
combinations
}
///
/// `sum_all_arrangements`
/// Sum the number of possible arrangements for all possible designs.
///
/// ## Returns
/// * `usize` - The sum of possible arrangements
fn sum_all_arrangements(&self) -> usize {
let desired_designs = self
.desired_designs
.iter()
.filter(|design| self.is_design_possible(design));
desired_designs
.map(|design| self.count_arrangements(design))
.sum()
}
}
// Functions =========================================================================== Functions
pub fn response_part_1() {
println!("Day 19 - Part 1");
let start = std::time::Instant::now();
let generator: TowelGenerator = INPUT.parse().unwrap();
let count = generator.count_possible_designs();
let duration = start.elapsed();
println!("Count: {count}");
println!("Duration: {duration:?}");
}
pub fn response_part_2() {
println!("Day 19 - Part 2");
let start = std::time::Instant::now();
let generator: TowelGenerator = INPUT.parse().unwrap();
let count = generator.sum_all_arrangements();
let duration = start.elapsed();
println!("Count: {count}");
println!("Duration: {duration:?}");
}
fn main() {
response_part_1();
response_part_2();
}
// Tests ==================================================================================== Tests
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE: &str = "\
r, wr, b, g, bwu, rb, gb, br
brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb";
#[test]
fn test_from_str() {
let generator: TowelGenerator = EXAMPLE.parse().unwrap();
assert_eq!(
generator.available_towels,
vec!["r", "wr", "b", "g", "bwu", "rb", "gb", "br"]
);
assert_eq!(
generator.desired_designs,
vec!["brwrr", "bggr", "gbbr", "rrbgbr", "ubwu", "bwurrg", "brgr", "bbrgwb"]
);
}
#[test]
fn test_is_design_possible() {
let generator: TowelGenerator = EXAMPLE.parse().unwrap();
assert_eq!(generator.is_design_possible("brwrr"), true);
assert_eq!(generator.is_design_possible("bggr"), true);
assert_eq!(generator.is_design_possible("gbbr"), true);
assert_eq!(generator.is_design_possible("rrbgbr"), true);
assert_eq!(generator.is_design_possible("ubwu"), false);
assert_eq!(generator.is_design_possible("bwurrg"), true);
assert_eq!(generator.is_design_possible("brgr"), true);
assert_eq!(generator.is_design_possible("bbrgwb"), false);
}
#[test]
fn test_count_possible_designs() {
let generator: TowelGenerator = EXAMPLE.parse().unwrap();
assert_eq!(generator.count_possible_designs(), 6);
}
#[test]
fn test_count_arrangements() {
let generator: TowelGenerator = EXAMPLE.parse().unwrap();
assert_eq!(generator.count_arrangements("brwrr"), 2); // b,r,wr,r or br,wr,r
assert_eq!(generator.count_arrangements("bggr"), 1); // b,g,g,r
assert_eq!(generator.count_arrangements("gbbr"), 4); // g,b,b,r or g,b,br or gb,b,r or gb,br
assert_eq!(generator.count_arrangements("rrbgbr"), 6); // All 6 combinations
assert_eq!(generator.count_arrangements("ubwu"), 0); // Impossible
assert_eq!(generator.count_arrangements("bwurrg"), 1); // bwu,r,r,g
assert_eq!(generator.count_arrangements("brgr"), 2); // b,r,g,r or br,g,r
assert_eq!(generator.count_arrangements("bbrgwb"), 0); // Impossible
}
#[test]
fn test_sum_all_arrangements() {
let generator: TowelGenerator = EXAMPLE.parse().unwrap();
assert_eq!(generator.sum_all_arrangements(), 16);
}
}