-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.rs
233 lines (198 loc) · 7.2 KB
/
day21.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//! [Day 21: Scrambled Letters and Hash](https://adventofcode.com/2016/day/21)
use regex::Regex;
enum Operation {
SwapPosition(usize, usize),
SwapLetter(char, char),
ReversePositions(usize, usize),
RotateLeft(usize),
RotateRight(usize),
MovePosition(usize, usize),
RotateBased(char),
}
impl Operation {
#[cfg(test)]
fn perform_str(&self, password: &str) -> String {
let mut p: Vec<_> = password.chars().collect();
self.perform(&mut p);
String::from_iter(p.iter())
}
#[cfg(test)]
fn reverse_str(&self, password: &str) -> String {
let mut p: Vec<_> = password.chars().collect();
self.reverse(&mut p);
String::from_iter(p.iter())
}
fn perform(&self, password: &mut [char]) {
let pos = |ch: &char| password.iter().position(|c| c == ch).unwrap();
match self {
&Self::SwapPosition(a, b) => {
password.swap(a, b);
}
Self::SwapLetter(a, b) => {
let a = pos(a);
let b = pos(b);
password.swap(a, b);
}
&Self::ReversePositions(a, b) => {
for i in 0..=((b - a) / 2) {
password.swap(a + i, b - i);
}
}
&Self::RotateLeft(p) => {
password.rotate_left(p);
}
&Self::RotateRight(p) => {
password.rotate_right(p);
}
&Self::MovePosition(a, b) => {
let c = password[a];
if a < b {
for i in a..b {
password[i] = password[i + 1];
}
} else {
for i in (b..a).rev() {
password[i + 1] = password[i];
}
}
password[b] = c;
}
Self::RotateBased(a) => {
let a = pos(a);
password.rotate_right(a);
password.rotate_right(1);
if a >= 4 {
password.rotate_right(1);
}
}
}
}
fn reverse(&self, password: &mut [char]) {
match self {
Self::SwapPosition(_, _) | Self::SwapLetter(_, _) | Self::ReversePositions(_, _) => {
self.perform(password);
}
Self::RotateLeft(p) => Self::RotateRight(*p).perform(password),
Self::RotateRight(p) => Self::RotateLeft(*p).perform(password),
Self::MovePosition(a, b) => Self::MovePosition(*b, *a).perform(password),
Self::RotateBased(_a) => {
// dummy reverse
let n = password.len();
for i in 0..n {
let mut p: Vec<char> = password.to_vec();
p.rotate_right(i);
self.perform(&mut p);
if p == password {
password.rotate_right(i);
break;
}
}
}
}
}
}
struct Puzzle {
ops: Vec<Operation>,
}
impl Puzzle {
fn new(data: &str) -> Self {
let mut ops = Vec::new();
let re1 = Regex::new(r"rotate based on position of letter (\w)").unwrap();
let re2 = Regex::new(r"move position (\d+) to position (\d+)").unwrap();
let re3 = Regex::new(r"reverse positions (\d+) through (\d+)").unwrap();
let re4 = Regex::new(r"rotate left (\d+) steps?").unwrap();
let re5 = Regex::new(r"rotate right (\d+) steps?").unwrap();
let re6 = Regex::new(r"swap letter (\w) with letter (\w)").unwrap();
let re7 = Regex::new(r"swap position (\d+) with position (\d+)").unwrap();
for line in data.lines() {
if let Some(m) = re1.captures(line) {
let c = m[1].chars().next().unwrap();
ops.push(Operation::RotateBased(c));
} else if let Some(m) = re2.captures(line) {
let a = m[1].parse().unwrap();
let b = m[2].parse().unwrap();
ops.push(Operation::MovePosition(a, b));
} else if let Some(m) = re3.captures(line) {
let a = m[1].parse().unwrap();
let b = m[2].parse().unwrap();
ops.push(Operation::ReversePositions(a, b));
} else if let Some(m) = re4.captures(line) {
let a = m[1].parse().unwrap();
ops.push(Operation::RotateLeft(a));
} else if let Some(m) = re5.captures(line) {
let a = m[1].parse().unwrap();
ops.push(Operation::RotateRight(a));
} else if let Some(m) = re6.captures(line) {
let a = m[1].chars().next().unwrap();
let b = m[2].chars().next().unwrap();
ops.push(Operation::SwapLetter(a, b));
} else if let Some(m) = re7.captures(line) {
let a = m[1].parse().unwrap();
let b = m[2].parse().unwrap();
ops.push(Operation::SwapPosition(a, b));
} else {
panic!("error: {line}");
}
}
Self { ops }
}
/// Solve part one.
fn part1(&self) -> String {
let mut password: Vec<_> = "abcdefgh".chars().collect();
for op in &self.ops {
op.perform(&mut password);
}
String::from_iter(&password)
}
/// Solve part two.
fn part2(&self) -> String {
let mut password: Vec<_> = "fbgdceah".chars().collect();
for op in self.ops.iter().rev() {
op.reverse(&mut password);
}
String::from_iter(&password)
}
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (String, String) {
let puzzle = Puzzle::new(data);
(puzzle.part1(), puzzle.part2())
}
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test01() {
let mut password = "abcde".to_string();
password = Operation::SwapPosition(0, 4).perform_str(&password);
assert_eq!(password, "ebcda");
password = Operation::SwapLetter('d', 'b').perform_str(&password);
assert_eq!(password, "edcba");
password = Operation::ReversePositions(0, 4).perform_str(&password);
assert_eq!(password, "abcde");
password = Operation::RotateLeft(1).perform_str(&password);
assert_eq!(password, "bcdea");
password = Operation::MovePosition(1, 4).perform_str(&password);
assert_eq!(password, "bdeac");
password = Operation::MovePosition(3, 0).perform_str(&password);
assert_eq!(password, "abdec");
password = Operation::RotateBased('b').perform_str(&password);
assert_eq!(password, "ecabd");
password = Operation::RotateBased('d').perform_str(&password);
assert_eq!(password, "decab");
}
#[test]
fn test02() {
let p = Operation::RotateBased('b').perform_str("abdec");
assert_eq!(p, "ecabd");
let p = Operation::RotateBased('b').reverse_str(&p);
assert_eq!(p, "abdec");
let p = Operation::RotateBased('d').reverse_str("facdgehb");
assert_eq!(p, "cdgehbfa");
}
}