-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
144 lines (138 loc) Β· 5.5 KB
/
main.rs
File metadata and controls
144 lines (138 loc) Β· 5.5 KB
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
use std::fs;
#[derive(Clone, Copy, PartialEq, Eq)]
enum Instruction {
SwapPosition(usize, usize),
SwapLetter(u8, u8),
RotateLeft(usize),
RotateRight(usize),
RotateBasedOnLetter(u8),
ReversePositions(usize, usize),
MovePosition(usize, usize),
}
fn main() {
let input = fs::read_to_string("input.txt").expect("Could not read file");
let instructions = input
.lines()
.map(|l| {
if l.starts_with("swap position") {
let mut params = l.split_ascii_whitespace();
let x = params.nth(2).unwrap().parse::<usize>().unwrap();
let y = params.next_back().unwrap().parse::<usize>().unwrap();
Instruction::SwapPosition(x, y)
} else if l.starts_with("swap letter") {
let mut params = l.split_ascii_whitespace();
let x = params.nth(2).unwrap().bytes().next().unwrap();
let y = params.next_back().unwrap().bytes().next().unwrap();
Instruction::SwapLetter(x, y)
} else if l.starts_with("rotate left") {
let mut params = l.split_ascii_whitespace();
let x = params.nth(2).unwrap().parse::<usize>().unwrap();
Instruction::RotateLeft(x)
} else if l.starts_with("rotate right") {
let mut params = l.split_ascii_whitespace();
let x = params.nth(2).unwrap().parse::<usize>().unwrap();
Instruction::RotateRight(x)
} else if l.starts_with("rotate based on position of letter") {
let mut params = l.split_ascii_whitespace();
let x = params.next_back().unwrap().bytes().next().unwrap();
Instruction::RotateBasedOnLetter(x)
} else if l.starts_with("reverse positions") {
let mut params = l.split_ascii_whitespace();
let x = params.nth(2).unwrap().parse::<usize>().unwrap();
let y = params.next_back().unwrap().parse::<usize>().unwrap();
Instruction::ReversePositions(x, y)
} else if l.starts_with("move position") {
let mut params = l.split_ascii_whitespace();
let x = params.nth(2).unwrap().parse::<usize>().unwrap();
let y = params.next_back().unwrap().parse::<usize>().unwrap();
Instruction::MovePosition(x, y)
} else {
panic!("Unknown instruction: {l}");
}
})
.collect::<Vec<_>>();
for part1 in [true, false] {
let mut instructions = instructions.clone();
let mut s = if part1 {
b"abcdefgh".to_vec()
} else {
b"fbgdceah".to_vec()
};
if !part1 {
instructions.reverse();
}
for i in instructions {
match i {
Instruction::SwapPosition(x, y) => {
s.swap(x, y);
}
Instruction::SwapLetter(x, y) => {
s = s
.into_iter()
.map(|c| {
if c == x {
b'#'
} else if c == y {
x
} else {
c
}
})
.map(|c| if c == b'#' { y } else { c })
.collect();
}
Instruction::RotateLeft(x) => {
if part1 {
s.rotate_left(x);
} else {
s.rotate_right(x);
}
}
Instruction::RotateRight(x) => {
if part1 {
s.rotate_right(x);
} else {
s.rotate_left(x);
}
}
Instruction::RotateBasedOnLetter(x) => {
if part1 {
let idx = s.iter().position(|c| *c == x).unwrap();
let len = s.len();
s.rotate_right((1 + idx + (if idx >= 4 { 1 } else { 0 })) % len);
} else {
let mut cl = s.clone();
loop {
cl.rotate_left(1);
let mut cl2 = cl.clone();
let idx = cl2.iter().position(|c| *c == x).unwrap();
let len = cl2.len();
cl2.rotate_right((1 + idx + (if idx >= 4 { 1 } else { 0 })) % len);
if cl2 == s {
s = cl;
break;
}
}
}
}
Instruction::ReversePositions(mut x, mut y) => {
while x < y {
s.swap(x, y);
x += 1;
y -= 1;
}
}
Instruction::MovePosition(x, y) => {
if part1 {
let c = s.remove(x);
s.insert(y, c);
} else {
let c = s.remove(y);
s.insert(x, c);
}
}
}
}
println!("{}", s.into_iter().map(|b| b as char).collect::<String>());
}
}