Sudoku: Difference between revisions
Content deleted Content added
Added another C# solution |
m →{{header|Rust}}: do rustfmt |
||
Line 9,780: | Line 9,780: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|Ada}} |
{{trans|Ada}} |
||
<lang rust> |
<lang rust>type SudokuArType = [u8; 81]; |
||
type SudokuArType = [u8; 81]; |
|||
fn check_validity( |
fn check_validity(val: u8, x: usize, y: usize, sudoku_ar: &mut SudokuArType) -> bool { |
||
for i in 0..=8 { |
|||
if (sudoku_ar[y * 9 + i] == val) || (sudoku_ar[i * 9 + x] == val) { |
|||
return false; |
|||
⚫ | |||
} |
|||
} |
|||
} |
|||
let start_x: usize = (x / 3) * 3; |
|||
let start_y: usize = (y / 3) * 3; |
|||
for i in start_y..=(start_y + 2) { |
|||
for j in start_x..=(start_x + 2) { |
|||
if sudoku_ar[i * 9 + j] == val { |
|||
return false; |
|||
} |
|||
} |
|||
} |
} |
||
true |
true |
||
} |
} |
||
⚫ | |||
let mut ret: bool; |
|||
let mut ret : bool; |
|||
if pos == 81 { |
if pos == 81 { |
||
return true; |
|||
} |
|||
} |
|||
if sudoku_ar[pos] > 0 { |
|||
ret = place_number(pos + 1, sudoku_ar); |
|||
if ret == true { |
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
for n in 1..=9 { |
|||
if check_validity(n, pos % 9, pos / 9, sudoku_ar) == true { |
|||
sudoku_ar[pos] = n; |
|||
ret = place_number(pos + 1, sudoku_ar); |
|||
if ret == true { |
|||
return true; |
|||
} |
|||
} |
|||
sudoku_ar[pos] = 0; |
|||
} |
|||
} |
|||
} |
} |
||
false |
false |
||
Line 9,834: | Line 9,832: | ||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if (i + 1) % 3 == 0 && !((i + 1) % 9 == 0) { |
|||
⚫ | |||
print!("| "); |
|||
} |
|||
⚫ | |||
} |
|||
⚫ | |||
println!(" "); |
println!(" "); |
||
} |
|||
if (i + 1) % 27 == 0 { |
|||
println!("{}", line_sep); |
println!("{}", line_sep); |
||
} |
} |
||
} |
|||
} |
|||
} |
} |
||
fn solve(sudoku_ar: &mut SudokuArType) -> bool { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
fn main() { |
fn main() { |
||
let mut sudoku_ar: SudokuArType = [ |
|||
8,5,0,0,0,2,4,0,0, |
8, 5, 0, 0, 0, 2, 4, 0, 0, |
||
7,2,0,0,0,0,0,0,9, |
7, 2, 0, 0, 0, 0, 0, 0, 9, |
||
0,0,4,0,0,0,0,0,0, |
0, 0, 4, 0, 0, 0, 0, 0, 0, |
||
0,0,0,1,0,7,0,0,2, |
0, 0, 0, 1, 0, 7, 0, 0, 2, |
||
3,0,5,0,0,0,9,0,0, |
3, 0, 5, 0, 0, 0, 9, 0, 0, |
||
0,4,0,0,0,0,0,0,0, |
0, 4, 0, 0, 0, 0, 0, 0, 0, |
||
0,0,0,0,8,0,0,7,0, |
0, 0, 0, 0, 8, 0, 0, 7, 0, |
||
0,1,7,0,0,0,0,0,0, |
0, 1, 7, 0, 0, 0, 0, 0, 0, |
||
0,0,0,0,3,6,0,4,0 |
0, 0, 0, 0, 3, 6, 0, 4, 0 |
||
]; |
|||
⚫ | |||
if solve(&mut sudoku_ar) == false { |
|||
println!("Unsolvable"); |
|||
} else { |
|||
pretty_print(sudoku_ar); |
|||
} |
|||
}</lang> |
}</lang> |
||