generated from clechasseur/rust-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add helpers, clean up, prepare
- Loading branch information
1 parent
5ae3da9
commit e1f6a8c
Showing
16 changed files
with
799 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub fn part_1() -> usize { | ||
0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
|
||
pub mod direction; | ||
pub mod looping; | ||
pub mod pt; | ||
pub mod pt_3d; | ||
pub mod regex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::ops::Neg; | ||
|
||
use num::{one, zero, One, Zero}; | ||
use strum::{Display, EnumCount, FromRepr}; | ||
|
||
use crate::helpers::pt::Pt; | ||
|
||
/// ↓ ↑ ← → | ||
#[repr(u8)] | ||
#[derive( | ||
Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, FromRepr, EnumCount, Display, | ||
)] | ||
pub enum Direction { | ||
Right, | ||
Down, | ||
Left, | ||
Up, | ||
} | ||
|
||
impl Direction { | ||
/// Turns 90 degrees to the left. | ||
pub fn turn_left(&self) -> Self { | ||
Self::from_repr(((*self as u8) + 3) % (Self::COUNT as u8)).unwrap() | ||
} | ||
|
||
/// Turns 90 degrees to the right. | ||
pub fn turn_right(&self) -> Self { | ||
Self::from_repr(((*self as u8) + 1) % (Self::COUNT as u8)).unwrap() | ||
} | ||
|
||
/// Turns around (e.g. performs a 180 degrees turn). | ||
pub fn turn_around(&self) -> Self { | ||
Self::from_repr(((*self as u8) + 2) % (Self::COUNT as u8)).unwrap() | ||
} | ||
|
||
/// Returns the displacement to apply to move one step in this direction. | ||
/// The displacement is returned as a [`Pt`]. | ||
/// | ||
/// # Notes | ||
/// | ||
/// Because this enum is meant to be used to move around a map represented as a series of rows | ||
/// like on a computer screen, `Up`'s displacement will _subtract_ one from the Y axis, while | ||
/// `Down`'s will _add_ one to the Y axis. | ||
pub fn displacement<T>(&self) -> Pt<T> | ||
where | ||
T: Zero + One + Neg<Output = T>, | ||
{ | ||
match self { | ||
Direction::Right => Pt::new(one(), zero()), | ||
Direction::Down => Pt::new(zero(), one()), | ||
Direction::Left => Pt::new(-one::<T>(), zero()), | ||
Direction::Up => Pt::new(zero(), -one::<T>()), | ||
} | ||
} | ||
} |
Oops, something went wrong.