Uses a homebrew graph to solve sudokus.
random_puzzle(s, n)
(where s
is the Sudoku size and n
is the number of
squares to fill in) is guaranteed to generate a valid puzzle, but not guaranteed to
generate a puzzle with a unique solution.
Use new_puzzle()
with the same arguments as random_puzzle
to print directly.
solve!(graph::SudokuGraph)
...well, solves the given graph. Currently not
very fast for larger puzzles.
For examples on how to format a puzzle for loading, check out src/data
and
src/puzzle/io.jl
2 and 3-sudokus have an extra, simplified input method using a string:
> g = load_puzzle_string(2, "4000104334100134");
> print(g)
4 1 | 3
| 4 1
-----------
4 | 1 3
3 | 4
> g = random_puzzle(3, 33)
> print(g)
| 7 | 2
6 5 | 2 9 | 8 7
7 2 | 8 |
---------------------
3 | 4 5 9 | 1
1 7 | 2 | 4 9
2 4 | 7 | 3 8
---------------------
| 1 |
8 | 6 | 7 5
| 4 | 1
> @time g = random_puzzle(4);
0.074973 seconds (646.10 k allocations: 123.388 MiB)
> print(g)
10 4 | 3 8 | 9 15 6 | 16 12 13 5
15 14 6 9 | 13 5 12 16 | 4 10 11 2 | 8 3 7 1
3 1 8 7 | 15 6 9 14 | 12 13 | 4 10 11
13 12 16 | 10 | 8 | 6 14 15
-----------------------------------------------------
2 11 4 | 7 3 | 6 14 15 9 | 12 5 16
14 9 15 | 13 5 12 | 11 2 10 | 3 1 8 7
13 16 5 | 2 10 11 | 3 7 1 8 | 15 9 14
7 3 | 15 9 | 5 | 4 10
-----------------------------------------------------
8 3 1 | 6 9 14 | 13 12 | 10 2 11 4
4 11 | 3 8 7 | 14 9 | 13 16 12
12 5 16 | 11 4 2 10 | 3 8 1 | 14 9 15 6
15 9 | 5 16 | 4 10 | 7 8 3
-----------------------------------------------------
9 6 | 12 16 5 | 10 4 2 11 | 1 7 3 8
16 12 13 5 | 4 2 10 | 1 8 7 3 | 15 14 6 9
7 8 1 3 | 9 14 15 | 5 13 | 11 10 4 2
2 4 10 11 | 7 1 3 | 15 9 14 6 | 5 13 12 16
> @time new_puzzle(5)
0.510008 seconds (3.77 M allocations: 724.684 MiB, 10.26% gc time)
11 23 14 19 | 22 6 9 3 7 | 2 18 | 10 12 4 | 21 8 17 16
22 25 8 7 16 | 21 15 23 20 12 | 5 1 4 10 19 | 11 24 2 17 | 18 14 6 3 9
5 4 10 12 | 11 17 16 13 25 | 7 8 21 | 3 1 9 14 18 | 15 19 2
9 17 13 3 20 | 2 19 1 24 | 16 11 14 25 | 6 22 21 | 23 7 4 10 5
2 24 21 | 14 4 5 10 8 | 9 22 6 3 17 | 19 23 16 | 25 1 11 13
--------------------------------------------------------------------------------
20 19 15 | 22 17 9 13 | 25 11 21 | 4 10 6 3 | 14 12 16 2 8
21 11 7 | 3 6 4 10 | 17 18 | 2 14 8 12 16 | 20 24 1 15
18 22 9 17 | 8 | 3 5 | 19 20 24 | 11 21 7 23
3 10 5 4 6 | 24 1 19 20 | 16 2 14 | 23 7 11 | 13 18 9 17
12 14 16 2 8 | 7 23 25 11 | 15 24 19 20 | 9 13 17 18 | 10 3 5 4 6
--------------------------------------------------------------------------------
4 3 24 5 | 1 12 11 8 19 | 13 23 20 15 18 | 17 22 14 10 6 | 2 16 21 25 7
19 16 11 2 | 4 13 3 5 | 9 6 22 | 1 7 23 15 | 24 17 14
13 21 17 25 | 10 9 2 6 | 1 19 8 16 | 5 3 4 | 12 23 18 15
15 20 8 | 16 24 18 | 3 4 12 17 | 2 13 21 | 22 10 9 19
14 6 22 | 23 20 21 17 15 | 25 24 11 7 | 12 19 9 | 5 4 13 1 3
--------------------------------------------------------------------------------
6 10 4 | 19 2 23 16 | 14 17 1 11 | 18 9 21 22 24 | 3 13 8 25
14 3 25 9 | 8 11 13 21 | 16 15 23 24 12 | 7 6 1 2 | 19 10 18 4
1 19 18 13 11 | 5 10 14 3 | 9 2 25 6 | 23 8 16 15 12 | 7 17 24 21 20
16 8 2 15 24 | 18 20 1 | 4 5 10 21 3 | 14 17 13 11 19 | 6 9 12 22
12 7 21 | 9 24 22 15 6 | 19 20 13 18 8 | 25 5 | 16 11 2 14 1
--------------------------------------------------------------------------------
2 9 19 22 | 21 12 23 | 11 13 7 1 | 15 24 18 8 25 | 4 6 20 5
25 16 13 | 20 8 24 1 11 | 10 6 15 5 4 | 12 19 17 14 | 9 3 22 18
11 17 | 3 19 22 9 | 18 21 25 2 | 5 4 10 23 | 8 15 7 24
7 24 15 18 | 6 25 10 5 4 | 14 8 3 22 9 | 2 20 1 13 | 19 23 11
8 4 6 5 10 | 15 7 18 16 | 20 17 23 24 | 22 21 11 9 3 | 1 25 12
> g = random_puzzle(3, 25)
> print(g)
2 3 | 4 5 |
6 | | 9 4
4 | 7 | 8
---------------------
| 5 | 7
| 9 |
| 8 | 1 3
---------------------
| 1 6 |
4 2 | 8 3 | 6 1
6 | |
> solve!(g)
> print(g)
2 3 8 | 4 9 5 | 1 6 7
6 7 5 | 3 8 1 | 9 2 4
9 1 4 | 7 6 2 | 8 3 5
---------------------
4 8 1 | 6 5 3 | 7 9 2
3 2 7 | 9 1 4 | 5 8 6
5 9 6 | 2 7 8 | 4 1 3
---------------------
8 5 3 | 1 4 6 | 2 7 9
7 4 2 | 8 3 9 | 6 5 1
1 6 9 | 5 2 7 | 3 4 8
> g = SudokuGraph(4)
> print(g)
| | |
| | |
| | |
| | |
-----------------------------------------------------
| | |
| | |
| | |
| | |
-----------------------------------------------------
| | |
| | |
| | |
| | |
-----------------------------------------------------
| | |
| | |
| | |
| | |
> solve!(g)
> print(g)
16 12 14 3 | 10 6 2 11 | 1 8 4 7 | 13 15 5 9
8 4 10 7 | 5 14 9 3 | 12 11 15 13 | 16 1 2 6
15 2 13 6 | 1 8 7 4 | 5 16 3 9 | 14 11 10 12
11 1 5 9 | 15 16 12 13 | 10 14 6 2 | 8 3 4 7
-----------------------------------------------------
14 5 7 2 | 13 3 6 10 | 15 9 8 12 | 1 4 16 11
1 3 11 12 | 16 2 8 9 | 13 10 5 4 | 7 14 6 15
4 9 6 15 | 14 12 5 7 | 3 1 11 16 | 10 2 8 13
13 10 16 8 | 11 15 4 1 | 6 7 2 14 | 12 5 9 3
-----------------------------------------------------
7 6 3 10 | 9 1 16 15 | 2 5 13 8 | 4 12 11 14
9 14 15 1 | 8 4 13 5 | 7 6 12 11 | 2 16 3 10
5 16 2 11 | 12 7 3 14 | 9 4 1 10 | 6 13 15 8
12 8 4 13 | 2 10 11 6 | 16 3 14 15 | 5 9 7 1
-----------------------------------------------------
10 11 8 5 | 6 13 14 2 | 4 15 9 1 | 3 7 12 16
2 7 12 16 | 3 9 15 8 | 14 13 10 5 | 11 6 1 4
6 13 9 4 | 7 11 1 12 | 8 2 16 3 | 15 10 14 5
3 15 1 14 | 4 5 10 16 | 11 12 7 6 | 9 8 13 2