-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.rs
168 lines (157 loc) · 5.6 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
use aoc_runner_derive::aoc;
fn parse(input: &str) -> Vec<utils::SpringSequence> {
input
.lines()
.map(|line| {
let (springs, contiguously_damaged_springs) =
line.split_once(char::is_whitespace).unwrap();
utils::SpringSequence {
springs: springs.chars().map(|c| c.try_into().unwrap()).collect(),
contiguously_damaged_springs: contiguously_damaged_springs
.split(',')
.map(|s| s.parse().unwrap())
.collect(),
}
})
.collect()
}
#[aoc(day12, part1)]
pub fn part1(input: &str) -> usize {
let input = parse(input);
input
.iter()
.map(utils::SpringSequence::discover_arrangements)
.sum()
}
#[aoc(day12, part2)]
#[must_use]
pub fn part2(input: &str) -> usize {
let input = parse(input);
input
.iter()
.map(|sequence| {
// Repeat the input while joining with an unknown spring
const N_REPEATS: usize = 5;
utils::SpringSequence {
springs: itertools::Itertools::intersperse(
[&sequence.springs; N_REPEATS].into_iter(),
&vec![utils::SpringType::Unknown],
)
.flatten()
.copied()
.collect(),
contiguously_damaged_springs: sequence
.contiguously_damaged_springs
.repeat(N_REPEATS),
}
})
.map(|sequence| sequence.discover_arrangements())
.sum()
}
mod utils {
pub struct SpringSequence {
pub springs: Vec<SpringType>,
pub contiguously_damaged_springs: Vec<usize>,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum SpringType {
Operational,
Damaged,
Unknown,
}
impl TryFrom<char> for SpringType {
type Error = &'static str;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'.' => Ok(Self::Operational),
'#' => Ok(Self::Damaged),
'?' => Ok(Self::Unknown),
_ => Err("Unknown spring type"),
}
}
}
impl SpringSequence {
pub fn discover_arrangements(&self) -> usize {
let last = self.contiguously_damaged_springs.last().unwrap();
self.contiguously_damaged_springs
.iter()
.rev()
.skip(1)
.fold(
(0..self.springs.len())
.map(|i| {
usize::from(
!(last + i > self.springs.len()
|| self.springs[i..i + last]
.iter()
.any(|&spring| spring == SpringType::Operational)
|| self.springs[i + last..]
.iter()
.any(|&spring| spring == SpringType::Damaged)
|| i != 0 && self.springs[i - 1] == SpringType::Damaged),
)
})
.collect::<Vec<_>>(),
|counts, run| {
(0..self.springs.len())
.map(|i| {
if run + i >= self.springs.len()
|| i != 0 && self.springs[i - 1] == SpringType::Damaged
|| self.springs[i..run + i]
.iter()
.any(|&spring| spring == SpringType::Operational)
|| self.springs[run + i] == SpringType::Damaged
{
0
} else {
counts[run + i + 1..]
.iter()
.zip(
self.springs[run + i + 1..]
.iter()
.take_while(|&&spring| {
spring != SpringType::Damaged
})
.chain([&SpringType::Damaged]),
)
.map(|(&count, _)| count)
.sum()
}
})
.collect()
},
)
.into_iter()
.zip(
self.springs
.clone()
.into_iter()
.take_while(|&spring| spring != SpringType::Damaged)
.chain([SpringType::Damaged]),
)
.map(|(count, _)| count)
.sum()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
const SAMPLE: &str = indoc! {"
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1
"};
#[test]
pub fn part1_example() {
assert_eq!(part1(SAMPLE), 21);
}
#[test]
pub fn part2_example() {
assert_eq!(part2(SAMPLE), 525_152);
}
}