Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 18 Next »

This page describes a Groovy solution for ShortestSudokuSolver. Check the link for all the details, but basically the puzzle state is fed in as a String. Each line of the script must be no more than 80 characters in length.

The solution (210 characters plus 2 newlines):


  • The script could be 26 characters shorter if Groovy supported integer division.
  • The script could be 2 characters shorter using print rather than println if you didn't care about formatting
  • To make the function stop as soon as it finds the first solution (proper puzzles will only have one solution), the else becomes else println a;System.exit(1)

Add the following line to the script to solve a partially complete puzzle (should take just a few seconds):

Alternatively, add the following line to the script to solve a puzzle from scratch (may take 30-60 minutes or more):

The expected output is:

Here is the more usual representation of the puzzle:

You can run it from the command-line by adding ;r args[0] to the end of the script (saved in a file called suduko.groovy) and then invoking:

A slightly longer version without the g closure trick (213 characters plus 2 newlines):

Also see Another version for a more understandable (though much longer) algorithm.

  • No labels