Skip to content

Commit 2c5ab37

Browse files
committed
fixup
1 parent 3729e4a commit 2c5ab37

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

2024/lib/src/grid.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ use crate::vector::Vector;
77

88
pub type Pos = Vector<i64, 2>;
99

10+
const UP: Vector<i64, 2> = Vector::new([0, -1]);
11+
const DOWN: Vector<i64, 2> = Vector::new([0, 1]);
12+
const LEFT: Vector<i64, 2> = Vector::new([-1, 0]);
13+
const RIGHT: Vector<i64, 2> = Vector::new([1, 0]);
14+
1015
#[derive(Debug, Clone)]
1116
pub struct Grid {
1217
data: BTreeMap<Pos, char>,
@@ -27,6 +32,19 @@ impl Grid {
2732
end: self.end,
2833
}
2934
}
35+
36+
pub fn adjacent(&self, pos: Pos) -> Vec<Pos> {
37+
[UP, DOWN, LEFT, RIGHT].into_iter()
38+
.map(|d| pos + d)
39+
.filter(|&p| self.valid(p))
40+
.collect()
41+
}
42+
43+
pub fn valid(&self, pos: Pos) -> bool {
44+
let (x_range, y_range) = self.range();
45+
46+
x_range.contains(&pos[0]) && y_range.contains(&pos[1])
47+
}
3048
}
3149

3250
impl Display for Grid {

0 commit comments

Comments
 (0)