Skip to content

Commit

Permalink
2019: Rename Coordinate to Point
Browse files Browse the repository at this point in the history
Point is the proper term where as x and y are coordinates.
  • Loading branch information
ericvw committed Apr 22, 2024
1 parent b66c166 commit ea3f3a2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
16 changes: 8 additions & 8 deletions 2019/src/bin/puzzle_03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashSet;
use std::convert::From;
use std::io;

use aoc2019::grid::Coordinate;
use aoc2019::grid::Point;

#[derive(Copy, Clone)]
enum Direction {
Expand All @@ -30,10 +30,10 @@ struct Vector {
magnitude: u32,
}

fn trace_path(path: &Vec<Vector>) -> HashMap<Coordinate, u32> {
fn trace_path(path: &Vec<Vector>) -> HashMap<Point, u32> {
let mut trace = HashMap::new();

let mut c = Coordinate(0, 0);
let mut c = Point { x: 0, y: 0 };
let mut len = 0;

for &Vector {
Expand All @@ -42,10 +42,10 @@ fn trace_path(path: &Vec<Vector>) -> HashMap<Coordinate, u32> {
} in path
{
let step = match direction {
Direction::Up => Coordinate(0, 1),
Direction::Down => Coordinate(0, -1),
Direction::Left => Coordinate(-1, 0),
Direction::Right => Coordinate(1, 0),
Direction::Up => Point { x: 0, y: 1 },
Direction::Down => Point { x: 0, y: -1 },
Direction::Left => Point { x: -1, y: 0 },
Direction::Right => Point { x: 1, y: 0 },
};

for _ in 0..magnitude {
Expand Down Expand Up @@ -86,7 +86,7 @@ fn main() {
"Part 1: {}",
intersections
.clone()
.map(|c| c.manhattan_distance(Coordinate(0, 0)))
.map(|c| c.manhattan_distance(Point { x: 0, y: 0 }))
.min()
.unwrap()
);
Expand Down
18 changes: 12 additions & 6 deletions 2019/src/grid.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
use std::ops::Add;

#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub struct Coordinate(pub i32, pub i32);
pub struct Point {
pub x: i32,
pub y: i32,
}

impl Coordinate {
pub fn manhattan_distance(&self, Coordinate(x, y): Coordinate) -> u32 {
x.abs_diff(self.0) + y.abs_diff(self.1)
impl Point {
pub fn manhattan_distance(&self, other: Self) -> u32 {
self.x.abs_diff(other.x) + self.y.abs_diff(other.y)
}
}

impl Add for Coordinate {
impl Add for Point {
type Output = Self;

fn add(self, other: Self) -> Self {
Self(self.0 + other.0, self.1 + other.1)
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}

0 comments on commit ea3f3a2

Please sign in to comment.