Add TODOs

This commit is contained in:
2024-07-16 05:14:59 +02:00
parent db37835aa4
commit 7190e4faef

View File

@@ -24,6 +24,7 @@ fun generateGame(size: Int = 6): Game {
// If i >= n, we are done. The set of clues is minimal. // If i >= n, we are done. The set of clues is minimal.
while (i < clues.size) { while (i < clues.size) {
// Run your solver on the reduced set of clues and count the number of possible solutions. // Run your solver on the reduced set of clues and count the number of possible solutions.
// TODO: first do binary search
val temp = clues - clues[i] val temp = clues - clues[i]
if (solve(grid.toGrid(), temp) == SOLVABLE) { if (solve(grid.toGrid(), temp) == SOLVABLE) {
// If there is exactly one solution, update clues and reset index. // If there is exactly one solution, update clues and reset index.
@@ -63,6 +64,7 @@ internal fun solve(
} catch (e: UnsolvablePuzzleException) { } catch (e: UnsolvablePuzzleException) {
return NO_SOLUTION return NO_SOLUTION
} }
// TODO clean up groups in rows (?)
grid.forEach { row -> row.forEach { cell -> row.cleanupOptions(cell) } } grid.forEach { row -> row.forEach { cell -> row.cleanupOptions(cell) } }
// grid.forEach { row -> // grid.forEach { row ->
// removedOptions = row.tryOptionsForClues(grid, otherClues) || removedOptions // removedOptions = row.tryOptionsForClues(grid, otherClues) || removedOptions
@@ -88,46 +90,12 @@ internal fun solve(
return MULTIPLE_SOLUTIONS return MULTIPLE_SOLUTIONS
} }
internal fun <C : ItemClass<C>> GameCell<C>.countSolutions(
grid: Grid,
clues: Collection<Clue>
): Int {
val options = options.toList()
var solvableCount = 0
for (option in options) {
selection = option
if (solve(grid, clues) == SOLVABLE) {
solvableCount++
}
}
selection = null
this.options.addAll(options)
return solvableCount
}
internal enum class PuzzleSolution { internal enum class PuzzleSolution {
NO_SOLUTION, NO_SOLUTION,
SOLVABLE, SOLVABLE,
MULTIPLE_SOLUTIONS MULTIPLE_SOLUTIONS
} }
fun <C : ItemClass<C>> GameRow<C>.tryOptionsForClues(grid: Grid, clues: List<Clue>): Boolean {
var removedOptions = false
filter { cell -> cell.selection == null }.forEach { cell ->
val removed = cell.options.removeIf { option ->
cell.selection = option
clues.any { it.isRuleViolated(grid) }
}
cell.selection = null
if (removed) {
cleanupOptions(cell)
}
removedOptions = removedOptions || removed
}
return removedOptions
}
fun <C : ItemClass<C>> GameRow<C>.cleanupOptions(cell: GameCell<C>) { fun <C : ItemClass<C>> GameRow<C>.cleanupOptions(cell: GameCell<C>) {
if (cell.options.size == 1 && cell.selection == null) { if (cell.options.size == 1 && cell.selection == null) {
cell.selection = cell.options.first() cell.selection = cell.options.first()