Skip to content

Commit 45a2125

Browse files
committed
Use last years grid lib
1 parent b532f01 commit 45a2125

File tree

10 files changed

+949
-0
lines changed

10 files changed

+949
-0
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ chrono = { version = "0.4.38", optional = true }
2626
dhat = { version = "0.3.3", optional = true }
2727
heapless = "0.8.0"
2828
itertools = "0.13.0"
29+
mygrid = { version = "0.0.1", path = "mygrid" }
2930
pico-args = "0.5.0"
3031
rayon = "1.10.0"
3132
regex = "1.11.1"
3233
tinyjson = "2.5.1"
3334

35+
3436
# Solution dependencies

data/examples/04.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
MMMSXXMASM
2+
MSAMXMSMSA
3+
AMXSXMAAMM
4+
MSAMASMSMX
5+
XMASAMXAMM
6+
XXAMMXXAMA
7+
SMSMSASXSS
8+
SAXAMASAAA
9+
MAMMMXMMMM
10+
MXMXAXMASX

mygrid/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mygrid/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "mygrid"
3+
version = "0.0.1"
4+
publish = false
5+
6+
[lib]
7+
name = "mygrid"
8+
test = true
9+
plugin = false

mygrid/src/direction.rs

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
use std::hash::{Hash, Hasher};
2+
use std::ops::{Add, Mul};
3+
4+
use crate::point::Point;
5+
6+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
7+
pub struct Direction {
8+
pub vertical: isize,
9+
pub horizontal: isize,
10+
}
11+
12+
pub const UP: Direction = Direction::new(-1, 0);
13+
pub const DOWN: Direction = Direction::new(1, 0);
14+
pub const LEFT: Direction = Direction::new(0, -1);
15+
pub const RIGHT: Direction = Direction::new(0, 1);
16+
pub const ORTHOGONAL: [Direction; 4] = [UP, DOWN, LEFT, RIGHT];
17+
pub const ALL_AROUND: [Direction; 8] = [
18+
UP,
19+
UP.add_direction(&RIGHT),
20+
RIGHT,
21+
RIGHT.add_direction(&DOWN),
22+
DOWN,
23+
DOWN.add_direction(&LEFT),
24+
LEFT,
25+
LEFT.add_direction(&UP),
26+
];
27+
28+
impl Direction {
29+
#[inline]
30+
pub const fn new(vertical: isize, horizontal: isize) -> Self {
31+
Direction {
32+
vertical,
33+
horizontal,
34+
}
35+
}
36+
37+
#[inline]
38+
pub const fn new_i32(vertical: i32, horizontal: i32) -> Self {
39+
Direction::new(vertical as isize, horizontal as isize)
40+
}
41+
42+
#[inline]
43+
pub fn rotate_clockwise(&self) -> Self {
44+
Direction::new(self.horizontal, -self.vertical)
45+
}
46+
47+
#[inline]
48+
pub fn rotate_clockwise_mut(&mut self) {
49+
(self.horizontal, self.vertical) = (-self.vertical, self.horizontal);
50+
}
51+
52+
#[inline]
53+
pub fn rotate_counterclockwise(&self) -> Self {
54+
Direction::new(-self.horizontal, self.vertical)
55+
}
56+
57+
#[inline]
58+
pub fn rotate_counterclockwise_mut(&mut self) {
59+
(self.horizontal, self.vertical) = (self.vertical, -self.horizontal);
60+
}
61+
62+
#[inline]
63+
pub fn reverse(&self) -> Self {
64+
Direction::new(-self.vertical, -self.horizontal)
65+
}
66+
67+
#[inline]
68+
pub fn reverse_mut(&mut self) {
69+
(self.horizontal, self.vertical) = (-self.horizontal, -self.vertical);
70+
}
71+
72+
#[inline]
73+
pub fn is_opposite(&self, other: &Direction) -> bool {
74+
self.vertical == -other.vertical && self.horizontal == -other.horizontal
75+
}
76+
77+
#[inline]
78+
pub fn is_orthogonal(&self, other: &Direction) -> bool {
79+
self.vertical == 0 && other.vertical == 0 || self.horizontal == 0 && other.horizontal == 0
80+
}
81+
82+
#[inline]
83+
pub const fn add_direction(&self, other: &Direction) -> Self {
84+
Direction::new(
85+
self.vertical + other.vertical,
86+
self.horizontal + other.horizontal,
87+
)
88+
}
89+
90+
#[inline]
91+
pub fn add_direction_mut(&mut self, other: &Direction) {
92+
self.vertical += other.vertical;
93+
self.horizontal += other.horizontal;
94+
}
95+
}
96+
97+
impl Hash for Direction {
98+
#[inline]
99+
fn hash<H: Hasher>(&self, hasher: &mut H) {
100+
hasher.write_isize(self.vertical);
101+
hasher.write_isize(self.horizontal);
102+
}
103+
}
104+
105+
impl From<char> for Direction {
106+
#[inline]
107+
fn from(value: char) -> Self {
108+
match value {
109+
'^' | 'U' => UP,
110+
'v' | 'D' => DOWN,
111+
'<' | 'L' => LEFT,
112+
'>' | 'R' => RIGHT,
113+
_ => unreachable!(),
114+
}
115+
}
116+
}
117+
118+
impl From<&str> for Direction {
119+
#[inline]
120+
fn from(value: &str) -> Self {
121+
Direction::from(value.chars().next().unwrap())
122+
}
123+
}
124+
125+
impl std::fmt::Display for Direction {
126+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127+
match *self {
128+
UP => write!(f, "^"),
129+
DOWN => write!(f, "v"),
130+
LEFT => write!(f, "<"),
131+
RIGHT => write!(f, ">"),
132+
_ => unreachable!(),
133+
}
134+
}
135+
}
136+
137+
impl Add<Direction> for Point {
138+
type Output = Point;
139+
140+
#[inline]
141+
fn add(self, rhs: Direction) -> Self::Output {
142+
self.apply_direction(rhs)
143+
}
144+
}
145+
146+
impl Add<Direction> for Direction {
147+
type Output = Direction;
148+
149+
#[inline]
150+
fn add(self, rhs: Direction) -> Self::Output {
151+
Direction::new(
152+
self.vertical + rhs.vertical,
153+
self.horizontal + rhs.horizontal,
154+
)
155+
}
156+
}
157+
158+
impl Mul<isize> for Direction {
159+
type Output = Direction;
160+
161+
#[inline]
162+
fn mul(self, rhs: isize) -> Self::Output {
163+
Direction::new(self.vertical * rhs, self.horizontal * rhs)
164+
}
165+
}
166+
167+
impl Mul<i32> for Direction {
168+
type Output = Direction;
169+
170+
#[inline]
171+
fn mul(self, rhs: i32) -> Self::Output {
172+
self * (rhs as isize)
173+
}
174+
}
175+
176+
#[cfg(test)]
177+
mod tests {
178+
use super::*;
179+
use crate::point::Point;
180+
181+
#[test]
182+
pub fn test_direction() {
183+
let direction = Direction::new(1, 0);
184+
assert_eq!(direction.vertical, 1);
185+
assert_eq!(direction.horizontal, 0);
186+
}
187+
188+
#[test]
189+
pub fn test_point_apply_direction() {
190+
let point = Point::new(1, 2);
191+
let direction = Direction::new(1, 0);
192+
let new_point = point.apply_direction(direction);
193+
assert_eq!(new_point.line, 2);
194+
assert_eq!(new_point.column, 2);
195+
196+
let new_point = point + direction;
197+
assert_eq!(new_point.line, 2);
198+
assert_eq!(new_point.column, 2);
199+
}
200+
201+
#[test]
202+
pub fn test_direction_rotate() {
203+
let direction = Direction::new(1, 0);
204+
let new_direction = direction.rotate_clockwise();
205+
assert_eq!(new_direction.vertical, 0);
206+
assert_eq!(new_direction.horizontal, -1);
207+
208+
let new_direction = direction.rotate_counterclockwise();
209+
assert_eq!(new_direction.vertical, 0);
210+
assert_eq!(new_direction.horizontal, 1);
211+
212+
let mut direction = Direction::new(1, 0);
213+
direction.rotate_clockwise_mut();
214+
assert_eq!(direction.vertical, 0);
215+
assert_eq!(direction.horizontal, -1);
216+
217+
let mut direction = Direction::new(1, 0);
218+
direction.rotate_counterclockwise_mut();
219+
assert_eq!(direction.vertical, 0);
220+
assert_eq!(direction.horizontal, 1);
221+
}
222+
223+
#[test]
224+
pub fn test_direction_from_char() {
225+
let direction = Direction::from('U');
226+
assert_eq!(direction.vertical, -1);
227+
assert_eq!(direction.horizontal, 0);
228+
229+
let direction = Direction::from('D');
230+
assert_eq!(direction.vertical, 1);
231+
assert_eq!(direction.horizontal, 0);
232+
233+
let direction = Direction::from('L');
234+
assert_eq!(direction.vertical, 0);
235+
assert_eq!(direction.horizontal, -1);
236+
237+
let direction = Direction::from('R');
238+
assert_eq!(direction.vertical, 0);
239+
assert_eq!(direction.horizontal, 1);
240+
}
241+
242+
#[test]
243+
pub fn test_direction_mult() {
244+
let direction = Direction::new(2, 0);
245+
let new_direction: Direction = direction * 2;
246+
assert_eq!(new_direction.vertical, 4);
247+
assert_eq!(new_direction.horizontal, 0);
248+
}
249+
}

0 commit comments

Comments
 (0)