-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_2021_14.rs
183 lines (154 loc) · 5.22 KB
/
solution_2021_14.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
use std::collections::HashMap;
use std::str::FromStr;
use advent_of_code_common::parsing::{
Error, parse_lines_to_hashmap, parse_string_to_nonempty,
split_into_two_segments_separated_by_double_newline,
};
use advent_of_code_common::utils::additive_hashmap_from_vec;
use nonempty::NonEmpty;
const DATA: &str = include_str!("../../resources/14.txt");
#[derive(Clone, Eq, PartialEq, Hash, Debug, Copy)]
struct Element(char);
impl FromStr for Element {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let ch = s.chars().next().ok_or("Fail")?;
Ok(Element(ch))
}
}
type Polymer = NonEmpty<Element>;
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
struct SearchPattern {
a: Element,
b: Element,
}
impl FromStr for SearchPattern {
type Err = Error;
fn from_str(input: &str) -> Result<Self, Self::Err> {
let mut chars = input.chars();
let a_ch = chars.next().ok_or("Fail")?;
let b_ch = chars.next().ok_or("Fail")?;
let a = Element(a_ch);
let b = Element(b_ch);
Ok(SearchPattern { a, b })
}
}
struct Data {
starting_polymer: Polymer,
templates: HashMap<SearchPattern, Element>,
}
impl Data {
fn starting_polymer_as_frequency_map(&self) -> HashMap<SearchPattern, usize> {
let mut result = HashMap::new();
for idx in 0 .. self.starting_polymer.len() - 1 {
let a = self.starting_polymer[idx];
let b = self.starting_polymer[idx + 1];
let k = SearchPattern { a, b };
let existing = result.get(&k).unwrap_or(&0);
result.insert(k, existing + 1);
}
result
}
fn next_iteration(
&self,
frequencies: &HashMap<SearchPattern, usize>,
) -> HashMap<SearchPattern, usize> {
let vec: Vec<_> = frequencies
.iter()
.flat_map(|(pattern, &count)| {
let found = self.templates.get(pattern).unwrap();
let left = SearchPattern {
a: pattern.a,
b: *found,
};
let right = SearchPattern {
a: *found,
b: pattern.b,
};
vec![(left, count), (right, count)]
})
.collect();
additive_hashmap_from_vec(vec)
}
fn element_occurrences_after_n_steps(&self, steps: usize) -> HashMap<Element, usize> {
let mut frequencies = self.starting_polymer_as_frequency_map();
for _ in 0 .. steps {
frequencies = self.next_iteration(&frequencies);
}
let first: Vec<_> = vec![(self.starting_polymer.head, 1)];
let other: Vec<_> = frequencies.iter().map(|(k, &v)| (k.b, v)).collect();
let joined = [first, other].concat();
additive_hashmap_from_vec(joined)
}
}
impl FromStr for Data {
type Err = Error;
fn from_str(input: &str) -> Result<Self, Self::Err> {
let (a, b) = split_into_two_segments_separated_by_double_newline(input)?;
let starting_polymer: NonEmpty<Element> = parse_string_to_nonempty(&a)?;
let templates: HashMap<SearchPattern, Element> = parse_lines_to_hashmap(&b, " -> ")?;
Ok(Data {
starting_polymer,
templates,
})
}
}
fn solve(input: &str, iterations: usize) -> Result<usize, Error> {
let data: Data = input.parse()?;
let elements: HashMap<Element, usize> = data.element_occurrences_after_n_steps(iterations);
let frequencies: Vec<usize> = elements.values().copied().collect();
let min = frequencies.iter().min().unwrap();
let max = frequencies.iter().max().unwrap();
Ok(max - min)
}
fn main() {
let result_1 = solve(DATA, 10);
println!("Part 1: {result_1:?}");
let result_2 = solve(DATA, 40);
println!("Part 2: {result_2:?}");
}
#[cfg(test)]
mod tests {
use super::*;
const TEST_DATA: &str = include_str!("../../resources/14-test.txt");
#[test]
fn test_solve_on_test_data() {
let test_data: Data = TEST_DATA.parse().unwrap();
let c: Element = "C".parse().unwrap();
let b: Element = "B".parse().unwrap();
let n: Element = "N".parse().unwrap();
let h: Element = "H".parse().unwrap();
assert_eq!(
test_data.element_occurrences_after_n_steps(0),
HashMap::from([(c, 1), (b, 1), (n, 2)])
);
assert_eq!(
test_data.element_occurrences_after_n_steps(1),
HashMap::from([(h, 1), (b, 2), (c, 2), (n, 2)])
);
assert_eq!(
test_data.element_occurrences_after_n_steps(2),
HashMap::from([(c, 4), (h, 1), (n, 2), (b, 6)])
);
assert_eq!(
test_data.element_occurrences_after_n_steps(10),
HashMap::from([(h, 161), (n, 865), (b, 1749), (c, 298)])
);
}
#[test]
fn test_solve_1_test() {
assert_eq!(solve(TEST_DATA, 10), Ok(1588));
}
#[test]
fn test_solve_1_real() {
assert_eq!(solve(DATA, 10), Ok(2112));
}
#[test]
fn test_solve_2_test() {
assert_eq!(solve(TEST_DATA, 40), Ok(2_188_189_693_529));
}
#[test]
fn test_solve_2_real() {
assert_eq!(solve(DATA, 40), Ok(3_243_771_149_914));
}
}