Sudoku: Difference between revisions

Content deleted Content added
m →‎{{header|Groovy}}: cosmetic changes
Line 2,469:
}
 
def assignCandidatesslotList = { grid, slots ->
def slots = (0..8).collect { i -> (0..8).findAll { j -> ! (grid[i][j] in CELL_VALUES) } \
.collect {j -> [i: i, j: j] } }.flatten()
 
def assignCandidates = { grid, slots = slotList(grid) ->
slots.each { slot ->
def unavailable = [gridRow, gridCol, gridBox].collect { it(grid, slot) }.sum() as Set
Line 2,476 ⟶ 2,481:
slots.sort { - it.candidates.size() }
if (slots && ! slots[-1].candidates) {
throw new GridException('Invalid Sudoku Grid, overdetermined slot: ' + slots[-1])
}
slots
 
def slotList = { grid ->
def slots = (0..8).collect { i -> (0..8).findAll { j -> ! (grid[i][j] in CELL_VALUES) } \
.collect {j -> [i: i, j: j] } }.flatten()
assignCandidates(grid, slots)
}
 
Line 2,491 ⟶ 2,490:
def solve
solve = { grid ->
def slots = slotListassignCandidates(grid)
if (! slots) { return grid }
while (slots[-1].candidates.size() == 1) {
Line 2,497 ⟶ 2,496:
grid[slot.i][slot.j] = slot.candidates[0]
if (! slots) { return grid }
slots = assignCandidates(grid, slots)
}
if (! slots) { return grid }
Line 2,513 ⟶ 2,512:
}
if (!isSolved(grid)) {
throwslots new= GridExceptionassignCandidates('Invalid Sudoku Grid'grid)
throw new GridException('Invalid Sudoku Grid, underdetermined slots: ' + slots)
}
grid
Line 2,558:
[2, 1, 4, 8, 3, 7, 5, 9, 6]
 
ELAPSED: 0.572581 seconds
 
PUZZLE
Line 2,582:
[4, 7, 3, 2, 5, 8, 1, 9, 6]
 
ELAPSED: 3540.739484 seconds</pre>
 
=={{header|Haskell}}==