Skip to content

Commit 61c7596

Browse files
committed
Rename Grid "default_copy" to "same_size_with" and allow any copy value
1 parent c7ac2d0 commit 61c7596

File tree

7 files changed

+25
-30
lines changed

7 files changed

+25
-30
lines changed

src/util/grid.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
//! ```
1818
//!
1919
//! A convenience [`parse`] method creates a `Grid` directly from a 2 dimenionsal set of
20-
//! ASCII characters, a common occurence in Advent of Code inputs. The [`default_copy`] function
20+
//! ASCII characters, a common occurence in Advent of Code inputs. The [`same_size_with`] function
2121
//! creates a grid of the same size, that can be used for in BFS algorithms for tracking visited
2222
//! location or for tracking cost in Djikstra.
2323
//!
2424
//! [`Point`]: crate::util::point
2525
//! [`parse`]: Grid::parse
26-
//! [`default_copy`]: Grid::default_copy
26+
//! [`same_size_with`]: Grid::same_size_with
2727
use crate::util::point::*;
2828
use std::ops::{Index, IndexMut};
2929

@@ -35,6 +35,7 @@ pub struct Grid<T> {
3535
}
3636

3737
impl Grid<u8> {
38+
#[inline]
3839
pub fn parse(input: &str) -> Self {
3940
let raw: Vec<_> = input.lines().map(str::as_bytes).collect();
4041
let width = raw[0].len() as i32;
@@ -46,6 +47,7 @@ impl Grid<u8> {
4647
}
4748

4849
impl<T: Copy + PartialEq> Grid<T> {
50+
#[inline]
4951
pub fn find(&self, needle: T) -> Option<Point> {
5052
let to_point = |index| {
5153
let x = (index as i32) % self.width;
@@ -63,11 +65,12 @@ impl<T: Copy> Grid<T> {
6365
}
6466

6567
impl<T> Grid<T> {
66-
pub fn default_copy<U: Default + Copy>(&self) -> Grid<U> {
68+
#[inline]
69+
pub fn same_size_with<U: Copy>(&self, value: U) -> Grid<U> {
6770
Grid {
6871
width: self.width,
6972
height: self.height,
70-
bytes: vec![U::default(); (self.width * self.height) as usize],
73+
bytes: vec![value; (self.width * self.height) as usize],
7174
}
7275
}
7376

src/year2018/day13.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub fn parse(input: &str) -> Input {
8686

8787
pub fn part1(input: &Input) -> String {
8888
let mut carts = input.carts.clone();
89-
let mut occupied = input.grid.default_copy();
89+
let mut occupied = input.grid.same_size_with(false);
9090

9191
loop {
9292
// Turn order is important.
@@ -109,7 +109,7 @@ pub fn part1(input: &Input) -> String {
109109

110110
pub fn part2(input: &Input) -> String {
111111
let mut carts = input.carts.clone();
112-
let mut occupied = input.grid.default_copy();
112+
let mut occupied = input.grid.same_size_with(false);
113113

114114
while carts.len() > 1 {
115115
// Turn order is important.

src/year2022/day12.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn part2(input: &Input) -> u32 {
4646
fn bfs(input: &Input, end: u8) -> u32 {
4747
let (grid, start) = input;
4848
let mut todo = VecDeque::from([(*start, 0)]);
49-
let mut visited = grid.default_copy::<bool>();
49+
let mut visited = grid.same_size_with(false);
5050

5151
while let Some((point, cost)) = todo.pop_front() {
5252
if grid[point] == end {

src/year2023/day03.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn parse(input: &str) -> Input {
1414
// In order to tell if we've already seen a number before, store its index at the location
1515
// of every digit, using zero to indicate no value. For example:
1616
// `467..114..` => `1110022200`
17-
let mut seen: Grid<usize> = grid.default_copy();
17+
let mut seen: Grid<usize> = grid.same_size_with(0);
1818
// Stores each unique part number. The first value is a dummy placeholder.
1919
let mut parts = vec![0];
2020
let mut number = 0;

src/year2023/day14.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ pub fn parse(input: &str) -> Input {
100100
}
101101

102102
let mut rounded = Vec::new();
103-
let mut north = grid.default_copy();
104-
let mut west = grid.default_copy();
105-
let mut south = grid.default_copy();
106-
let mut east = grid.default_copy();
103+
let mut north = grid.same_size_with(0);
104+
let mut west = grid.same_size_with(0);
105+
let mut south = grid.same_size_with(0);
106+
let mut east = grid.same_size_with(0);
107107
let mut roll_north = Vec::new();
108108
let mut roll_west = Vec::new();
109109
let mut roll_south = Vec::new();

src/year2023/day16.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ struct Shared<'a> {
4444
pub fn parse(input: &str) -> Input {
4545
let grid = Grid::parse(input);
4646

47-
let mut up: Grid<i32> = grid.default_copy();
48-
let mut down: Grid<i32> = grid.default_copy();
49-
let mut left: Grid<i32> = grid.default_copy();
50-
let mut right: Grid<i32> = grid.default_copy();
47+
let mut up: Grid<i32> = grid.same_size_with(0);
48+
let mut down: Grid<i32> = grid.same_size_with(0);
49+
let mut left: Grid<i32> = grid.same_size_with(0);
50+
let mut right: Grid<i32> = grid.same_size_with(0);
5151

5252
for x in 0..grid.width {
5353
let mut last = -1;
@@ -151,8 +151,8 @@ fn count(input: &Input, start: Pair) -> usize {
151151
let Input { grid, up, down, left, right } = input;
152152

153153
let mut todo = VecDeque::with_capacity(1_000);
154-
let mut seen: Grid<u8> = grid.default_copy();
155-
let mut energized: Grid<bool> = grid.default_copy();
154+
let mut seen: Grid<u8> = grid.same_size_with(0);
155+
let mut energized: Grid<bool> = grid.same_size_with(false);
156156

157157
todo.push_back(start);
158158

src/year2024/day06.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ struct Shortcut {
161161

162162
impl Shortcut {
163163
fn from(grid: &Grid<u8>) -> Self {
164-
let mut up = copy(grid);
165-
let mut down = copy(grid);
166-
let mut left = copy(grid);
167-
let mut right = copy(grid);
164+
let mut up = grid.same_size_with(ORIGIN);
165+
let mut down = grid.same_size_with(ORIGIN);
166+
let mut left = grid.same_size_with(ORIGIN);
167+
let mut right = grid.same_size_with(ORIGIN);
168168

169169
for x in 0..grid.width {
170170
let mut last = Point::new(x, -1);
@@ -217,11 +217,3 @@ impl Shortcut {
217217
Shortcut { up, down, left, right }
218218
}
219219
}
220-
221-
fn copy(grid: &Grid<u8>) -> Grid<Point> {
222-
Grid {
223-
width: grid.width,
224-
height: grid.height,
225-
bytes: vec![ORIGIN; (grid.width * grid.height) as usize],
226-
}
227-
}

0 commit comments

Comments
 (0)