Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This probably sucks and is horribly inefficient, but I'm still learning Rust.
- type Row = [u32; 9];
- type Sudoku = [Row; 9];
- static SUDOKU: Sudoku = [
- [8, 2, 7, 1, 5, 4, 3, 9, 6],
- [9, 6, 5, 3, 2, 7, 1, 4, 8],
- [3, 4, 1, 6, 8, 9, 7, 5, 2],
- [5, 9, 3, 4, 6, 8, 2, 7, 1],
- [4, 7, 2, 5, 1, 3, 6, 8, 9],
- [6, 1, 8, 9, 7, 2, 4, 3, 5],
- [7, 8, 6, 2, 3, 5, 9, 1, 4],
- [1, 5, 4, 7, 9, 6, 8, 2, 3],
- [2, 3, 9, 8, 4, 1, 5, 6, 7]
- ];
- static VALID_ROW: Row = [1, 2, 3, 4, 5, 6, 7, 8, 9];
- fn is_valid(row: &[u32]) -> bool {
- let mut clone = [0u32; 9];
- clone.clone_from_slice(row);
- clone.sort();
- clone == VALID_ROW
- }
- fn rotate(sudoku: &Sudoku) -> Sudoku {
- let mut rotated = [[0u32; 9]; 9];
- for r in 0..9 {
- for c in 0..9 {
- rotated[c][r] = sudoku[r][c];
- }
- }
- rotated
- }
- fn chunk(sudoku: &Sudoku) -> Sudoku {
- let mut chunked = [[0u32; 9]; 9];
- let mut c = 0;
- let chunks = [0..3, 3..6, 6..9];
- for outer in chunks.iter() {
- for inner in chunks.iter() {
- let mut chunk: Vec<u32> = vec![];
- for o in outer.clone() {
- for i in inner.clone() {
- chunk.push(sudoku[o][i]);
- }
- }
- chunked[c].clone_from_slice(&chunk);
- c += 1;
- }
- }
- chunked
- }
- fn is_sudoku_valid(sudoku: &Sudoku) -> bool {
- sudoku.iter().map(|row| is_valid(row)).fold(true, |a, b| a && b)
- }
- fn validate(sudoku: &Sudoku) -> bool {
- is_sudoku_valid(sudoku) && is_sudoku_valid(&rotate(sudoku)) && is_sudoku_valid(&chunk(sudoku))
- }
- fn main() {
- println!("{:?}", validate(&SUDOKU));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement