Sudoku: Difference between revisions
Content deleted Content added
No edit summary |
Alextretyak (talk | contribs) Added 11l |
||
Line 8: | Line 8: | ||
* [https://www.youtube.com/watch?v=G_UYXzGuqvM Python Sudoku Solver] Computerphile video. |
* [https://www.youtube.com/watch?v=G_UYXzGuqvM Python Sudoku Solver] Computerphile video. |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
{{trans|Kotlin}} |
|||
<lang 11l>T Sudoku |
|||
solved = 0B |
|||
grid = [0] * 81 |
|||
F (rows) |
|||
assert(rows.len == 9 & all(rows.map(row -> row.len == 9)), ‘Grid must be 9 x 9’) |
|||
L(i) 9 |
|||
L(j) 9 |
|||
.grid[9 * i + j] = Int(rows[i][j]) |
|||
F solve() |
|||
print("Starting grid:\n\n"(.)) |
|||
.placeNumber(0) |
|||
print(I .solved {"Solution:\n\n"(.)} E ‘Unsolvable!’) |
|||
F placeNumber(pos) |
|||
I .solved |
|||
R |
|||
I pos == 81 |
|||
.solved = 1B |
|||
R |
|||
I .grid[pos] > 0 |
|||
.placeNumber(pos + 1) |
|||
R |
|||
L(n) 1..9 |
|||
I .checkValidity(n, pos % 9, pos I/ 9) |
|||
.grid[pos] = n |
|||
.placeNumber(pos + 1) |
|||
I .solved |
|||
R |
|||
.grid[pos] = 0 |
|||
F checkValidity(v, x, y) |
|||
L(i) 9 |
|||
I .grid[y * 9 + i] == v | |
|||
.grid[i * 9 + x] == v |
|||
R 0B |
|||
V startX = (x I/ 3) * 3 |
|||
V startY = (y I/ 3) * 3 |
|||
L(i) startY .< startY + 3 |
|||
L(j) startX .< startX + 3 |
|||
I .grid[i * 9 + j] == v |
|||
R 0B |
|||
R 1B |
|||
F String() |
|||
V s = ‘’ |
|||
L(i) 9 |
|||
L(j) 9 |
|||
s ‘’= .grid[i * 9 + j]‘ ’ |
|||
I j C (2, 5) |
|||
s ‘’= ‘| ’ |
|||
s ‘’= "\n" |
|||
I i C (2, 5) |
|||
s ‘’= "------+-------+------\n" |
|||
R s |
|||
V rows = [‘850002400’, |
|||
‘720000009’, |
|||
‘004000000’, |
|||
‘000107002’, |
|||
‘305000900’, |
|||
‘040000000’, |
|||
‘000080070’, |
|||
‘017000000’, |
|||
‘000036040’] |
|||
Sudoku(rows).solve()</lang> |
|||
{{out}} |
|||
<pre> |
|||
Starting grid: |
|||
8 5 0 | 0 0 2 | 4 0 0 |
|||
7 2 0 | 0 0 0 | 0 0 9 |
|||
0 0 4 | 0 0 0 | 0 0 0 |
|||
------+-------+------ |
|||
0 0 0 | 1 0 7 | 0 0 2 |
|||
3 0 5 | 0 0 0 | 9 0 0 |
|||
0 4 0 | 0 0 0 | 0 0 0 |
|||
------+-------+------ |
|||
0 0 0 | 0 8 0 | 0 7 0 |
|||
0 1 7 | 0 0 0 | 0 0 0 |
|||
0 0 0 | 0 3 6 | 0 4 0 |
|||
Solution: |
|||
8 5 9 | 6 1 2 | 4 3 7 |
|||
7 2 3 | 8 5 4 | 1 6 9 |
|||
1 6 4 | 3 7 9 | 5 2 8 |
|||
------+-------+------ |
|||
9 8 6 | 1 4 7 | 3 5 2 |
|||
3 7 5 | 2 6 8 | 9 1 4 |
|||
2 4 1 | 5 9 3 | 7 8 6 |
|||
------+-------+------ |
|||
4 3 2 | 9 8 1 | 6 7 5 |
|||
6 1 7 | 4 2 5 | 8 9 3 |
|||
5 9 8 | 7 3 6 | 2 4 1 |
|||
</pre> |
|||
=={{header|8th}}== |
=={{header|8th}}== |