Sudoku: Difference between revisions
Content deleted Content added
m →{{header|Groovy}}: cosmetic changes |
m →{{header|Groovy}}: cleanup |
||
Line 2,469:
}
def
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 slots = (0..8).collect { i -> (0..8).findAll { j -> ! (grid[i][j] in CELL_VALUES) } \
▲ .collect {j -> [i: i, j: j] } }.flatten()
}
Line 2,491 ⟶ 2,490:
def solve
solve = { grid ->
def slots =
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)) {
throw new GridException('Invalid Sudoku Grid, underdetermined slots: ' + slots)
}
grid
Line 2,558:
[2, 1, 4, 8, 3, 7, 5, 9, 6]
ELAPSED: 0.
PUZZLE
Line 2,582:
[4, 7, 3, 2, 5, 8, 1, 9, 6]
ELAPSED:
=={{header|Haskell}}==
|