Skip to content

Commit

Permalink
Refactor Point to implement From
Browse files Browse the repository at this point in the history
  • Loading branch information
maneatingape committed Aug 31, 2023
1 parent a65e8e1 commit a74cd02
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pie
| 6 | [Tuning Trouble](https://adventofcode.com/2022/day/6) | [Source](src/year2022/day06.rs) | 6 |
| 7 | [No Space Left On Device](https://adventofcode.com/2022/day/7) | [Source](src/year2022/day07.rs) | 10 |
| 8 | [Treetop Tree House](https://adventofcode.com/2022/day/8) | [Source](src/year2022/day08.rs) | 55 |
| 9 | [Rope Bridge](https://adventofcode.com/2022/day/9) | [Source](src/year2022/day09.rs) | 115 |
| 9 | [Rope Bridge](https://adventofcode.com/2022/day/9) | [Source](src/year2022/day09.rs) | 122 |
| 10 | [Cathode-Ray Tube](https://adventofcode.com/2022/day/10) | [Source](src/year2022/day10.rs) | 2 |
| 11 | [Monkey in the Middle](https://adventofcode.com/2022/day/11) | [Source](src/year2022/day11.rs) | 2386 |
| 12 | [Hill Climbing Algorithm](https://adventofcode.com/2022/day/12) | [Source](src/year2022/day12.rs) | 59 |
Expand Down
19 changes: 5 additions & 14 deletions src/util/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,20 @@ impl Hash for Point {
}
}

impl Point {
impl From<u8> for Point {
#[inline]
pub fn from_byte(b: u8) -> Point {
match b {
fn from(value: u8) -> Self {
match value {
b'^' | b'U' => UP,
b'v' | b'D' => DOWN,
b'<' | b'L' => LEFT,
b'>' | b'R' => RIGHT,
_ => unreachable!(),
}
}
}

#[inline]
pub fn from_string(s: &str) -> Point {
match s {
"U" => UP,
"D" => DOWN,
"L" => LEFT,
"R" => RIGHT,
_ => unreachable!(),
}
}

impl Point {
#[inline]
pub fn clockwise(self) -> Point {
Point { x: -self.y, y: self.x }
Expand Down
2 changes: 1 addition & 1 deletion src/year2015/day03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::util::hash::*;
use crate::util::point::*;

pub fn parse(input: &str) -> Vec<Point> {
input.trim().bytes().map(Point::from_byte).collect()
input.trim().bytes().map(Point::from).collect()
}

pub fn part1(input: &[Point]) -> usize {
Expand Down
4 changes: 2 additions & 2 deletions src/year2019/day03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn parse(input: &str) -> Input {
let mut horizontal = BTreeMap::new();

for (direction, amount) in steps(0) {
let delta = Point::from_byte(direction);
let delta = Point::from(direction);
let end = start + delta * amount;
let line = Line { start, end, distance };

Expand All @@ -65,7 +65,7 @@ pub fn parse(input: &str) -> Input {
let mut delay = i32::MAX;

for (direction, amount) in steps(1) {
let delta = Point::from_byte(direction);
let delta = Point::from(direction);
let end = start + delta * amount;

// Use a block to scope the `update` lamdbas mutable borrow of `distance`.
Expand Down
9 changes: 3 additions & 6 deletions src/year2022/day09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! This solution relies on the [`Point`] utility class. Two dimensional problems are common in
//! Advent of Code, so having a decent `Point` (or `Coord` or `Pos`) class in your back pocket
//! is handy.
use crate::util::iter::*;
use crate::util::parse::*;
use crate::util::point::*;

Expand All @@ -14,11 +13,9 @@ type Input = ([i32; 4], Vec<Pair>);
/// magnitude respectively. Then determines the maximum extent of the head so that we can allocate
/// a two dimensional grid.
pub fn parse(input: &str) -> Input {
let pairs: Vec<_> = input
.split_ascii_whitespace()
.chunk::<2>()
.map(|[d, n]| (Point::from_string(d), n.signed()))
.collect();
let first = input.bytes().filter(u8::is_ascii_alphabetic).map(Point::from);
let second = input.iter_signed::<i32>();
let pairs = first.zip(second).collect();

// Determine maximum extents
let mut x1 = i32::MAX;
Expand Down

0 comments on commit a74cd02

Please sign in to comment.