Skip to content

Commit 704fc02

Browse files
committed
Remove Grid::neighbors and Grid::neighbors_diag
1 parent e402e6f commit 704fc02

File tree

4 files changed

+12
-108
lines changed

4 files changed

+12
-108
lines changed

src/bin/day10_part1.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ fn floodfill(map: &Grid, start: usize) -> usize {
2727
let next_digit = char::from_digit(digit + 1, 10).unwrap();
2828

2929
queue.extend(
30-
map.neighbors(pos)
30+
map.pos(pos)
31+
.neighbors(map)
32+
.map(|p| p.index())
3133
.filter(|&next| map[next] == next_digit)
3234
.filter(|&next| {
3335
let v = visited[next];

src/bin/day10_part2.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ fn floodfill(map: &Grid, start: usize) -> usize {
2828
let next_digit = char::from_digit(digit + 1, 10).unwrap();
2929

3030
queue.extend(
31-
map.neighbors(pos)
31+
map.pos(pos)
32+
.neighbors(map)
33+
.map(|p| p.index())
3234
.filter(|&next| map[next] == next_digit)
3335
.filter(|&next| {
3436
let v = visited[next];

src/bin/day12_part1.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ fn floodfill(map: &Grid, visited: &mut [bool], start: usize) -> usize {
2828
area += 1;
2929

3030
perimeter += 4 - map
31-
.neighbors(pos)
31+
.pos(pos)
32+
.neighbors(map)
33+
.map(|p| p.index())
3234
.filter(|&next| map[next] == map[start])
3335
.count();
3436
queue.extend(
35-
map.neighbors(pos)
37+
map.pos(pos)
38+
.neighbors(map)
39+
.map(|p| p.index())
3640
.filter(|&next| map[next] == map[start])
3741
.filter(|&next| {
3842
let v = visited[next];

src/grid/mod.rs

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -114,52 +114,6 @@ impl Grid {
114114
.map(|(i, _)| self.pos(i))
115115
}
116116

117-
pub fn neighbors(&self, index: usize) -> impl Neighbors {
118-
let (row, col) = self.coords(index);
119-
NeighborsIterOld([
120-
// north
121-
row.checked_sub(1)
122-
.filter(|&r| self.in_bounds(r, col))
123-
.map(|r| r * self.width + col),
124-
// east
125-
Some((row, col + 1))
126-
.filter(|&(r, c)| self.in_bounds(r, c))
127-
.map(|(r, c)| r * self.width + c),
128-
// south
129-
Some((row + 1, col))
130-
.filter(|&(r, c)| self.in_bounds(r, c))
131-
.map(|(r, c)| r * self.width + c),
132-
// west
133-
col.checked_sub(1)
134-
.filter(|&c| self.in_bounds(row, c))
135-
.map(|c| row * self.width + c),
136-
])
137-
}
138-
139-
/// Returns an Iterator over the 4 diagonal neighbors of a grid location
140-
pub fn neighbors_diag(&self, index: usize) -> impl NeighborsDiag {
141-
let (row, col) = self.coords(index);
142-
NeighborsIterOld([
143-
// north east
144-
row.checked_sub(1)
145-
.filter(|&r| self.in_bounds(r, col + 1))
146-
.map(|r| r * self.width + (col + 1)),
147-
// south east
148-
Some((row + 1, col + 1))
149-
.filter(|&(r, c)| self.in_bounds(r, c))
150-
.map(|(r, c)| r * self.width + c),
151-
// south west
152-
col.checked_sub(1)
153-
.filter(|&c| self.in_bounds(row + 1, c))
154-
.map(|c| (row + 1) * self.width + c),
155-
// north west
156-
row.checked_sub(1)
157-
.zip(col.checked_sub(1))
158-
.filter(|&(r, c)| self.in_bounds(r, c))
159-
.map(|(r, c)| r * self.width + c),
160-
])
161-
}
162-
163117
pub fn pos(&self, index: usize) -> Pos {
164118
Pos {
165119
index,
@@ -292,47 +246,7 @@ impl Pos {
292246
}
293247
}
294248

295-
pub trait Neighbors: Iterator<Item = usize> {
296-
fn north(&self) -> Option<usize>;
297-
fn east(&self) -> Option<usize>;
298-
fn south(&self) -> Option<usize>;
299-
fn west(&self) -> Option<usize>;
300-
}
301-
302-
pub trait NeighborsDiag: Iterator<Item = usize> {
303-
fn north_east(&self) -> Option<usize>;
304-
fn south_east(&self) -> Option<usize>;
305-
fn south_west(&self) -> Option<usize>;
306-
fn north_west(&self) -> Option<usize>;
307-
}
308-
309249
pub struct NeighborsIter([Option<Pos>; 8]);
310-
pub struct NeighborsIterOld([Option<usize>; 4]);
311-
312-
impl Neighbors for NeighborsIterOld {
313-
fn north(&self) -> Option<usize> {
314-
self.0[0]
315-
}
316-
317-
fn east(&self) -> Option<usize> {
318-
self.0[1]
319-
}
320-
321-
fn south(&self) -> Option<usize> {
322-
self.0[2]
323-
}
324-
325-
fn west(&self) -> Option<usize> {
326-
self.0[3]
327-
}
328-
}
329-
330-
impl Iterator for NeighborsIterOld {
331-
type Item = usize;
332-
fn next(&mut self) -> Option<Self::Item> {
333-
self.0.iter_mut().find_map(|opt| opt.take())
334-
}
335-
}
336250

337251
impl Iterator for NeighborsIter {
338252
type Item = Pos;
@@ -341,24 +255,6 @@ impl Iterator for NeighborsIter {
341255
}
342256
}
343257

344-
impl NeighborsDiag for NeighborsIterOld {
345-
fn north_east(&self) -> Option<usize> {
346-
self.0[0]
347-
}
348-
349-
fn south_east(&self) -> Option<usize> {
350-
self.0[1]
351-
}
352-
353-
fn south_west(&self) -> Option<usize> {
354-
self.0[2]
355-
}
356-
357-
fn north_west(&self) -> Option<usize> {
358-
self.0[3]
359-
}
360-
}
361-
362258
#[cfg(test)]
363259
mod test {
364260
use super::*;

0 commit comments

Comments
 (0)