-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_12.rs
159 lines (140 loc) · 4.59 KB
/
day_12.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
//! Day 12: Hort Springs
//!
//! I didn't really have an idea how to solve this problem. I thought that it might be possible to
//! start looking from the end of the pattern somehow. After some reasoning with ChatGPT about
//! the problem it gave me something called "Dynamic Programming" where you break
//! the problem into smaller subproblems and then solve them.
//!
//! So, this solution was an iterative process of trying to understand and implementing
//! the algorithm.
//!
//! I later tested a solutions using recursion and memoization. It was a bit easier to
//! understand, but it was also slower.
use itertools::Itertools;
pub struct Input {
data: Vec<(String, Vec<usize>)>,
}
#[aoc_generator(day12)]
pub fn input_generator(input: &str) -> Input {
let data =
input
.lines()
.map(|line| {
let (springs, damaged) = line.split_once(' ').expect("Invalid input");
let damaged = damaged
.split(',')
.map(|s| s.parse::<usize>().unwrap())
.collect::<Vec<_>>();
(springs.to_string(), damaged)
})
.collect::<Vec<_>>();
Input { data }
}
pub fn possible_arrangements(springs: &[u8], damaged: &[usize]) -> usize {
// Setup the DP table
let num_springs = springs.len();
let num_damaged = damaged.len();
let mut dp = vec![vec![vec![0; num_springs + 1]; num_damaged + 1]; num_springs + 1];
// Base values
dp[num_springs][num_damaged][0] = 1;
dp[num_springs][num_damaged - 1][damaged[num_damaged - 1]] = 1;
// Go through the conditions backwards
for position in (0..num_springs).rev() {
for (group, &j) in damaged.iter().enumerate() {
for count in 0..=j {
for &c in &[b'.', b'#'] {
// If the current character is not a '?' or the character type
// we're looking for, then we can skip it.
if ![c, b'?'].contains(&springs[position]) {
continue;
}
let p = position + 1;
let value = match (c, count) {
(b'.', 0) => dp[p][group][0],
(b'.', c) if damaged[group] == c => dp[p][group + 1][0],
(b'#', _) => dp[p][group][count + 1],
_ => 0,
};
dp[position][group][count] += value;
}
}
}
// If the current character is a '?' or '.', then we can also add the value from the next
if matches!(springs[position], b'?' | b'.') {
dp[position][num_damaged][0] += dp[position + 1][num_damaged][0];
}
}
// The problem is solved backwards, so the answer will be in the first cell
dp[0][0][0]
}
/* Part One
*
*
*/
// Your puzzle answer was
#[doc = r#"```
use advent_of_code_2023::day_12::*;
let data = include_str!("../input/2023/day12.txt");
assert_eq!(solve_part_01(&input_generator(data)), 8270);
```"#]
#[aoc(day12, part1)]
pub fn solve_part_01(input: &Input) -> usize {
input
.data
.iter()
.map(|(springs, damaged)| possible_arrangements(springs.as_bytes(), damaged))
.sum::<usize>()
}
/* Part Two
*
*
*/
#[doc = r#"```
use advent_of_code_2023::day_12::*;
let data = include_str!("../input/2023/day12.txt");
assert_eq!(solve_part_02(&input_generator(data)), 204640299929836);
```"#]
#[aoc(day12, part2)]
pub fn solve_part_02(input: &Input) -> usize {
input
.data
.iter()
.map(|(conditions, damaged)| {
// Repeat the conditions and list of strings 5 times. Separate the patterns with '?'.
let springs = (0..5).map(|_| conditions).join("?");
let damaged = damaged.repeat(5);
possible_arrangements(springs.as_bytes(), &damaged)
})
.sum::<usize>()
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
#[rstest]
#[case(
"???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1",
21
)]
fn sample_01(#[case] input: &str, #[case] expected: usize) {
assert_eq!(solve_part_01(&input_generator(input)), expected);
}
#[rstest]
#[case(
"???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1",
525152
)]
fn sample_02(#[case] input: &str, #[case] expected: usize) {
assert_eq!(solve_part_02(&input_generator(input)), expected);
}
}