-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.rs
40 lines (34 loc) · 941 Bytes
/
position.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
use crate::instruction::Direction;
use std::{
cmp::Ordering,
fmt::{Debug, Formatter},
};
pub(crate) type Unit = isize;
#[derive(Eq, PartialEq, Clone, Copy)]
pub(crate) struct Position(pub Unit, pub Unit);
impl Debug for Position {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "({},{})", self.0, self.1)
}
}
impl PartialOrd<Self> for Position {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Position {
fn cmp(&self, other: &Self) -> Ordering {
self.1.cmp(&other.1).then_with(|| self.0.cmp(&other.0))
}
}
impl Position {
pub(crate) fn next_mut(&mut self, dir: Direction) -> &mut Self {
match dir {
Direction::U => self.1 -= 1,
Direction::R => self.0 += 1,
Direction::D => self.1 += 1,
Direction::L => self.0 -= 1,
};
self
}
}