-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.rs
182 lines (157 loc) · 4.9 KB
/
day12.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
use ahash::{HashMap, HashMapExt};
use aoc_runner_derive::{aoc, aoc_generator};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use crate::common::utils::parse_split;
#[derive(PartialEq, Eq, Clone)]
pub enum State {
Unknown,
Damaged,
Operational,
}
impl std::fmt::Debug for State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Unknown => write!(f, "?"),
Self::Damaged => write!(f, "#"),
Self::Operational => write!(f, "."),
}
}
}
fn parse_spring(s: &str) -> Vec<State> {
s.bytes()
.map(|b| match b {
b'.' => State::Operational,
b'#' => State::Damaged,
b'?' => State::Unknown,
_ => panic!("unknown symbol"),
})
.collect()
}
#[derive(Debug)]
pub struct Line {
spring: Vec<State>,
block: Vec<usize>,
}
impl Line {
fn solve(&self, dp: &mut HashMap<(usize, usize, usize), usize>) -> usize {
solve(&self.spring, &self.block, 0, dp)
}
fn expand(&self) -> Self {
let mut spring = Vec::with_capacity((self.spring.len() + 1) * 5);
let mut block = Vec::with_capacity(self.block.len() * 5);
for _ in 0..4 {
spring.extend_from_slice(&self.spring);
spring.push(State::Unknown);
block.extend_from_slice(&self.block);
}
spring.extend_from_slice(&self.spring);
block.extend_from_slice(&self.block);
Self { spring, block }
}
}
fn solve(
spring: &[State],
block: &[usize],
stride: usize,
dp: &mut HashMap<(usize, usize, usize), usize>,
) -> usize {
if let Some(v) = dp.get(&(spring.len(), block.len(), stride)) {
return *v;
}
let mut res = 0;
if let Some(piece) = spring.first() {
// Try doing the count if the variant is Operational and Damaged
// If the piece if Unknown we do both
// If the piece is Operational or Damaged, then we do it once when the variant matches
// Damaged : increase stride
// Operational && empty stride : continue processing
// Operational && stride == first block: next block, and reset stride
// Operational && anything else : Invalid, return 0
// Unknown : can't happen (handled by the variant logic)
for variant in [State::Operational, State::Damaged] {
if piece == &variant || piece == &State::Unknown {
res += match variant {
State::Damaged => solve(&spring[1..], block, stride + 1, dp),
State::Operational if stride == 0 => solve(&spring[1..], block, 0, dp),
State::Operational if block.first() == Some(&stride) => {
solve(&spring[1..], &block[1..], 0, dp)
}
State::Operational => 0,
State::Unknown => unreachable!(),
}
}
}
} else {
// if block is empty and stride is empty OR
// last block is the same as the stride
if block.is_empty() && stride == 0 || block.len() == 1 && stride == block[0] {
return 1;
} else {
return 0;
}
}
dp.insert((spring.len(), block.len(), stride), res);
res
}
#[aoc_generator(day12)]
pub fn generator(input: &str) -> Vec<Line> {
input
.lines()
.map(|line| {
let (spring, blockition) = line.split_once(' ').unwrap();
Line {
spring: parse_spring(spring),
block: parse_split(blockition, ','),
}
})
.collect()
}
#[aoc(day12, part1)]
pub fn part1(inputs: &[Line]) -> usize {
inputs
.par_iter()
.map(|l| l.solve(&mut HashMap::new()))
.sum()
}
#[aoc(day12, part2)]
pub fn part2(inputs: &[Line]) -> usize {
inputs
.par_iter()
.map(|l| l.expand().solve(&mut HashMap::new()))
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &str = "???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1
";
#[test]
pub fn input_test() {
// assert_eq!(generator(SAMPLE), Object());
}
#[test]
pub fn part1_test() {
assert_eq!(part1(&generator(SAMPLE)), 21);
}
#[test]
pub fn part2_test() {
assert_eq!(part2(&generator(SAMPLE)), 525152);
}
mod regression {
use super::*;
const INPUT: &str = include_str!("../input/2023/day12.txt");
const ANSWERS: (usize, usize) = (7286, 25470469710341);
#[test]
pub fn test() {
let input = INPUT.trim_end_matches('\n');
let output = generator(input);
assert_eq!(part1(&output), ANSWERS.0);
assert_eq!(part2(&output), ANSWERS.1);
}
}
}