Created
August 7, 2024 18:41
-
-
Save lakshayg/945b39f05318a048b82f3f588ea56cfa to your computer and use it in GitHub Desktop.
Solve sudoku using the cp module in Picat (http://picat-lang.org/)
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
import cp. % ::, all_different | |
import util. % transpose | |
write_sudoku(S) => | |
foreach (R in S) | |
foreach (X in R) | |
printf("%d ", X) | |
end, | |
printf("%n") | |
end. | |
sudoku(S) => | |
S :: 1 .. 9, | |
foreach (R in S) | |
all_different(R) | |
end, | |
foreach (C in transpose(S)) | |
all_different(C) | |
end, | |
foreach (CR in 0 .. 2, CC in 0 .. 2) | |
Cell = [S[3 * CR + I, 3 * CC + J] : I in 1 .. 3, J in 1 .. 3], | |
all_different(Cell) | |
end, | |
solve(S). | |
main => | |
S = { {8, _, _, _, _, _, _, _, _} | |
, {_, _, 3, 6, _, _, _, _, _} | |
, {_, 7, _, _, 9, _, 2, _, _} | |
, {_, 5, _, _, _, 7, _, _, _} | |
, {_, _, _, _, 4, 5, 7, _, _} | |
, {_, _, _, 1, _, _, _, 3, _} | |
, {_, _, 1, _, _, _, _, 6, 8} | |
, {_, _, 8, 5, _, _, _, 1, _} | |
, {_, 9, _, _, _, _, 4, _, _} | |
}, | |
time2(sudoku(S)), | |
write_sudoku(S). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment