-
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.
- Loading branch information
1 parent
c04baa1
commit a9b3099
Showing
4 changed files
with
213 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod part1; | ||
pub mod part2; | ||
mod shared; |
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,73 @@ | ||
use std::fmt; | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum Direction { | ||
U, | ||
R, | ||
L, | ||
D, | ||
} | ||
|
||
pub fn parse_moves(input: &str) -> Vec<(Direction, i32)> { | ||
input | ||
.lines() | ||
.map(|line| { | ||
let (dir, step) = line.trim().split_once(' ').unwrap(); | ||
let direction = match dir { | ||
"U" => Direction::U, | ||
"L" => Direction::L, | ||
"R" => Direction::R, | ||
_ => Direction::D, | ||
}; | ||
|
||
(direction, step.parse::<i32>().unwrap()) | ||
}) | ||
.collect() | ||
} | ||
|
||
#[derive(Clone, Copy, PartialEq, Debug)] | ||
pub struct Point { | ||
pub x: i32, | ||
pub y: i32, | ||
} | ||
|
||
impl Point { | ||
pub fn new(x: i32, y: i32) -> Point { | ||
Point { x, y } | ||
} | ||
|
||
pub fn move_to(&mut self, direction: &Direction) { | ||
match direction { | ||
Direction::R => self.x += 1, | ||
Direction::L => self.x -= 1, | ||
Direction::D => self.y += 1, | ||
Direction::U => self.y -= 1, | ||
} | ||
} | ||
|
||
pub fn move_diagonal_to(&mut self, direction_x: &Direction, direction_y: &Direction) { | ||
self.move_to(direction_x); | ||
self.move_to(direction_y); | ||
} | ||
|
||
pub fn gradient(point1: &Point, point2: &Point) -> Point { | ||
Point { | ||
x: point2.x - point1.x, | ||
y: point2.y - point1.y, | ||
} | ||
} | ||
|
||
pub fn distance_to(&self, point: &Point) -> i32 { | ||
(self.x - point.x).abs().max((self.y - point.y).abs()) | ||
} | ||
|
||
pub fn is_adjacent_to(&self, point: &Point) -> bool { | ||
self.distance_to(point) <= 1 | ||
} | ||
} | ||
|
||
impl fmt::Display for Point { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "({}, {})", self.x, self.y) | ||
} | ||
} |