From 02fc1545476972503010c0745f4657d4d418b7c6 Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Sat, 21 Dec 2024 21:01:06 -0800 Subject: [PATCH] grid: use coord2d more consistently --- src/day10.rs | 2 +- src/day16.rs | 15 ++++++++------- src/day20.rs | 2 +- src/day6.rs | 2 +- src/day8.rs | 4 ++-- utils/grid/lib.rs | 27 ++++++++++++++++++++++++--- 6 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/day10.rs b/src/day10.rs index c52214d..8f94402 100644 --- a/src/day10.rs +++ b/src/day10.rs @@ -25,7 +25,7 @@ impl TrailMap { .iter() .enumerate() .filter(|(_, v)| **v == b'0') - .map(|(i, _v)| self.map.coord(i as i64).unwrap()) + .map(|(i, _v)| self.map.coord(i as i64).unwrap().into()) .collect_vec() } fn count_reachable_from(&self, pos: &(i64, i64), needle: u8, visited: &mut Grid) -> u64 { diff --git a/src/day16.rs b/src/day16.rs index 34fed6c..5252f08 100644 --- a/src/day16.rs +++ b/src/day16.rs @@ -1,5 +1,5 @@ use aoc_runner_derive::aoc; -use grid::Grid; +use grid::{Coord2d, Grid}; use std::{ cmp::Ordering, collections::{BinaryHeap, HashMap}, @@ -108,10 +108,10 @@ impl Maze { }) } fn dijkstra(&self) -> usize { - let (start_x, start_y) = self.map.find(&b'S').expect("can't find start"); + let Coord2d {x: start_x, y: start_y} = self.map.find(&b'S').expect("can't find start"); let start = (start_x as CoordType, start_y as CoordType); - let (finish_x, finish_y) = self.map.find(&b'E').expect("can't find finish"); + let Coord2d {x: finish_x, y: finish_y} = self.map.find(&b'E').expect("can't find finish"); let finish = (finish_x as CoordType, finish_y as CoordType); let mut distances = HashMap::new(); @@ -149,10 +149,11 @@ impl Maze { usize::MAX } fn path_dijkstra(&mut self) -> (usize, Vec>) { - let (start_x, start_y) = self.map.find(&b'S').expect("can't find start"); - let start = (start_x.try_into().unwrap(), start_y.try_into().unwrap()); - let (finish_x, finish_y) = self.map.find(&b'E').expect("can't find finish"); - let finish = (finish_x.try_into().unwrap(), finish_y.try_into().unwrap()); + let Coord2d {x: start_x, y: start_y} = self.map.find(&b'S').expect("can't find start"); + let start = (start_x as CoordType, start_y as CoordType); + + let Coord2d {x: finish_x, y: finish_y} = self.map.find(&b'E').expect("can't find finish"); + let finish = (finish_x as CoordType, finish_y as CoordType); let mut distances = HashMap::new(); let mut queue = BinaryHeap::with_capacity(self.map.data.len()); diff --git a/src/day20.rs b/src/day20.rs index b680046..514d38d 100644 --- a/src/day20.rs +++ b/src/day20.rs @@ -118,7 +118,7 @@ fn part1_impl(input: &str, cheat_min: usize) -> i64 { let track = parse(input); let start = track.map.find(&b'S').unwrap(); let goal = track.map.find(&b'E').unwrap(); - let (best_path, costs) = track.path_costs(start, goal); + let (best_path, costs) = track.path_costs(start.into(), goal.into()); let cheats = track.find_cheats(&best_path, &costs, cheat_min); cheats.len() as i64 diff --git a/src/day6.rs b/src/day6.rs index ce318a3..3f68126 100644 --- a/src/day6.rs +++ b/src/day6.rs @@ -104,7 +104,7 @@ impl From for Map { let guard_facing = FacingDirection::Up; Self { grid, - guard_pos, + guard_pos: guard_pos.into(), guard_facing, visited_from, path: Vec::new(), diff --git a/src/day8.rs b/src/day8.rs index 85ad368..1a435b9 100644 --- a/src/day8.rs +++ b/src/day8.rs @@ -42,9 +42,9 @@ impl AntennaMap { // permutations generates both pairs, ie. ((1,2),(2,1)) and ((2,1),(1,2)) so we don't need // to consider the 'negative' side of the line, which will be generated by the other pair let (a, b) = (pair[0], pair[1]); - let offset = (a.0 - b.0, a.1 - b.1); + let offset = (a.x - b.x, a.y - b.y); for i in (start..).map_while(|i| if Some(i - start) != reps { Some(i as i64) } else { None }) { - let node_pos = (a.0 + i * offset.0, a.1 + i * offset.1); + let node_pos = (a.x + i * offset.0, a.y + i * offset.1); if antinodes.set(&node_pos, true).is_none() { // left the grid break; diff --git a/utils/grid/lib.rs b/utils/grid/lib.rs index 92364c3..ff75b74 100644 --- a/utils/grid/lib.rs +++ b/utils/grid/lib.rs @@ -82,6 +82,12 @@ where } } +impl From for (i64, i64) { + fn from(value: Coord2d) -> Self { + (value.x, value.y) + } +} + #[derive(Debug)] pub struct GridRowIter<'a, T> { iter: std::slice::Iter<'a, T>, @@ -164,13 +170,28 @@ impl Grid { pub fn pos(&self, c: &C) -> i64 { c.y() * self.width + c.x() } - pub fn coord(&self, pos: i64) -> Option<(i64, i64)> { + pub fn coord(&self, pos: i64) -> Option { if pos < 0 || pos >= self.data.len() as i64 { None } else { - Some((pos % self.width, pos / self.width)) + Some(Coord2d { + x: pos % self.width, + y: pos / self.width, + }) } } + // pub fn coord_iter(&self) -> CoordIter<_> { + // CoordIter { pos: 0, grid: self } + // } + pub fn is_valid(&self, c: &C) -> bool { + if c.x() < 0 || c.x() >= self.width { + return false; + } + if c.y() < 0 || c.y() as usize >= self.height() { + return false; + } + return true; + } fn valid_pos(&self, c: &C) -> Option { if c.x() < 0 || c.x() >= self.width { return None; @@ -249,7 +270,7 @@ impl Grid { } } - pub fn find(&self, haystack: &T) -> Option<(i64, i64)> { + pub fn find(&self, haystack: &T) -> Option { self.coord( self.data .iter()