Created
May 26, 2025 12:47
-
-
Save asukaminato0721/241b1acd778b49fece68695069cf7db6 to your computer and use it in GitHub Desktop.
ZigzagScan with pattern matching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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