Skip to content

Commit 2235e3e

Browse files
committed
replaced my Array2D with maneatingape (similar logic, easier migration later)
1 parent 16b1a37 commit 2235e3e

File tree

6 files changed

+94
-144
lines changed

6 files changed

+94
-144
lines changed

src/maneatingape/grid.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/72c8feb22030d55d7a08f16c119425b2ba759470/src/util/hash.rs
2+
3+
//! Fast 2 dimensional Grid backed by a single `vec`. This module is designed to work with [`Point`].
4+
//!
5+
//! The traits [`Index`] and [`IndexMut`] are implemented for [`Point`] to allow usage like:
6+
//!
7+
//! ```
8+
//! # use aoc::util::grid::Grid;
9+
//! # use aoc::util::point::Point;
10+
//!
11+
//! let mut grid = Grid::parse("1");
12+
//! let point = Point::new(0, 0);
13+
//!
14+
//! let foo = grid[point];
15+
//! assert_eq!(foo, b'1');
16+
//!
17+
//! grid[point] = foo + 1;
18+
//! assert_eq!(grid[point], b'2');
19+
//! ```
20+
//!
21+
//! A convenience [`parse`] method creates a `Grid` directly from a 2 dimenionsal set of
22+
//! ASCII characters, a common occurence in Advent of Code inputs. The [`default_copy`] function
23+
//! creates a grid of the same size, that can be used for in BFS algorithms for tracking visited
24+
//! location or for tracking cost in Djikstra.
25+
//!
26+
//! [`Point`]: crate::util::point
27+
//! [`parse`]: Grid::parse
28+
//! [`default_copy`]: Grid::default_copy
29+
use crate::maneatingape::point::*;
30+
use std::ops::{Index, IndexMut};
31+
32+
#[derive(Clone, PartialEq, Eq, Hash)]
33+
pub struct Grid<T> {
34+
pub width: i32,
35+
pub height: i32,
36+
pub bytes: Vec<T>,
37+
}
38+
39+
impl Grid<u8> {
40+
pub fn parse(input: &str) -> Self {
41+
let raw: Vec<_> = input.lines().map(str::as_bytes).collect();
42+
let width = raw[0].len() as i32;
43+
let height = raw.len() as i32;
44+
let mut bytes = Vec::with_capacity((width * height) as usize);
45+
raw.iter().for_each(|slice| bytes.extend_from_slice(slice));
46+
Grid {
47+
width,
48+
height,
49+
bytes,
50+
}
51+
}
52+
}
53+
54+
impl<T: Copy + PartialEq> Grid<T> {
55+
pub fn default_copy<U: Default + Copy>(&self) -> Grid<U> {
56+
Grid {
57+
width: self.width,
58+
height: self.height,
59+
bytes: vec![U::default(); (self.width * self.height) as usize],
60+
}
61+
}
62+
63+
pub fn find(&self, needle: T) -> Option<Point> {
64+
let to_point = |index| {
65+
let x = (index as i32) % self.width;
66+
let y = (index as i32) / self.width;
67+
Point::new(x, y)
68+
};
69+
self.bytes.iter().position(|&h| h == needle).map(to_point)
70+
}
71+
72+
#[inline]
73+
pub fn contains(&self, point: Point) -> bool {
74+
point.x >= 0 && point.x < self.width && point.y >= 0 && point.y < self.height
75+
}
76+
}
77+
78+
impl<T> Index<Point> for Grid<T> {
79+
type Output = T;
80+
81+
#[inline]
82+
fn index(&self, point: Point) -> &Self::Output {
83+
&self.bytes[(self.width * point.y + point.x) as usize]
84+
}
85+
}
86+
87+
impl<T> IndexMut<Point> for Grid<T> {
88+
#[inline]
89+
fn index_mut(&mut self, point: Point) -> &mut Self::Output {
90+
&mut self.bytes[(self.width * point.y + point.x) as usize]
91+
}
92+
}

src/maneatingape/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,4 @@ impl<I: Iterator> Iterator for Chunk<I, 8> {
117117
let h = self.iter.next()?;
118118
Some([a, b, c, d, e, f, g, h])
119119
}
120-
}
120+
}

src/maneatingape/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod grid;
12
pub mod hash;
23
pub mod heap;
34
pub mod integer;

src/util/grid.rs

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/util/list.rs

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/util/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
pub mod bignumbers;
2-
pub mod grid;
3-
pub mod list;

0 commit comments

Comments
 (0)