-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
311 lines (282 loc) · 10.5 KB
/
main.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
use std::{fmt::Display, str::FromStr};
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one::<8>(data));
println!("Part 2: {}", part_two::<8>(data));
}
fn part_one<const N: usize>(data: &str) -> Password<N> {
let mut password = Password::new();
for line in data.lines() {
let instr: Instruction<N> = line.parse().unwrap();
password.apply(instr);
}
password
}
fn part_two<const N: usize>(data: &str) -> Password<N> {
let mut password: Password<N> = "fbgdceah".parse().unwrap();
for line in data.lines().rev() {
let instr: Instruction<N> = line.parse().unwrap();
password.apply(instr.reverse());
}
password
}
struct Password<const N: usize>([u8; N]);
impl<const N: usize> Password<N> {
fn new() -> Self {
let data = std::array::from_fn(|i| b'a' + u8::try_from(i).unwrap());
Self(data)
}
fn apply(&mut self, instr: Instruction<N>) {
match instr {
Instruction::SwapIndex((i, j)) => self.swap_elements(i.0, j.0),
Instruction::SwapLetter((x, y)) => {
self.swap_elements(self.find_letter(x.0), self.find_letter(y.0));
}
Instruction::RotateLeft(i) => self.rotate(N - i.0),
Instruction::RotateRight(i) => self.rotate(i.0),
Instruction::RotateLetter(x) => {
let mut index = self.find_letter(x.0);
if index >= 4 {
index += 1;
}
index += 1;
self.rotate(index);
}
Instruction::RotateReverseLetter(x) => {
// Normal mapping has unique from -> to relationship:
// 0 -> 1 (0 + 1 + 0 + 0)
// 1 -> 3 (1 + 1 + 1 + 0)
// 2 -> 5 (2 + 1 + 2 + 0)
// 3 -> 7 (3 + 1 + 3 + 0)
// 4 -> 2 (4 + 1 + 4 + 1) % 8
// 5 -> 4 (5 + 1 + 5 + 1) % 8
// 6 -> 6 (6 + 1 + 6 + 1) % 8
// 7 -> 0 (7 + 1 + 7 + 1) % 8
// This means we can just hard-code the reverse mapping
let map_to = [7, 0, 4, 1, 5, 2, 6, 3];
let from = self.find_letter(x.0);
let to = map_to[from];
self.rotate(to + N - from);
}
Instruction::Reverse((i, j)) => self.reverse(i.0, j.0),
Instruction::Move((i, j)) => self.move_letter(i.0, j.0),
}
}
fn swap_elements(&mut self, i: usize, j: usize) {
let tmp = self.0[i];
let tmp = std::mem::replace(&mut self.0[j], tmp);
self.0[i] = tmp;
}
fn find_letter(&self, letter: u8) -> usize {
self.0
.iter()
.enumerate()
.find(|&(_, l)| l == &letter)
.map(|(i, _)| i)
.unwrap()
}
fn rotate(&mut self, by: usize) {
let tmp = std::array::from_fn(|i| self.0[(i + 2 * N - by) % N]);
self.0 = tmp;
}
fn reverse(&mut self, from: usize, to: usize) {
let mut tot = from + to;
// Bump up if odd
if tot & 1 == 1 {
tot += 1;
}
let half = tot / 2;
for i in from..half {
let j = to - (i - from);
self.swap_elements(i, j);
}
}
fn move_letter(&mut self, from: usize, to: usize) {
let min = std::cmp::min(from, to);
let max = std::cmp::max(from, to);
let tmp = std::array::from_fn(|i| match i {
i if i < min => self.0[i],
i if i > max => self.0[i],
i if i == to => self.0[from],
i if from < to => self.0[i + 1],
i => self.0[i - 1],
});
self.0 = tmp;
}
}
impl<const N: usize> FromStr for Password<N> {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() != N {
return Err(format!(
"String length {} does not match required password length {N}",
s.len()
));
}
let bytes = s.as_bytes();
let data = std::array::from_fn(|i| bytes[i]);
Ok(Self(data))
}
}
impl<const N: usize> Display for Password<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for letter in self.0 {
let letter_array = &[letter];
let char = std::str::from_utf8(letter_array).map_err(|_| std::fmt::Error)?;
write!(f, "{char}")?;
}
Ok(())
}
}
#[derive(Debug, Clone, Copy)]
enum Instruction<const N: usize> {
SwapIndex((PasswordIndex<N>, PasswordIndex<N>)),
SwapLetter((PasswordChar<N>, PasswordChar<N>)),
RotateLeft(PasswordIndex<N>),
RotateRight(PasswordIndex<N>),
RotateLetter(PasswordChar<N>),
RotateReverseLetter(PasswordChar<N>),
Reverse((PasswordIndex<N>, PasswordIndex<N>)),
Move((PasswordIndex<N>, PasswordIndex<N>)),
}
impl<const N: usize> FromStr for Instruction<N> {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (first, rest) = s.split_once(' ').ok_or("Badly formatted")?;
match first {
"swap" => {
let (next, rest2) = rest
.split_once(' ')
.ok_or("Badly formatted swap instruction")?;
match next {
"letter" => {
let (letter1, rest3) = rest2
.split_once(' ')
.ok_or("Badly formatted swap letter instruction")?;
let letter2 = rest3.trim_start_matches("with letter ");
let letter1 = letter1.parse()?;
let letter2 = letter2.parse()?;
Ok(Self::SwapLetter((letter1, letter2)))
}
"position" => {
let (position1, rest3) = rest2
.split_once(' ')
.ok_or("Badly formatted swap letter instruction")?;
let position2 = rest3.trim_start_matches("with position ");
let position1 = position1.parse()?;
let position2 = position2.parse()?;
Ok(Self::SwapIndex((position1, position2)))
}
_ => Err("Incorrect swap type".to_owned()),
}
}
"rotate" => {
let (next, rest2) = rest
.split_once(' ')
.ok_or("Badly formatted rotate instruction")?;
match next {
"left" => {
let (count, _) = rest2
.split_once(' ')
.ok_or("Badly formatted rotate left instruction")?;
let count = count.parse()?;
Ok(Self::RotateLeft(count))
}
"right" => {
let (count, _) = rest2
.split_once(' ')
.ok_or("Badly formatted rotate right instruction")?;
let count = count.parse()?;
Ok(Self::RotateRight(count))
}
"based" => {
let (_, letter) = rest2
.split_once("position of letter ")
.ok_or("Badly formatted rotate based on letter instruction")?;
let letter = letter.parse()?;
Ok(Self::RotateLetter(letter))
}
_ => Err("Incorrect rotate type".to_owned()),
}
}
"reverse" => {
let rest2 = rest.trim_start_matches("positions ");
let (position1, rest3) = rest2
.split_once(' ')
.ok_or("Badly formatted reverse instruction")?;
let position2 = rest3.trim_start_matches("through ");
let position1 = position1.parse()?;
let position2 = position2.parse()?;
Ok(Self::Reverse((position1, position2)))
}
"move" => {
let rest2 = rest.trim_start_matches("position ");
let (position1, rest3) = rest2
.split_once(' ')
.ok_or("Badly formatted move instruction")?;
let position2 = rest3.trim_start_matches("to position ");
let position1 = position1.parse()?;
let position2 = position2.parse()?;
Ok(Self::Move((position1, position2)))
}
_ => Err("Incorrect first instruction word".to_owned()),
}
}
}
impl<const N: usize> Instruction<N> {
const fn reverse(self) -> Self {
match self {
Self::SwapIndex(..) | Self::SwapLetter(..) | Self::Reverse(..) => self,
Self::RotateLeft(i) => Self::RotateRight(i),
Self::RotateRight(i) => Self::RotateLeft(i),
Self::Move((i, j)) => Self::Move((j, i)),
Self::RotateLetter(x) => Self::RotateReverseLetter(x),
Self::RotateReverseLetter(x) => Self::RotateLetter(x),
}
}
}
#[derive(Debug, Clone, Copy)]
struct PasswordIndex<const N: usize>(usize);
impl<const N: usize> FromStr for PasswordIndex<N> {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let n: usize = s
.parse()
.map_err(|_| "Cannot parse supplied index {s} into a usize number")?;
if n > N {
return Err(format!(
"Index number {n} is greater than the max allowed of {N}"
));
}
Ok(Self(n))
}
}
#[derive(Debug, Clone, Copy)]
struct PasswordChar<const N: usize>(u8);
impl<const N: usize> FromStr for PasswordChar<N> {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() > 1 {
return Err(format!("More than one character supplied: {s}"));
}
let n = s.as_bytes()[0];
let end = b'a' + u8::try_from(N).unwrap();
if !(b'a'..=end).contains(&n) {
return Err(format!(
"Character {s} is not between 'a' and '{}'",
std::str::from_utf8(&[end]).unwrap()
));
}
Ok(Self(n))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!("decab", part_one::<5>(data).to_string());
}
}