-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_19.rs
102 lines (84 loc) · 2.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
use std::collections::HashMap;
use common::{solution, Answer};
solution!("Linen Layout", 19);
fn part_a(input: &str) -> Answer {
let problem = Onsen::parse(input);
problem.possible().into()
}
fn part_b(input: &str) -> Answer {
let problem = Onsen::parse(input);
problem.ways().into()
}
struct Onsen<'a> {
segments: Vec<&'a str>,
towels: Vec<&'a str>,
}
impl<'a> Onsen<'a> {
fn parse(input: &'a str) -> Self {
let (sources, needed) = input.split_once("\n\n").unwrap();
let segments = sources.split(", ").collect();
let towels = needed.lines().collect();
Self { segments, towels }
}
/// Returns the number of possible towel designs by counting all the towels
/// that can be made a non-zero number of ways.
fn possible(&self) -> usize {
self.towels
.iter()
.filter(|x| count_ways(&mut HashMap::new(), x, &self.segments) != 0)
.count()
}
/// Here we just sum up the number of ways each towel can be made.
fn ways(&self) -> u64 {
self.towels
.iter()
.map(|x| count_ways(&mut HashMap::new(), x, &self.segments))
.sum()
}
}
fn count_ways<'a>(memo: &mut HashMap<&'a str, u64>, expected: &'a str, sources: &[&'a str]) -> u64 {
if let Some(&cache) = memo.get(expected) {
return cache;
}
// If there is no more towel to find designs for, we have found one way to
// make the towel.
if expected.is_empty() {
return 1;
}
// Otherwise, we will sum up the number of ways the towel can be made from
// adding each of the available segments to the current towel, but only the
// ones that match the current pattern.
let mut ways = 0;
for source in sources {
if expected.len() >= source.len() && expected.starts_with(source) {
ways += count_ways(memo, &expected[source.len()..], sources);
}
}
// Memoization!!! This is what allows us to avoid evaluating huge segments
// of the tree and get good performance.
memo.insert(expected, ways);
ways
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
r, wr, b, g, bwu, rb, gb, br
brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 6.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 16.into());
}
}