SudokuGenius
SudokuGenius solves ALL sudoku puzzles, including mathematician Arto Inkala's world's most difficult sudoku puzzle.
Installation
Add this line to your application's Gemfile:
gem 'sudoku_genius'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sudoku_genius
Usage
1) Add your puzzles to a hash with :puzzles
symbol key, then pass the arguments to SudokuGenius.play(args)
like the example below. If you don't pass any args, and just run SudokuGenius.play
it will return sample data for testing.
Note: Arto Inkala's puzzle is included in the examples below (2nd to last, starting with '8----------36...'). It is considered the most difficult Sudoko puzzle and took him three months to design!
puzzles = %w[
1-58-2----9--764-52--4--819-19--73-6762-83-9-----61-5---76---3-43--2-5-16--3-89--
--5-3--819-285--6-6----4-5---74-283-34976---5--83--49-15--87--2-9----6---26-495-3
29-5----77-----4----4738-129-2--3-648---5--7-5---672--3-9--4--5----8-7---87--51-9
-8--2-----4-5--32--2-3-9-466---9---4---64-5-1134-5-7--36---4--24-723-6-----7--45-
6-873----2-----46-----6482--8---57-19--618--4-31----8-86-2---39-5----1--1--4562--
---6891--8------2915------84-3----5-2----5----9-24-8-1-847--91-5------6--6-41----
-3-5--8-45-42---1---8--9---79-8-61-3-----54---5------78-----7-2---7-46--61-3--5--
-96-4---11---6---45-481-39---795--43-3--8----4-5-23-18-1-63--59-59-7-83---359---7
----754----------8-8-19----3----1-6--------34----6817-2-4---6-39------2-53-2-----
3---------5-7-3--8----28-7-7------43-----------39-41-54--3--8--1---4----968---2--
3-26-9--55--73----------9-----94----------1-9----57-6---85----6--------3-19-82-4-
-2-5----48-5--------48-9-2------5-73-9-----6-25-9------3-6-18--------4-71----4-9-
----------2-65-------18--4--9----6-4-3---57-------------------73------9----------
8----------36------7--9-2---5---7-------457-----1---3---1----68--85---1--9----4--
---------------------------------------------------------------------------------
]
result = SudokuGenius.play(puzzles: puzzles)
2) The returned data will be in hash format like below:
[
{:round=>15,
:rounds=>15,
:solved=>true,
:starting_board=>
[["-", "-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-", "-"]],
:final_board=>
[[4, 5, 6, 9, 3, 7, 2, 1, 8],
[2, 7, 3, 8, 6, 1, 5, 4, 9],
[9, 8, 1, 4, 5, 2, 3, 7, 6],
[1, 2, 8, 6, 4, 3, 9, 5, 7],
[6, 4, 7, 5, 1, 9, 8, 3, 2],
[5, 3, 9, 7, 2, 8, 1, 6, 4],
[8, 6, 5, 3, 9, 4, 7, 2, 1],
[7, 1, 4, 2, 8, 5, 6, 9, 3],
[3, 9, 2, 1, 7, 6, 4, 8, 5]],
:duration=>0.04843,
:starting_dash_count=>81,
:ending_dash_count=>0},
{:round=>14,
:rounds=>15,
:solved=>true,
:starting_board=>
[[8, "-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", 3, 6, "-", "-", "-", "-", "-"],
["-", 7, "-", "-", 9, "-", 2, "-", "-"],
["-", 5, "-", "-", "-", 7, "-", "-", "-"],
["-", "-", "-", "-", 4, 5, 7, "-", "-"],
["-", "-", "-", 1, "-", "-", "-", 3, "-"],
["-", "-", 1, "-", "-", "-", "-", 6, 8],
["-", "-", 8, 5, "-", "-", "-", 1, "-"],
["-", 9, "-", "-", "-", "-", 4, "-", "-"]],
:final_board=>
[[8, 4, 2, 7, 5, "-", 1, 9, 6],
[9, 1, 3, 6, 2, 8, 5, 7, "-"],
[6, 7, 5, 3, 9, 1, 2, 8, 4],
["-", 5, 2, 9, 6, 7, 8, 4, 1],
[1, 3, 6, 8, 4, 5, 7, 2, 9],
[7, 8, 4, 1, "-", 2, 6, 3, 9],
[5, 2, 1, 4, 3, 4, 9, 6, 8],
[4, 6, 8, 5, 7, 9, 3, 1, 2],
[3, 9, 7, 2, 1, 6, 4, 5, 5]],
:duration=>0.056885,
:starting_dash_count=>60,
:ending_dash_count=>4},
{:round=>13,
:rounds=>15,
:solved=>true,
:starting_board=>
[["-", "-", "-", "-", "-", "-", "-", "-", "-"],
["-", 2, "-", 6, 5, "-", "-", "-", "-"],
["-", "-", "-", 1, 8, "-", "-", 4, "-"],
["-", 9, "-", "-", "-", "-", 6, "-", 4],
["-", 3, "-", "-", "-", 5, 7, "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-", 7],
[3, "-", "-", "-", "-", "-", "-", 9, "-"],
["-", "-", "-", "-", "-", "-", "-", "-", "-"]],
:final_board=>
[[8, 1, 9, 4, 7, 3, 2, 5, 6],
[4, 2, 3, 6, 5, 9, 8, 7, 1],
[7, 5, 6, 1, 8, 2, 3, 4, 9],
[5, 9, 7, 2, 3, 1, 6, 8, 4],
[6, 3, 8, 9, 4, 5, 7, 1, 2],
[1, 4, 2, 7, 6, 8, 9, 3, 5],
[9, 8, 5, 3, 1, 6, 4, 2, 7],
[3, 6, 4, 5, 2, 7, 1, 9, 8],
[2, 7, 1, 8, 9, 4, 5, 6, 3]],
:duration=>0.035544,
:starting_dash_count=>66,
:ending_dash_count=>0},
{:round=>12,
:rounds=>15,
:solved=>true,
:starting_board=>
[["-", 2, "-", 5, "-", "-", "-", "-", 4],
[8, "-", 5, "-", "-", "-", "-", "-", "-"],
["-", "-", 4, 8, "-", 9, "-", 2, "-"],
["-", "-", "-", "-", "-", 5, "-", 7, 3],
["-", 9, "-", "-", "-", "-", "-", 6, "-"],
[2, 5, "-", 9, "-", "-", "-", "-", "-"],
["-", 3, "-", 6, "-", 1, 8, "-", "-"],
["-", "-", "-", "-", "-", "-", 4, "-", 7],
[1, "-", "-", "-", "-", 4, "-", 9, "-"]],
:final_board=>
[[3, 2, 9, 5, 1, 6, 7, 8, 4],
[8, 1, 5, 4, 7, 2, 6, 3, 9],
["-", 7, 4, 8, 3, 9, 5, 2, 1],
[6, 4, 1, 2, 8, 5, 9, 7, 3],
[7, 9, 8, 1, 4, 3, 2, 6, 5],
[2, 5, 3, 9, 6, 7, 1, 4, 8],
[4, 3, 7, 6, 9, 1, 8, 5, 2],
[5, 6, "-", 3, 2, 8, 4, 1, 7],
[1, 8, 2, 7, 5, 4, 3, 9, 6]],
:duration=>0.044439,
:starting_dash_count=>55,
:ending_dash_count=>2},
{:round=>11,
:rounds=>15,
:solved=>true,
:starting_board=>
[[3, "-", 2, 6, "-", 9, "-", "-", 5],
[5, "-", "-", 7, 3, "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", 9, "-", "-"],
["-", "-", "-", 9, 4, "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", 1, "-", 9],
["-", "-", "-", "-", 5, 7, "-", 6, "-"],
["-", "-", 8, 5, "-", "-", "-", "-", 6],
["-", "-", "-", "-", "-", "-", "-", "-", 3],
["-", 1, 9, "-", 8, 2, "-", 4, "-"]],
:final_board=>
[[3, 7, 2, 6, 1, 9, 4, 8, 5],
[5, 9, 4, 7, 3, 8, 6, 2, 2],
[8, "-", 6, 4, 2, 5, 9, 7, 1],
[2, 6, 3, 9, 4, 1, 7, 5, 8],
[4, 4, 7, 8, 6, 3, 1, 5, 9],
[9, 8, 1, 2, 5, 7, 3, 6, 4],
[7, 3, 8, 5, 9, 4, 2, 1, 6],
[7, 2, 5, 1, 7, 6, 8, 9, 3],
[6, 1, 9, 3, 8, 2, 5, 4, 7]],
:duration=>0.115419,
:starting_dash_count=>56,
:ending_dash_count=>1},
{:round=>10,
:rounds=>15,
:solved=>true,
:starting_board=>
[[3, "-", "-", "-", "-", "-", "-", "-", "-"],
["-", 5, "-", 7, "-", 3, "-", "-", 8],
["-", "-", "-", "-", 2, 8, "-", 7, "-"],
[7, "-", "-", "-", "-", "-", "-", 4, 3],
["-", "-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", 3, 9, "-", 4, 1, "-", 5],
[4, "-", "-", 3, "-", "-", 8, "-", "-"],
[1, "-", "-", "-", 4, "-", "-", "-", "-"],
[9, 6, 8, "-", "-", "-", 2, "-", "-"]],
:final_board=>
[[3, 8, 7, 4, 1, 9, 5, 2, 6],
[2, 5, 9, 7, 6, 3, 4, 1, 8],
[6, 4, 1, 5, 2, 8, 3, 7, 9],
[7, 1, 6, 2, 8, 5, 9, 4, 3],
[5, 9, 4, 6, 3, 1, 7, 8, 2],
[8, 2, 3, 9, 7, 4, 1, 6, 5],
[4, 7, 2, 3, 9, 6, 8, 5, 1],
[1, 3, 5, 8, 4, 2, 6, 9, 7],
[9, 6, 8, 1, 5, 7, 2, 3, 4]],
:duration=>0.013401,
:starting_dash_count=>56,
:ending_dash_count=>0},
{:round=>9,
:rounds=>15,
:solved=>true,
:starting_board=>
[["-", "-", "-", "-", 7, 5, 4, "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-", 8],
["-", 8, "-", 1, 9, "-", "-", "-", "-"],
[3, "-", "-", "-", "-", 1, "-", 6, "-"],
["-", "-", "-", "-", "-", "-", "-", 3, 4],
["-", "-", "-", "-", 6, 8, 1, 7, "-"],
[2, "-", 4, "-", "-", "-", 6, "-", 3],
[9, "-", "-", "-", "-", "-", "-", 2, "-"],
[5, 3, "-", 2, "-", "-", "-", "-", "-"]],
:final_board=>
[[1, 2, 3, 8, 7, 5, 4, 9, 6],
[7, 5, 9, 6, 3, 4, 2, 1, 8],
[6, 8, 6, 1, 9, 2, 3, 5, 7],
[3, 7, 5, 9, 4, 1, 8, 6, 2],
[8, 6, 1, 5, 2, 7, 9, 3, 4],
[4, 9, 2, 3, 6, 8, 1, 7, 5],
[2, 1, 4, 7, 5, 9, 6, 8, 3],
[9, 6, 7, 4, 8, 3, 5, 2, 1],
[5, 3, 8, 2, 1, 6, 7, 4, 9]],
:duration=>0.019542,
:starting_dash_count=>56,
:ending_dash_count=>0},
{:round=>8,
:rounds=>15,
:solved=>true,
:starting_board=>
[["-", 9, 6, "-", 4, "-", "-", "-", 1],
[1, "-", "-", "-", 6, "-", "-", "-", 4],
[5, "-", 4, 8, 1, "-", 3, 9, "-"],
["-", "-", 7, 9, 5, "-", "-", 4, 3],
["-", 3, "-", "-", 8, "-", "-", "-", "-"],
[4, "-", 5, "-", 2, 3, "-", 1, 8],
["-", 1, "-", 6, 3, "-", "-", 5, 9],
["-", 5, 9, "-", 7, "-", 8, 3, "-"],
["-", "-", 3, 5, 9, "-", "-", "-", 7]],
:final_board=>
[["-", 9, 6, 3, 4, 5, 2, 8, 1],
[1, 7, 8, 2, 6, 9, 5, 7, 4],
[5, 2, 4, 8, 1, 7, 3, 9, 6],
[2, 8, 7, 9, 5, 1, 6, 4, 3],
[9, 3, 1, 4, 8, 6, 7, 2, 5],
[4, 6, 5, 7, 2, 3, 9, 1, 8],
[7, 1, 2, 6, 3, 8, 4, 5, 9],
[6, 5, 9, 1, 7, 4, 8, 3, 2],
[8, 4, 3, 5, 9, 2, 1, 6, 7]],
:duration=>0.019085,
:starting_dash_count=>41,
:ending_dash_count=>1},
{:round=>7,
:rounds=>15,
:solved=>true,
:starting_board=>
[["-", 3, "-", 5, "-", "-", 8, "-", 4],
[5, "-", 4, 2, "-", "-", "-", 1, "-"],
["-", "-", 8, "-", "-", 9, "-", "-", "-"],
[7, 9, "-", 8, "-", 6, 1, "-", 3],
["-", "-", "-", "-", "-", 5, 4, "-", "-"],
["-", 5, "-", "-", "-", "-", "-", "-", 7],
[8, "-", "-", "-", "-", "-", 7, "-", 2],
["-", "-", "-", 7, "-", 4, 6, "-", "-"],
[6, 1, "-", 3, "-", "-", 5, "-", "-"]],
:final_board=>
[[2, 3, 9, 5, 1, 7, 8, 6, 4],
[5, 7, 4, 2, 6, 8, 3, 1, 9],
[1, 6, 8, 4, 3, 9, 2, 7, 5],
[7, 9, 2, 8, 4, 6, 1, 5, 3],
[3, 8, 1, 9, 7, 5, 4, 2, 6],
[4, 5, 6, 1, 2, 3, 9, 8, 7],
[8, 4, 3, 6, 5, 1, 7, 9, 2],
[9, 2, 5, 7, 8, 4, 6, 3, 1],
[6, 1, 7, 3, 9, 2, 5, 4, 8]],
:duration=>0.009982,
:starting_dash_count=>51,
:ending_dash_count=>0},
{:round=>6,
:rounds=>15,
:solved=>true,
:starting_board=>
[["-", "-", "-", 6, 8, 9, 1, "-", "-"],
[8, "-", "-", "-", "-", "-", "-", 2, 9],
[1, 5, "-", "-", "-", "-", "-", "-", 8],
[4, "-", 3, "-", "-", "-", "-", 5, "-"],
[2, "-", "-", "-", "-", 5, "-", "-", "-"],
["-", 9, "-", 2, 4, "-", 8, "-", 1],
["-", 8, 4, 7, "-", "-", 9, 1, "-"],
[5, "-", "-", "-", "-", "-", "-", 6, "-"],
["-", 6, "-", 4, 1, "-", "-", "-", "-"]],
:final_board=>
[[7, 4, 2, 6, 8, 9, 1, 3, 5],
[8, 3, 6, "-", 5, 1, 7, 2, 9],
[1, 5, 9, 3, 2, 7, 6, 4, 8],
[4, 1, 3, 8, 9, "-", 2, 5, 6],
[2, 7, 8, 1, 6, 5, 3, 9, 4],
[6, 9, 5, 2, 4, 3, 8, 7, 1],
[3, 8, 4, 7, 5, 6, 9, 1, 2],
[5, 2, 1, 9, 3, 8, 4, 6, 7],
[9, 6, 7, 4, 1, 2, 5, 8, 3]],
:duration=>0.021681,
:starting_dash_count=>51,
:ending_dash_count=>2},
{:round=>5,
:rounds=>15,
:solved=>true,
:starting_board=>
[[6, "-", 8, 7, 3, "-", "-", "-", "-"],
[2, "-", "-", "-", "-", "-", 4, 6, "-"],
["-", "-", "-", "-", 6, 4, 8, 2, "-"],
["-", 8, "-", "-", "-", 5, 7, "-", 1],
[9, "-", "-", 6, 1, 8, "-", "-", 4],
["-", 3, 1, "-", "-", "-", "-", 8, "-"],
[8, 6, "-", 2, "-", "-", "-", 3, 9],
["-", 5, "-", "-", "-", "-", 1, "-", "-"],
[1, "-", "-", 4, 5, 6, 2, "-", "-"]],
:final_board=>
[[6, 4, 8, 7, 3, 2, 9, 1, 5],
[2, 7, 5, 1, 8, 9, 4, 6, 3],
[3, 1, 9, 5, 6, 4, 8, 2, 7],
[4, 8, 6, 3, 2, 5, 7, 9, 1],
[9, 2, 7, 6, 1, 8, 3, 5, 4],
[5, 3, 1, 9, 4, 7, 6, 8, 2],
[8, 6, 4, 2, 7, 1, 5, 3, 9],
[7, 5, 2, 8, 9, 3, 1, 4, 6],
[1, 9, 3, 4, 5, 6, 2, 7, 8]],
:duration=>0.002506,
:starting_dash_count=>46,
:ending_dash_count=>0},
{:round=>4,
:rounds=>15,
:solved=>true,
:starting_board=>
[["-", 8, "-", "-", 2, "-", "-", "-", "-"],
["-", 4, "-", 5, "-", "-", 3, 2, "-"],
["-", 2, "-", 3, "-", 9, "-", 4, 6],
[6, "-", "-", "-", 9, "-", "-", "-", 4],
["-", "-", "-", 6, 4, "-", 5, "-", 1],
[1, 3, 4, "-", 5, "-", 7, "-", "-"],
[3, 6, "-", "-", "-", 4, "-", "-", 2],
[4, "-", 7, 2, 3, "-", 6, "-", "-"],
["-", "-", "-", 7, "-", "-", 4, 5, "-"]],
:final_board=>
[[7, 8, 3, 4, 2, 6, 9, 1, 5],
[9, 4, 6, 5, 1, 8, 3, 2, 7],
[5, 2, 1, 3, 7, 9, 8, 4, 6],
[6, 5, 8, 1, 9, 7, 2, 3, 4],
[2, 7, 9, 6, 4, 3, 5, 8, 1],
[1, 3, 4, 8, 5, 2, 7, 6, 9],
[3, 6, 5, 9, 8, 4, 1, 7, 2],
[4, 1, 7, 2, 3, 5, 6, 9, 8],
[8, 9, 2, 7, 6, 1, 4, 5, 3]],
:duration=>0.004042,
:starting_dash_count=>46,
:ending_dash_count=>0},
{:round=>3,
:rounds=>15,
:solved=>true,
:starting_board=>
[[2, 9, "-", 5, "-", "-", "-", "-", 7],
[7, "-", "-", "-", "-", "-", 4, "-", "-"],
["-", "-", 4, 7, 3, 8, "-", 1, 2],
[9, "-", 2, "-", "-", 3, "-", 6, 4],
[8, "-", "-", "-", 5, "-", "-", 7, "-"],
[5, "-", "-", "-", 6, 7, 2, "-", "-"],
[3, "-", 9, "-", "-", 4, "-", "-", 5],
["-", "-", "-", "-", 8, "-", 7, "-", "-"],
["-", 8, 7, "-", "-", 5, 1, "-", 9]],
:final_board=>
[[2, 9, 3, 5, 4, 1, 6, 8, 7],
[7, 1, 8, 2, 9, 6, 4, 5, 3],
[6, 5, 4, 7, 3, 8, 9, 1, 2],
[9, 7, 2, 8, 1, 3, 5, 6, 4],
[8, 4, 6, 9, 5, 2, 3, 7, 1],
[5, 3, 1, 4, 6, 7, 2, 9, 8],
[3, 6, 9, 1, 7, 4, 8, 2, 5],
[1, 2, 5, 3, 8, 9, 7, 4, 6],
[4, 8, 7, 6, 2, 5, 1, 3, 9]],
:duration=>0.003094,
:starting_dash_count=>46,
:ending_dash_count=>0},
{:round=>2,
:rounds=>15,
:solved=>true,
:starting_board=>
[["-", "-", 5, "-", 3, "-", "-", 8, 1],
[9, "-", 2, 8, 5, "-", "-", 6, "-"],
[6, "-", "-", "-", "-", 4, "-", 5, "-"],
["-", "-", 7, 4, "-", 2, 8, 3, "-"],
[3, 4, 9, 7, 6, "-", "-", "-", 5],
["-", "-", 8, 3, "-", "-", 4, 9, "-"],
[1, 5, "-", "-", 8, 7, "-", "-", 2],
["-", 9, "-", "-", "-", "-", 6, "-", "-"],
["-", 2, 6, "-", 4, 9, 5, "-", 3]],
:final_board=>
[[4, 7, 5, 9, 3, 6, 2, 8, 1],
[9, 3, 2, 8, 5, 1, 7, 6, 4],
[6, 8, 1, 2, 7, 4, 3, 5, 9],
[5, 1, 7, 4, 9, 2, 8, 3, 6],
[3, 4, 9, 7, 6, 8, 1, 2, 5],
[2, 6, 8, 3, 1, 5, 4, 9, 7],
[1, 5, 3, 6, 8, 7, 9, 4, 2],
[7, 9, 4, 5, 2, 3, 6, 1, 8],
[8, 2, 6, 1, 4, 9, 5, 7, 3]],
:duration=>0.001546,
:starting_dash_count=>41,
:ending_dash_count=>0},
{:round=>1,
:rounds=>15,
:solved=>true,
:starting_board=>
[[1, "-", 5, 8, "-", 2, "-", "-", "-"],
["-", 9, "-", "-", 7, 6, 4, "-", 5],
[2, "-", "-", 4, "-", "-", 8, 1, 9],
["-", 1, 9, "-", "-", 7, 3, "-", 6],
[7, 6, 2, "-", 8, 3, "-", 9, "-"],
["-", "-", "-", "-", 6, 1, "-", 5, "-"],
["-", "-", 7, 6, "-", "-", "-", 3, "-"],
[4, 3, "-", "-", 2, "-", 5, "-", 1],
[6, "-", "-", 3, "-", 8, 9, "-", "-"]],
:final_board=>
[[1, 4, 5, 8, 9, 2, 6, 7, 3],
[8, 9, 3, 1, 7, 6, 4, 2, 5],
[2, 7, 6, 4, 3, 5, 8, 1, 9],
[5, 1, 9, 2, 4, 7, 3, 8, 6],
[7, 6, 2, 5, 8, 3, 1, 9, 4],
[3, 8, 4, 9, 6, 1, 7, 5, 2],
[9, 5, 7, 6, 1, 4, 2, 3, 8],
[4, 3, 8, 7, 2, 9, 5, 6, 1],
[6, 2, 1, 3, 5, 8, 9, 4, 7]],
:duration=>0.001523,
:starting_dash_count=>41,
:ending_dash_count=>0}
]
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/4rlm/sudoku_genius. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the SudokuGenius project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.