-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
221 lines (195 loc) · 5.57 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
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> String {
let mut t = Tubes::new(data);
t.follow_tube();
t.found_letters()
}
fn part_two(data: &str) -> u32 {
let mut t = Tubes::new(data);
t.follow_tube();
t.distance
}
const VERTICAL: u8 = b'|';
const HORIZONTAL: u8 = b'-';
const CORNER: u8 = b'+';
#[derive(Debug)]
struct Tubes<'a> {
data: &'a [u8],
row_length: usize,
location: Location,
letters: [Option<u8>; 20],
at_end: bool,
distance: u32,
}
impl<'a> Tubes<'a> {
fn new(data: &'a str) -> Self {
let data = data.as_bytes();
let row_length = data.iter().position(|&b| b == b'\n').unwrap() + 1;
let start = data.iter().position(|&b| b == b'|').unwrap();
Self {
data,
row_length,
location: Location::new(start, 0),
letters: [None; 20],
at_end: false,
distance: 0,
}
}
fn follow_tube(&mut self) {
while !self.at_end {
self.step();
}
}
fn step(&mut self) {
let mut next = self.location.step();
match (self.get_val(next), next.direction) {
(Some(VERTICAL | HORIZONTAL), _) => {} // nothing to do
(Some(CORNER), Direction::Up | Direction::Down) => {
// Turn right / left
let left = self.get_val(next.turn(Direction::Left).step());
let right = self.get_val(next.turn(Direction::Right).step());
match (left, right) {
(Some(HORIZONTAL), _) => next = next.turn(Direction::Left),
(Some(ch), _) if ch.is_ascii_uppercase() => next = next.turn(Direction::Left),
(_, Some(HORIZONTAL)) => next = next.turn(Direction::Right),
(_, Some(ch)) if ch.is_ascii_uppercase() => next = next.turn(Direction::Right),
(_, _) => unreachable!(),
}
}
(Some(CORNER), Direction::Left | Direction::Right) => {
// Turn up / down
let up = self.get_val(next.turn(Direction::Up).step());
let down = self.get_val(next.turn(Direction::Down).step());
match (up, down) {
(Some(VERTICAL), _) => next = next.turn(Direction::Up),
(Some(ch), _) if ch.is_ascii_uppercase() => next = next.turn(Direction::Up),
(_, Some(VERTICAL)) => next = next.turn(Direction::Down),
(_, Some(ch)) if ch.is_ascii_uppercase() => next = next.turn(Direction::Down),
(_, _) => unreachable!(),
}
}
(Some(ch), _) if ch.is_ascii_uppercase() => {
// record the letter
self.add_letter(ch);
}
(_, _) => {
// At end of tube
self.at_end = true;
}
}
self.location = next;
self.distance += 1;
}
fn add_letter(&mut self, letter: u8) {
if let Some(index) = self.letters.iter().position(Option::is_none) {
self.letters[index] = Some(letter);
}
}
const fn get_val(&self, loc: Location) -> Option<u8> {
let index = loc.point.y * (self.row_length) + loc.point.x;
if index >= self.data.len() {
return None;
}
Some(self.data[index])
}
fn found_letters(&self) -> String {
let letter_vec = self.letters.iter().filter_map(|&l| l).collect();
String::from_utf8(letter_vec).unwrap()
}
}
#[derive(Clone, Copy, Debug)]
struct Location {
point: Point,
direction: Direction,
}
impl Location {
fn new(x: usize, y: usize) -> Self {
Self {
point: Point::new(x, y),
direction: Direction::default(),
}
}
const fn step(&self) -> Self {
Self {
point: self.point.step(self.direction),
direction: self.direction,
}
}
const fn turn(&self, dir: Direction) -> Self {
Self {
point: self.point,
direction: dir,
}
}
}
#[derive(Clone, Copy, Debug)]
struct Point {
x: usize,
y: usize,
}
impl Point {
const fn new(x: usize, y: usize) -> Self {
Self { x, y }
}
const fn step(&self, dir: Direction) -> Self {
match dir {
Direction::Down => Self {
x: self.x,
y: self.y + 1,
},
Direction::Up => Self {
x: self.x,
y: self.y.saturating_sub(1),
},
Direction::Right => Self {
x: self.x + 1,
y: self.y,
},
Direction::Left => Self {
x: self.x.saturating_sub(1),
y: self.y,
},
}
}
}
impl Default for Point {
fn default() -> Self {
Self::new(0, 0)
}
}
#[derive(Clone, Copy, Debug)]
enum Direction {
Up,
Down,
Left,
Right,
}
impl Direction {
const fn new() -> Self {
Self::Down
}
}
impl Default for Direction {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!("ABCDEF", part_one(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(38, part_two(data));
}
}