-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.rs
184 lines (166 loc) · 5.19 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
183
184
use std::{
collections::HashMap,
fmt::{Display, Write},
};
use aoc_lib::{answer::Answer, solution::Solution};
pub struct Day12;
impl Solution for Day12 {
fn part_a(&self, input: &[String]) -> Answer {
let parsed = parse(input);
parsed
.rows
.iter()
.map(|r| r.count_arrangements())
.sum::<usize>()
.into()
}
fn part_b(&self, input: &[String]) -> Answer {
let parsed = parse(input);
parsed
.rows
.iter()
.map(|r| r.expand().count_arrangements())
.sum::<usize>()
.into()
}
}
struct Parsed {
rows: Vec<Row>,
}
fn parse(input: &[String]) -> Parsed {
let mut rows = vec![];
for line in input {
let (conditions, group_sized) = line.split_once(' ').unwrap();
let group_sizes = group_sized
.split(',')
.map(|d| d.parse::<usize>().unwrap())
.collect::<Vec<_>>();
let mut conditions = conditions
.chars()
.map(SpringCondition::from)
.collect::<Vec<_>>();
// to ensure block separation at the end
conditions.push(SpringCondition::Working);
rows.push(Row {
conditions,
group_sizes,
})
}
Parsed { rows }
}
#[derive(Clone)]
struct Row {
conditions: Vec<SpringCondition>,
group_sizes: Vec<usize>,
}
impl Row {
fn count_arrangements(&self) -> usize {
fn dfs(
memo: &mut HashMap<(usize, usize, usize), usize>,
row: &Row,
pos: usize,
group_index: usize,
group_len: usize,
) -> usize {
if let Some(&key) = memo.get(&(pos, group_index, group_len)) {
return key;
}
let mut arrangements = 0;
if pos == row.conditions.len() {
arrangements = (group_index == row.group_sizes.len()) as usize;
} else if row.conditions[pos] == SpringCondition::Broken {
arrangements = dfs(memo, row, pos + 1, group_index, group_len + 1)
} else if row.conditions[pos] == SpringCondition::Working
|| group_index == row.group_sizes.len()
{
if group_index < row.group_sizes.len() && group_len == row.group_sizes[group_index]
{
// closing the block
arrangements = dfs(memo, row, pos + 1, group_index + 1, 0);
} else if group_len == 0 {
// multiple working..
arrangements = dfs(memo, row, pos + 1, group_index, 0);
}
} else {
// continue with broken spring
arrangements += dfs(memo, row, pos + 1, group_index, group_len + 1);
// finished the block, closing
if group_len == row.group_sizes[group_index] {
arrangements += dfs(memo, row, pos + 1, group_index + 1, 0);
} else if group_len == 0 {
arrangements += dfs(memo, row, pos + 1, group_index, 0);
}
}
memo.insert((pos, group_index, group_len), arrangements);
arrangements
}
dfs(&mut HashMap::new(), self, 0, 0, 0)
}
fn expand(&self) -> Self {
let mut new_condition = self.conditions.clone();
*new_condition.last_mut().unwrap() = SpringCondition::Unknown;
Self {
conditions: new_condition.repeat(5),
group_sizes: self.group_sizes.repeat(5),
}
}
}
#[derive(Clone, PartialEq, Copy)]
enum SpringCondition {
Broken,
Working,
Unknown,
}
impl Display for SpringCondition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SpringCondition::Broken => f.write_char('#'),
SpringCondition::Working => f.write_char('.'),
SpringCondition::Unknown => f.write_char('?'),
}
}
}
impl From<SpringCondition> for char {
fn from(value: SpringCondition) -> Self {
match value {
SpringCondition::Broken => '#',
SpringCondition::Working => '.',
SpringCondition::Unknown => '?',
}
}
}
impl From<char> for SpringCondition {
fn from(value: char) -> Self {
match value {
'.' => SpringCondition::Working,
'#' => SpringCondition::Broken,
'?' => SpringCondition::Unknown,
_ => unreachable!(),
}
}
}
#[cfg(test)]
mod test {
use aoc_lib::{self, answer::Answer, input, solution::Solution};
use super::Day12;
#[test]
fn test_a() {
let input = input::read_file(&format!(
"{}day_12_test.txt",
crate::FILES_PREFIX_TEST
))
.unwrap();
let answer = Day12.part_a(&input);
assert_eq!(<i32 as Into<Answer>>::into(21), answer);
}
#[test]
fn test_b() {
let input = input::read_file(&format!(
"{}day_12_test.txt",
crate::FILES_PREFIX_TEST
))
.unwrap();
let answer = Day12.part_b(&input);
assert_eq!(<i32 as Into<Answer>>::into(525152), answer);
}
}