Skip to content

Instantly share code, notes, and snippets.

@ph4un00b
Last active September 28, 2023 20:34
Show Gist options
  • Save ph4un00b/2b44eae98b3bdc6df64dc9790f87b745 to your computer and use it in GitHub Desktop.
Save ph4un00b/2b44eae98b3bdc6df64dc9790f87b745 to your computer and use it in GitHub Desktop.
rustic refactorings
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4c469132823a44a3e014f18c54a0f0a9
for (row_idx, row) in world.floor.clone().iter().enumerate() {
for (col_idx, _value) in row.iter().enumerate() {
if world.floor[row_idx][col_idx] == 0_u8 {
world.floor[row_idx][col_idx] = 7_u8;
}
}
}
self.floor
.iter_mut()
.flat_map(|row| row.iter_mut())
.filter(|value| **value == 0_u8)
.for_each(|value| *value = 7_u8);
//=====================
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3a9f21f9c096889369177ba5672a58fc
let mut stack = vec![(0, 0)];
while let Some(current) = stack.pop() {
let neighbors = {
let x = current.0;
let y = current.1;
let mut result = vec![];
if x + 1 < 10 && world.floor[x + 1][y] == 2_u8 {
result.push((x + 1, y))
}
if x > 0 && world.floor[x - 1][y] == 2_u8 {
result.push((x - 1, y))
}
if y + 1 < 24 && world.floor[x][y + 1] == 2_u8 {
result.push((x, y + 1))
}
if y > 0 && world.floor[x][y - 1] == 2_u8 {
result.push((x, y - 1))
}
result
};
for (x, y) in neighbors {
// println!("{x}, {y}");
world.floor[x][y] = 0_u8;
stack.push((x, y));
}
}
pub fn rusty_flood_fill(&mut self, x0: usize, y0: usize, target: u8, replacement: u8) {
let mut queue = VecDeque::new();
queue.push_back((x0, y0));
let directions = [(1_isize, 0_isize), (-1, 0), (0, 1), (0, -1)];
while let Some((x, y)) = queue.pop_front() {
if self.floor[x][y] != target {
continue;
}
self.floor[x][y] = replacement;
directions
.iter()
.map(|&(dx, dy)| (x as isize + dx, y as isize + dy))
.filter(|&(x, y)| (0..=IW).contains(&x) && (0..=IH).contains(&y))
.for_each(|(x, y)| queue.push_back((x as usize, y as usize)));
}
}
// * a bit slower❓ but sensual simpler
pub fn recur_flood_fill(&mut self, x0: isize, y0: isize, target: u8, replacement: u8) {
if x0 < 0 || y0 < 0 {
return;
}
if !matches!((x0, y0), (0..=IW, 0..=IH) if self.floor[x0 as usize][y0 as usize] == target) {
return;
}
self.floor[x0 as usize][y0 as usize] = replacement;
self.recur_flood_fill(x0 + 1, y0, target, replacement);
self.recur_flood_fill(x0 - 1, y0, target, replacement);
self.recur_flood_fill(x0, y0 + 1, target, replacement);
self.recur_flood_fill(x0, y0 - 1, target, replacement);
}
pub fn iter_flood_fill(&mut self, x0: usize, y0: usize, target: u8, replacement: u8) {
let mut stack = Vec::new();
stack.push((x0, y0));
while let Some((x, y)) = stack.pop() {
if self.floor[x][y] != target {
continue;
}
self.floor[x][y] = replacement;
if x > 0 {
stack.push((x - 1, y));
}
if x < W {
stack.push((x + 1, y));
}
if y > 0 {
stack.push((x, y - 1));
}
if y < H {
stack.push((x, y + 1));
}
}
}
//===================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment