-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.rs
52 lines (45 loc) · 1.11 KB
/
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
#![feature(test)]
use aoc::trie::Trie;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
#[derive(Debug)]
struct Input {
patterns: Trie<u8>,
designs: Vec<Vec<u8>>,
}
fn setup(input: &str) -> Input {
let mut lines = input.trim().lines();
let patterns = lines
.next()
.unwrap()
.split(", ")
.map(|p| p.bytes())
.collect();
let designs = lines.skip(1).map(Into::into).collect();
Input { patterns, designs }
}
fn count(design: &[u8], patterns: &Trie<u8>) -> usize {
let mut dp = vec![0; design.len() + 1];
dp[design.len()] = 1;
for i in (0..design.len()).rev() {
dp[i] = patterns
.prefix_matches(design[i..].iter().copied())
.map(|len| dp[i + len])
.sum();
}
dp[0]
}
fn part1(input: &Input) -> usize {
input
.designs
.par_iter()
.map(|d| count(d, &input.patterns).min(1))
.sum()
}
fn part2(input: &Input) -> usize {
input
.designs
.par_iter()
.map(|d| count(d, &input.patterns))
.sum()
}
aoc::main!(2024, 19, ex: 1);