From d432d855e650267bf9a3e9fca10d360d6ef64c56 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 20 Dec 2022 09:39:55 -0500 Subject: [PATCH] Simplify shared Grid --- aoc-shared/src/grid.rs | 78 +++++++++++++++++------------------------- day12/src/main.rs | 1 - day8/src/main.rs | 1 - 3 files changed, 32 insertions(+), 48 deletions(-) diff --git a/aoc-shared/src/grid.rs b/aoc-shared/src/grid.rs index f9d28ac..f1c6763 100644 --- a/aoc-shared/src/grid.rs +++ b/aoc-shared/src/grid.rs @@ -6,34 +6,52 @@ pub struct Grid { pub vec: Vec, } -pub trait Grid2d { - fn new(width: usize) -> Self; +impl Grid { + pub fn new(width: usize) -> Self { + Grid { + width, + vec: Vec::new(), + } + } - fn len(&self) -> usize; + pub fn len(&self) -> usize { + self.vec.len() + } - fn num_cols(&self) -> usize; + pub fn num_cols(&self) -> usize { + self.width + } - fn num_rows(&self) -> usize { + pub fn num_rows(&self) -> usize { self.len() / self.num_cols() } // Convert x,y coordinate into linear array index - fn xy_idx(&self, x: usize, y: usize) -> usize { + pub fn xy_idx(&self, x: usize, y: usize) -> usize { (y * self.num_cols()) + x } /// Convert linear array index to x,y coordinate - fn idx_xy(&self, idx: usize) -> (usize, usize) { + pub fn idx_xy(&self, idx: usize) -> (usize, usize) { (idx % self.num_cols(), idx / self.num_cols()) } - fn get(&self, idx: usize) -> Option<&T>; + pub fn get(&self, idx: usize) -> Option<&T> { + self.vec.get(idx) + } - fn get_mut(&mut self, idx: usize) -> Option<&mut T>; + pub fn get_mut(&mut self, idx: usize) -> Option<&mut T> { + self.vec.get_mut(idx) + } - fn get_row(&mut self, row_num: usize) -> &mut [T]; + pub fn get_row(&mut self, row_num: usize) -> &mut [T] { + let start = self.row_first_idx(row_num); + let end = self.row_last_idx(row_num); - fn row_first_idx(&self, row: usize) -> usize { + &mut self.vec[start..=end] + } + + pub fn row_first_idx(&self, row: usize) -> usize { let idx = row * self.num_cols(); if idx < self.len() { @@ -43,7 +61,7 @@ pub trait Grid2d { } } - fn row_last_idx(&self, row: usize) -> usize { + pub fn row_last_idx(&self, row: usize) -> usize { if (row + 1) > self.num_rows() { return self.len(); } @@ -51,11 +69,11 @@ pub trait Grid2d { self.row_first_idx(row + 1) - 1 } - fn get_row_indexes(&self, row_num: usize) -> Vec { + pub fn get_row_indexes(&self, row_num: usize) -> Vec { (self.row_first_idx(row_num)..=self.row_last_idx(row_num)).collect() } - fn get_column_indexes(&self, col_num: usize) -> Vec { + pub fn get_column_indexes(&self, col_num: usize) -> Vec { let mut indexes = Vec::new(); if col_num >= self.num_cols() { @@ -75,38 +93,6 @@ pub trait Grid2d { } } -impl Grid2d for Grid { - fn new(width: usize) -> Self { - Grid { - width, - vec: Vec::new(), - } - } - - fn len(&self) -> usize { - self.vec.len() - } - - fn num_cols(&self) -> usize { - self.width - } - - fn get(&self, idx: usize) -> Option<&T> { - self.vec.get(idx) - } - - fn get_mut(&mut self, idx: usize) -> Option<&mut T> { - self.vec.get_mut(idx) - } - - fn get_row(&mut self, row_num: usize) -> &mut [T] { - let start = self.row_first_idx(row_num); - let end = self.row_last_idx(row_num); - - &mut self.vec[start..=end] - } -} - #[macro_export] /// Simplifies newtype wrapping of the `Grid` struct macro_rules! impl_grid_newtype { diff --git a/day12/src/main.rs b/day12/src/main.rs index d8c5ca9..e41f1e1 100644 --- a/day12/src/main.rs +++ b/day12/src/main.rs @@ -1,6 +1,5 @@ use aoc_shared::enums::Direction; use aoc_shared::grid::Grid as BaseGrid; -use aoc_shared::grid::Grid2d; use aoc_shared::impl_grid_newtype; #[derive(Debug)] diff --git a/day8/src/main.rs b/day8/src/main.rs index be1ba90..853557a 100644 --- a/day8/src/main.rs +++ b/day8/src/main.rs @@ -1,6 +1,5 @@ use aoc_shared::impl_grid_newtype; use aoc_shared::grid::Grid as BaseGrid; -use aoc_shared::grid::Grid2d; use std::collections::HashSet; #[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq, Hash)]