Skip to content

Instantly share code, notes, and snippets.

@asukaminato0721
Created May 26, 2025 12:47
Show Gist options
  • Save asukaminato0721/241b1acd778b49fece68695069cf7db6 to your computer and use it in GitHub Desktop.
Save asukaminato0721/241b1acd778b49fece68695069cf7db6 to your computer and use it in GitHub Desktop.
ZigzagScan with pattern matching
const X: u32 = 5;
const Y: u32 = 5;
fn step(x: u32, y: u32) -> i32 {
dbg!((x, y));
match (x, y) {
(X, Y) => 1,
(..X, 0) => match x % 2 {
0 => step(x + 1, y),
_ => step(x - 1, y + 1),
},
(0, ..Y) => match y % 2 {
0 => step(x + 1, y - 1),
_ => step(x, y + 1),
},
(X, ..Y) => match y % 2 {
0 => step(x - 1, y + 1),
_ => step(x, y + 1),
},
(..X, Y) => match x % 2 {
0 => step(x + 1, y),
_ => step(x + 1, y - 1),
},
(x, y) => match x.abs_diff(y) % 2 {
0 => step(x + 1, y - 1),
_ => step(x - 1, y + 1),
},
}
}
fn main() {
step(0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment