aoc/year2023/day12.rs
1//! # Hot Springs
2//!
3//! A [dynamic programming](https://en.wikipedia.org/wiki/Dynamic_programming) approach calculates
4//! the possible arrangements for each entry in `O(n * w)` complexity where:
5//!
6//! * `n` Number of springs.
7//! * `w` "Wiggle" is the amount of extra free space the springs can slide around in the pattern.
8//!
9//! We build a table for each entry with a row for each spring and a column for every character
10//! in the pattern. Adding a trailing `.` character makes bounds checking easier without changing
11//! the number of arrangements. The result will be the bottom right value.
12//!
13//! Using the sample `?###???????? 3,2,1`:
14//!
15//! ```none
16//! n = 3
17//! w = 13 - (3 + 2 + 1) - 3 + 1 = 5
18//!
19//! ? # # # ? ? ? ? ? ? ? ? .
20//! ┌----------------------------------------
21//! 3 | 0 0 0 [0 1 1 1 1] 0 0 0 0 0
22//! 2 | 0 0 0 0 0 0 [0 1 2 3 4] 0 0
23//! 1 | 0 0 0 0 0 0 0 0 [0 1 3 6 10]
24//! ```
25//!
26//! Each pattern updates the total at the index one *after* its end, if it can fit at that location.
27//! For example, the first spring can only match at indices `[1, 2, 3]` so it updates the total
28//! at index 4 to 1.
29//!
30//! The key insight is that the number of arrangements is then propagated as a prefix sum
31//! from left to right for each row as long as the character at the index is not a `#` or until
32//! `wiggle` characters are reached, whichever comes sooner.
33//!
34//! To calculate the next row, each matching pattern adds the value from the row above at the
35//! index one before its start. The first row is a special case where this value is always 1.
36//!
37//! As a nice side effect this approach always overwrites values so we can reuse the memory buffer
38//! for different entries without having to zero out values.
39//!
40//! ## Alternate approach
41//!
42//! Another way to look at the problem is to search to the left from each matching position
43//! until a `#` character is found. The previous pattern can't leave any trailing `#` characters
44//! otherwise it wouldn't be the previous pattern.
45//!
46//! Using the same example `?###???????? 3,2,1` and adding a trailing `.`:
47//!
48//! * `###` can only match at one location giving:
49//! ```none
50//! . # # # . . . . . . . . .
51//! [0 0 0 0 1 0 0 0 0 0 0 0 0]
52//! ```
53//!
54//! * The next `##` can match at 4 possible locations:
55//! ```none
56//! . # # # . # # . . . . . .
57//! [0 0 0 0 1 0 0 0 0 0 0 0 0]
58//! <<
59//! [0 0 0 0 0 0 0 1 0 0 0 0 0]
60//! ```
61//! * 2nd location:
62//! ```none
63//! . # # # . . # # . . . . .
64//! [0 0 0 0 1 0 0 0 0 0 0 0 0]
65//! <<<<
66//! [0 0 0 0 0 0 0 1 1 0 0 0 0]
67//! ```
68//! * 3rd location:
69//! ```none
70//! . # # # . . . # # . . . .
71//! [0 0 0 0 1 0 0 0 0 0 0 0 0]
72//! <<<<<<
73//! [0 0 0 0 0 0 0 1 1 1 0 0 0]
74//! ```
75//! * 4th location:
76//! ```none
77//! . # # # . . . . # # . . .
78//! [0 0 0 0 1 0 0 0 0 0 0 0 0]
79//! <<<<<<<<
80//! [0 0 0 0 0 0 0 1 1 1 1 0 0]
81//! ```
82//! * The final `#` can also match at 4 possible locations (for brevity only showing the 2nd pattern
83//! in a single position):
84//! ```none
85//! . # # # . # # . # . . . .
86//! [0 0 0 0 1 0 0 0 0 0 0 0 0]
87//! [0 0 0 0 0 0 0 1 1 1 1 0 0]
88//! <<
89//! [0 0 0 0 0 0 0 0 1 0 0 0 0]
90//! ```
91//! * 2nd location:
92//! ```none
93//! . # # # . # # . . # . . .
94//! [0 0 0 0 1 0 0 0 0 0 0 0 0]
95//! [0 0 0 0 0 0 0 1 1 1 1 0 0]
96//! <<<<
97//! [0 0 0 0 0 0 0 0 1 2 0 0 0]
98//! ```
99//! * 3rd location:
100//! ```none
101//! . # # # . # # . . . # . .
102//! [0 0 0 0 1 0 0 0 0 0 0 0 0]
103//! [0 0 0 0 0 0 0 1 1 1 1 0 0]
104//! <<<<<<
105//! [0 0 0 0 0 0 0 0 1 2 3 0 0]
106//! ```
107//! * 4th location:
108//! ```none
109//! . # # # . # # . . . . # .
110//! [0 0 0 0 1 0 0 0 0 0 0 0 0]
111//! [0 0 0 0 0 0 0 1 1 1 1 0 0]
112//! <<<<<<<<
113//! [0 0 0 0 0 0 0 0 1 2 3 4 0]
114//! ```
115//!
116//! The final result is then the sum of the bottom row with the nuance that any numbers before the
117//! last `#` don't count as they represent an invalid pattern.
118//!
119//! This is equivalent to the prefix sum approach described above but a little clearer to
120//! understand however slower to calculate.
121use crate::util::parse::*;
122use crate::util::thread::*;
123
124type Spring<'a> = (&'a [u8], Vec<usize>);
125
126pub fn parse(input: &str) -> Vec<Spring<'_>> {
127 input
128 .lines()
129 .map(|line| {
130 let (prefix, suffix) = line.split_once(' ').unwrap();
131 (prefix.as_bytes(), suffix.iter_unsigned().collect())
132 })
133 .collect()
134}
135
136pub fn part1(input: &[Spring<'_>]) -> u64 {
137 solve(input.iter(), 1)
138}
139
140pub fn part2(input: &[Spring<'_>]) -> u64 {
141 // Use as many cores as possible to parallelize the calculation.
142 let result = spawn_parallel_iterator(input, |iter| solve(iter, 5));
143 result.into_iter().sum()
144}
145
146fn solve<'a>(iter: impl Iterator<Item = &'a Spring<'a>>, repeat: usize) -> u64 {
147 let mut pattern = Vec::new();
148 let mut springs = Vec::new();
149 // Exact size is not too important as long as there's enough space.
150 let mut broken = vec![0; 200];
151 let mut table = vec![0; 200 * 50];
152
153 iter.map(|(first, second)| {
154 // Create input sequence reusing the buffers to minimize memory allocations.
155 pattern.clear();
156 springs.clear();
157
158 for _ in 0..repeat {
159 pattern.extend_from_slice(first);
160 pattern.push(b'?');
161 springs.extend_from_slice(second);
162 }
163
164 // Replace the final '?' with a trailing '.' so that we don't have to check bounds when
165 // testing the last pattern. This has no effect on the number of possible combinations.
166 *pattern.last_mut().unwrap() = b'.';
167
168 // Calculate prefix sum of the number of broken springs and unknowns before each index
169 // to quickly check if a range can contain a broken spring without checking every element.
170 // For example `.??..??...?##` becomes `[0, 0, 1, 2, 2, 2, 3, 4, 4, 4, 4, 5, 6, 7, 7]`.
171 let mut sum = 0;
172
173 for (i, &b) in pattern.iter().enumerate() {
174 sum += usize::from(b != b'.');
175 broken[i + 1] = sum;
176 }
177
178 // Determine how many spaces each pattern can slide around to speed things up.
179 // We only need to check at most that many spaces for each pattern.
180 let wiggle = pattern.len() - springs.iter().sum::<usize>() - springs.len() + 1;
181
182 // Count combinations, handling the first row as a special case.
183 let size = springs[0];
184 let mut sum = 0;
185 let mut valid = true;
186
187 for i in 0..wiggle {
188 // In order to be a broken spring, an interval must only contain `#` or `?`
189 // characters and not have a '#' character immediately before or after.
190 if pattern[i + size] == b'#' {
191 sum = 0;
192 } else if valid && broken[i + size] - broken[i] == size {
193 sum += 1;
194 }
195
196 table[i + size] = sum;
197
198 // The first pattern can't have any '#' characters anywhere to its left
199 // otherwise it wouldn't be the first pattern.
200 valid &= pattern[i] != b'#';
201 }
202
203 // Count each subsequent spring. The previous patterns take at least the sum of their size
204 // and 1 space afterward so no need to check indices before that.
205 let mut start = size + 1;
206
207 for (row, &size) in springs.iter().enumerate().skip(1) {
208 // We're using a one-dimensional vec to implement a two-dimensional table.
209 // Calculate the starting index of current and previous row for convenience.
210 let previous = (row - 1) * pattern.len();
211 let current = row * pattern.len();
212
213 // Reset the running sum.
214 sum = 0;
215
216 for i in start..start + wiggle {
217 // As a minor optimization only check the pattern if the previous row
218 // will contribute a non-zero value. A '#' immediately before the pattern
219 // needs no separate check as the previous row is always zero there.
220 if pattern[i + size] == b'#' {
221 sum = 0;
222 } else if table[previous + i - 1] > 0 && broken[i + size] - broken[i] == size {
223 sum += table[previous + i - 1];
224 }
225
226 table[current + i + size] = sum;
227 }
228
229 start += size + 1;
230 }
231
232 // The final value of sum (the bottom right of the table) is the number of possible
233 // arrangements of the pattern.
234 sum
235 })
236 .sum()
237}