Created
November 10, 2016 00:10
-
-
Save tuzz/7534bb2dad961b15966e661b33797163 to your computer and use it in GitHub Desktop.
My canonical solution for solving a Sudoku in Sentient
This file contains 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
function main () { | |
array9<array9<int5>> sudoku; | |
sudoku.each(function (row) { | |
invariant row.uniq?; | |
invariant row.all?(function (n) { | |
return n.between?(1, 9); | |
}); | |
}); | |
sudoku.transpose.each(function (column) { | |
invariant column.uniq?; | |
}); | |
sudoku.boxes.each(function (box) { | |
invariant box.uniq?; | |
}); | |
expose sudoku; | |
}; | |
function boxes (sudoku) { | |
boxes = []; | |
3.times(function^ (i) { | |
3.times(function^ (j) { | |
boxes.push(sudoku.box(i, j)); | |
}); | |
}); | |
return boxes; | |
}; | |
function box (sudoku, i, j) { | |
cells = []; | |
x = i * 3; | |
y = j * 3; | |
3.times(function^ (i) { | |
3.times(function^ (j) { | |
cells = cells.push(sudoku[x + i][y + j]); | |
}); | |
}); | |
return cells; | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment