From a74cd0241329ccab3c66a0afd626e055d726557a Mon Sep 17 00:00:00 2001 From: maneatingape <44142177+maneatingape@users.noreply.github.com> Date: Thu, 31 Aug 2023 20:04:06 +0200 Subject: [PATCH] Refactor Point to implement From --- README.md | 2 +- src/util/point.rs | 19 +++++-------------- src/year2015/day03.rs | 2 +- src/year2019/day03.rs | 4 ++-- src/year2022/day09.rs | 9 +++------ 5 files changed, 12 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 2437203..3907044 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/src/util/point.rs b/src/util/point.rs index 9e6d733..8a9cfe1 100644 --- a/src/util/point.rs +++ b/src/util/point.rs @@ -84,10 +84,10 @@ impl Hash for Point { } } -impl Point { +impl From 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, @@ -95,18 +95,9 @@ impl Point { _ => 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 } diff --git a/src/year2015/day03.rs b/src/year2015/day03.rs index a3bf96a..7584e87 100644 --- a/src/year2015/day03.rs +++ b/src/year2015/day03.rs @@ -10,7 +10,7 @@ use crate::util::hash::*; use crate::util::point::*; pub fn parse(input: &str) -> Vec { - input.trim().bytes().map(Point::from_byte).collect() + input.trim().bytes().map(Point::from).collect() } pub fn part1(input: &[Point]) -> usize { diff --git a/src/year2019/day03.rs b/src/year2019/day03.rs index ae1f190..ab4d795 100644 --- a/src/year2019/day03.rs +++ b/src/year2019/day03.rs @@ -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 }; @@ -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`. diff --git a/src/year2022/day09.rs b/src/year2022/day09.rs index 06dedee..56842ba 100644 --- a/src/year2022/day09.rs +++ b/src/year2022/day09.rs @@ -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::*; @@ -14,11 +13,9 @@ type Input = ([i32; 4], Vec); /// 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::(); + let pairs = first.zip(second).collect(); // Determine maximum extents let mut x1 = i32::MAX;