Skip to content

Commit f52b72f

Browse files
committed
refactor(01/2016): improve readability
1 parent 6000e3f commit f52b72f

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

src/solutions/year2016/day01.rs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::solutions::Solution;
22
use crate::utils::direction::Direction;
33
use crate::utils::point::Point;
4+
use crate::utils::rotation::Rotation;
45
use crate::utils::vector::Vector;
56
use std::collections::HashSet;
67

@@ -12,12 +13,7 @@ impl Solution for Day01 {
1213
let mut vector = Vector::new(start, Direction::North);
1314

1415
self.parse(input.trim()).for_each(|(rotation, distance)| {
15-
vector = match rotation {
16-
RotationDirection::Right => vector.rotate_cw(),
17-
RotationDirection::Left => vector.rotate_ccw(),
18-
};
19-
20-
vector = vector.forward_with_length(distance);
16+
vector = vector.rotate(rotation).forward_with_length(distance);
2117
});
2218

2319
vector.position().manhattan_distance(&start).to_string()
@@ -31,38 +27,27 @@ impl Solution for Day01 {
3127
visited.insert(start);
3228

3329
'outer: for (rotation, distance) in self.parse(input.trim()) {
34-
vector = match rotation {
35-
RotationDirection::Right => vector.rotate_cw(),
36-
RotationDirection::Left => vector.rotate_ccw(),
37-
};
30+
vector = vector.rotate(rotation);
3831

3932
for _ in 0..distance {
4033
vector = vector.forward();
4134

42-
let position = vector.position();
43-
if visited.contains(&position) {
35+
if !visited.insert(vector.position()) {
4436
break 'outer;
4537
}
46-
47-
visited.insert(position);
4838
}
4939
}
5040

5141
vector.position().manhattan_distance(&start).to_string()
5242
}
5343
}
5444

55-
enum RotationDirection {
56-
Right,
57-
Left,
58-
}
59-
6045
impl Day01 {
61-
fn parse<'a>(&self, input: &'a str) -> impl Iterator<Item = (RotationDirection, isize)> + 'a {
46+
fn parse<'a>(&self, input: &'a str) -> impl Iterator<Item = (Rotation, isize)> + 'a {
6247
input.split(", ").map(|instruction| {
6348
let rotation = match &instruction[0..1] {
64-
"R" => RotationDirection::Right,
65-
"L" => RotationDirection::Left,
49+
"R" => Rotation::Clockwise,
50+
"L" => Rotation::CounterClockwise,
6651
_ => unreachable!(),
6752
};
6853

src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod point;
1515
pub mod point3d;
1616
pub mod polygon;
1717
pub mod range;
18+
pub mod rotation;
1819
pub mod shoelace_formula;
1920
pub mod surface_range;
2021
pub mod traits;

src/utils/rotation.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub enum Rotation {
2+
Clockwise,
3+
CounterClockwise,
4+
}

src/utils/vector.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::utils::direction::Direction;
22
use crate::utils::point::Point;
3+
use crate::utils::rotation::Rotation;
34
use std::fmt::{Display, Formatter};
45

56
#[derive(Debug, PartialEq, Eq, Clone, Hash, Copy, PartialOrd, Ord)]
@@ -21,33 +22,48 @@ impl Vector {
2122
self.facing
2223
}
2324

25+
#[inline]
2426
pub fn forward(&self) -> Self {
2527
Self::new(self.position.move_in(self.facing), self.facing)
2628
}
2729

30+
#[inline]
2831
pub fn forward_with_length(&self, length: isize) -> Self {
2932
Self::new(
3033
self.position.move_in_with_length(self.facing, length),
3134
self.facing,
3235
)
3336
}
3437

38+
#[inline]
3539
pub fn backward(&self) -> Self {
3640
Self::new(self.position.move_in(self.facing.opposite()), self.facing)
3741
}
3842

39-
pub fn rotate(&self, facing: Direction) -> Self {
43+
#[inline]
44+
pub fn rotate(&self, rotation: Rotation) -> Self {
45+
match rotation {
46+
Rotation::Clockwise => self.rotate_cw(),
47+
Rotation::CounterClockwise => self.rotate_ccw(),
48+
}
49+
}
50+
51+
#[inline]
52+
pub fn rotate_to_direction(&self, facing: Direction) -> Self {
4053
Self::new(self.position, facing)
4154
}
4255

56+
#[inline]
4357
pub fn rotate_cw(&self) -> Self {
4458
Self::new(self.position, self.facing.cw())
4559
}
4660

61+
#[inline]
4762
pub fn rotate_ccw(&self) -> Self {
4863
Self::new(self.position, self.facing.ccw())
4964
}
5065

66+
#[inline]
5167
pub fn opposite(&self) -> Self {
5268
Self::new(self.position, self.facing.opposite())
5369
}

0 commit comments

Comments
 (0)