Skip to content

Commit

Permalink
Extracts methods
Browse files Browse the repository at this point in the history
  • Loading branch information
meikpiep committed Aug 2, 2023
1 parent bdc017a commit 5a4d8ef
Showing 1 changed file with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class MathDokuDLX(
)
}

var dlx: DLX? = null
private var dlx: DLX? = null
private var constraints = emptyList<BooleanArray>()

private var numberOfCages = 0


private fun initializeDLX() {
// Number of columns = number of constraints =
// BOARD * BOARD (for columns) +
Expand Down Expand Up @@ -58,13 +58,17 @@ class MathDokuDLX(

numberOfCages = creators.size + extraRectingularCages

val constraints = constraintsFromCages(creators) + constraintsFromRectangular(creators)
constraints = constraintsFromCages(creators) + constraintsFromRectangular(creators)

dlx = DLX(
possibleDigits.size * (grid.gridSize.width + grid.gridSize.height) + numberOfCages,
numberOfNodes
)

logConstraints(constraints)
}

private fun logConstraints(constraints: List<BooleanArray>) {
if (logger.isInfoEnabled) {
val headerCellId = StringBuilder()
val headerValue = StringBuilder()
Expand All @@ -89,24 +93,25 @@ class MathDokuDLX(

logger.info { headerValue.toString() }
logger.info { headerCellId.toString() }
}

for ((currentCombination, constraint) in constraints.withIndex()) {
if (logger.isInfoEnabled) {
for (constraint in constraints) {
val constraintInfo = StringBuilder()

constraint.forEach { constraintInfo.append(if (it) { "*" } else { "-" }.padEnd(4)) }
constraint.forEach {
constraintInfo.append(
if (it) {
"*"
} else {
"-"
}.padEnd(4)
)
}

logger.info { constraintInfo.toString() }
}

for (constraintIndex in constraint.indices) {
if (constraint[constraintIndex]) {
dlx!!.addNode(constraintIndex, currentCombination)
}
}
}
}

private fun cartesianProduct(values: List<Int>, numberOfCopies: Int): Set<Set<Int>> {
return UniqueIndexSetsOfGivenLength(values, numberOfCopies).calculateProduct()
}
Expand Down Expand Up @@ -219,6 +224,14 @@ class MathDokuDLX(
fun solve(type: DLX.SolveType): Int {
initializeDLX()

for ((currentCombination, constraint) in constraints.withIndex()) {
for (constraintIndex in constraint.indices) {
if (constraint[constraintIndex]) {
dlx!!.addNode(constraintIndex, currentCombination)
}
}
}

return dlx!!.solve(type)
}
}

0 comments on commit 5a4d8ef

Please sign in to comment.