-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
216 lines (185 loc) · 6.61 KB
/
mod.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
use std::collections::HashMap;
use itertools::Itertools;
use crate::utils::{get_lines, part_end, part_start};
pub fn main() {
part_one();
part_two();
}
fn part_one() {
let start = part_start(1);
println!("Result: {}", compute_count_sum("d12/input"));
part_end(start);
}
fn part_two() {
let start = part_start(2);
println!("Result: {}", compute_unfolded_count_sum("d12/input"));
part_end(start);
}
fn compute_count_sum(file_path: &str) -> usize {
let mut cache: HashMap<String, usize> = HashMap::new();
parse_input(file_path)
.iter()
.map(|(row, counts)| get_or_compute_for_row(&skip_and_trim(row, 0), counts, &mut cache))
.sum()
}
fn compute_unfolded_count_sum(file_path: &str) -> usize {
let mut cache: HashMap<String, usize> = HashMap::new();
parse_input(file_path)
.iter()
.map(|(row, counts)| {
(
[row; 5].iter().join("?"),
[counts; 5].into_iter().flatten().map(|&n| n).collect_vec(),
)
})
.map(|(row, counts)| get_or_compute_for_row(&skip_and_trim(&row, 0), &counts, &mut cache))
.sum()
}
fn get_or_compute_for_row(r: &str, counts: &Vec<usize>, cache: &mut HashMap<String, usize>) -> usize {
let cache_key = format!("{}_{}", r, counts.iter().join("_"));
if cache.contains_key(&cache_key) {
return *cache.get(&cache_key).unwrap();
}
let result = compute_for_row(r, counts, cache);
cache.insert(cache_key, result);
result
}
fn compute_for_row(r: &str, counts: &Vec<usize>, cache: &mut HashMap<String, usize>) -> usize {
// When the full row was already processed, there should be no damage count left
if r.is_empty() {
return counts.is_empty() as usize;
}
// When there is no damage count left, there should only be '.' and '?' (which will be regarded as '.') remaining
if counts.is_empty() {
return !r.contains('#') as usize;
}
let c = *counts.first().unwrap();
// This is invalid: "## 3" or "?? 3" or even ".. 3"
if c > r.len() {
return 0;
}
// There is only a single valid arrangement in case of: "### 3" or "??? 3" or "#?# 3", but not "??. 3" or "##. 3"
if c == r.len() {
return (!r.contains('.') && counts.len() == 1) as usize;
}
// Early termination in case the counts + single gap exceed row length
if counts.iter().sum::<usize>() + counts.len() - 1 > r.len() {
return 0;
}
let followed_by_damaged = r.chars().skip(c).next().unwrap() == '#';
let next_count_has_operational = r[0..c].contains('.');
// See below in which cases this is relevant
let cannot_be_damaged = followed_by_damaged || next_count_has_operational;
let first_char = r.chars().next().unwrap();
match first_char {
'#' => {
// Such cases are invalid: "##. 3" or "#??# 3"
if cannot_be_damaged {
return 0;
}
let counts_without_first = &counts.into_iter().skip(1).map(|&n| n).collect_vec();
get_or_compute_for_row(&skip_and_trim(r, c + 1), counts_without_first, cache)
}
'?' => {
let result_when_not_placed = get_or_compute_for_row(&skip_and_trim(r, 1), counts, cache);
// Next '?' must mean '.' in such cases: "??. 3" or "???# 3"
if cannot_be_damaged {
return result_when_not_placed;
}
let counts_without_first = &counts.into_iter().skip(1).map(|&n| n).collect_vec();
let result_when_placed = get_or_compute_for_row(&skip_and_trim(r, c + 1), counts_without_first, cache);
return result_when_placed + result_when_not_placed;
}
_ => unreachable!("Impossible char: {first_char}"),
}
}
fn skip_and_trim(row: &str, skip: usize) -> String {
String::from(row[skip..].trim_matches(|c| c == '.'))
}
fn parse_input(file_path: &str) -> Vec<(String, Vec<usize>)> {
get_lines(file_path)
.iter()
.map(|line| line.split_once(' ').unwrap())
.map(|(s, nums)| {
(
String::from(s),
nums.split(',').map(|n| n.parse::<usize>().unwrap()).collect_vec(),
)
})
.collect_vec()
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
#[test]
fn example_part_one() {
let res = super::compute_count_sum("d12/example");
assert_eq!(21, res);
}
#[test]
fn example_part_two() {
let res = super::compute_unfolded_count_sum("d12/example");
assert_eq!(525152, res);
}
// Investigated some examples during debugging, so I kept them:
#[test]
fn test_case_1() {
let res = super::compute_for_row("?#?#?#?#?#?#?#?", &vec![1, 3, 1, 6], &mut HashMap::new());
assert_eq!(1, res);
}
#[test]
fn test_case_2() {
let res = super::compute_for_row("????.######..#####.", &vec![1, 6, 5], &mut HashMap::new());
assert_eq!(4, res);
}
#[test]
fn test_case_3() {
let res = super::compute_for_row("?###????????", &vec![3, 2, 1], &mut HashMap::new());
assert_eq!(10, res);
}
#[test]
fn test_case_4() {
let res = super::compute_for_row("??????#?#?#??", &vec![2, 2, 6], &mut HashMap::new());
assert_eq!(1, res);
}
#[test]
fn test_case_5() {
let res = super::compute_for_row("??????.?##?#??#?????", &vec![3, 1, 12], &mut HashMap::new());
assert_eq!(6, res);
}
#[test]
fn test_case_6() {
let res = super::compute_for_row("??#?.#??.?#?.???", &vec![2, 2, 2, 1, 1], &mut HashMap::new());
assert_eq!(4, res);
}
#[test]
fn test_case_7() {
let res = super::compute_for_row("??#?????#?#?????", &vec![1, 5, 1, 3, 1], &mut HashMap::new());
assert_eq!(2, res);
}
#[test]
fn test_case_8() {
let res = super::compute_for_row("?????#??#???##?.?#??", &vec![1, 6, 3, 3], &mut HashMap::new());
assert_eq!(28, res);
}
#[test]
fn test_case_9() {
let res = super::compute_for_row("?#..??#.????####?##?", &vec![2, 3, 3, 4, 3], &mut HashMap::new());
assert_eq!(1, res);
}
#[test]
fn test_case_10() {
let res = super::compute_for_row("??.?????????", &vec![2, 1, 2, 1], &mut HashMap::new());
assert_eq!(21, res);
}
#[test]
fn test_case_11() {
let res = super::compute_for_row("?????????", &vec![1, 2, 1], &mut HashMap::new());
assert_eq!(20, res);
}
#[test]
fn test_case_12() {
let res = super::compute_for_row("?.?????????", &vec![2, 1, 2, 1], &mut HashMap::new());
assert_eq!(1, res);
}
}