diff --git a/src/main/kotlin/be/nielandt/CrossSolver.kt b/src/main/kotlin/be/nielandt/CrossSolver.kt index 6783b47..978e690 100644 --- a/src/main/kotlin/be/nielandt/CrossSolver.kt +++ b/src/main/kotlin/be/nielandt/CrossSolver.kt @@ -44,18 +44,12 @@ enum class Move { } /** - * Six colors + * Convert a color into a letter. */ -const val WHITE: Short = 0 -const val YELLOW: Short = 1 -const val ORANGE: Short = 2 -const val RED: Short = 3 -const val GREEN: Short = 4 -const val BLUE: Short = 5 val COLOR_LETTER = arrayOf("W", "Y", "O", "R", "G", "B") -fun l(colorIndex: Short): String { - return COLOR_LETTER[colorIndex.toInt()] +fun l(c: Color): String { + return c.name.first().toString() } fun main(args: Array) { @@ -97,33 +91,19 @@ fun main(args: Array) { // doAllCrossMoveCounts(scrambledModel) val allCrossMoveCount = allCrossMoveCount(scrambledModel) - allCrossMoveCount.forEachIndexed { index, list -> - println("cross for color: ${colorName(index.toShort())} in ${list?.size}: ${list?.joinToString(" ")}") - } -} - -fun colorName(color: Short): String { - return when (color) { - WHITE -> "white" - YELLOW -> "yellow" - RED -> "red" - BLUE -> "blue" - GREEN -> "green" - ORANGE -> "orange" - else -> { - "?" - } + allCrossMoveCount.forEach { color, moves -> + println("cross for color: ${color} in ${moves.size}: ${moves.joinToString(" ")}") } } fun doAllCrossMoveCounts(edgeModel: EdgeModel) { - for (i in 0 until 6) { - val crossMoveCount = crossMoveCount(edgeModel, i.toShort()) - println("${colorName(i.toShort())} in ${crossMoveCount?.size}: ${crossMoveCount?.joinToString()}") + for (c: Color in Color.values()) { + val crossMoveCount = crossMoveCount(edgeModel, c) + println("${c} in ${crossMoveCount?.size}: ${crossMoveCount?.joinToString()}") } } -fun crossMoveCount(edgeModel: EdgeModel, color: Short): List? { +fun crossMoveCount(edgeModel: EdgeModel, color: Color): List? { val moveCounts = Array?>(6) { null } for (moveCount in 1..8) { @@ -146,9 +126,9 @@ fun crossMoveCount(edgeModel: EdgeModel, color: Short): List? { return null } -fun allCrossMoveCount(edgeModel: EdgeModel): Array?> { +fun allCrossMoveCount(edgeModel: EdgeModel): Map> { val start = Instant.now() - val moveCounts = Array?>(6) { null } + val moveCounts = mutableMapOf>() for (moveCount in 1..8) { // build a counter of moveCount big @@ -161,23 +141,23 @@ fun allCrossMoveCount(edgeModel: EdgeModel): Array?> { // execute the moves val afterMoves = edgeModel.doMoves(moves) // check crosses that have not been found yet - moveCounts.forEachIndexed { index, list -> - if (list == null) { - val crossSolved = afterMoves.crossSolved(index.toShort()) - if (crossSolved) { - moveCounts[index] = moves - } - } + Color.values().forEach { color -> + if(!moveCounts.containsKey(color)) { + val crossSolved = afterMoves.crossSolved(color) + if (crossSolved) { + moveCounts[color] = moves + } + } } - if (moveCounts.all { it != null }) { - println("Execution time: ${Duration.between(start, Instant.now()).toSeconds()}s") + if (moveCounts.keys.size == Color.values().size) { + println("Execution time: ${Duration.between(start, Instant.now()).toMillis() / 1000}s") return@allCrossMoveCount moveCounts } } while (counter.increase()) } - println("Execution time: ${Duration.between(start, Instant.now()).toSeconds()}s") + println("Execution time: ${Duration.between(start, Instant.now()).toMillis() / 1000}s") return moveCounts } diff --git a/src/main/kotlin/be/nielandt/EdgeModel.kt b/src/main/kotlin/be/nielandt/EdgeModel.kt index dfbb88c..bb9e472 100644 --- a/src/main/kotlin/be/nielandt/EdgeModel.kt +++ b/src/main/kotlin/be/nielandt/EdgeModel.kt @@ -1,5 +1,6 @@ package be.nielandt + /** * 24 edge-faces (a single part of an edge), so 24 pieces that can have a color - ---------- @@ -25,7 +26,7 @@ package be.nielandt class EdgeModel { - val model: ShortArray + val model: Array private val F = intArrayOf(13, 18, 7, 20, 3, 0, 1, 2) private val B = intArrayOf(8, 9, 10, 11, 16, 15, 22, 5) @@ -44,13 +45,14 @@ class EdgeModel { throw RuntimeException("each index should occur exactly twice in the arrays") } - model = shortArrayOf( - GREEN, GREEN, GREEN, GREEN, - RED, RED, RED, RED, - BLUE, BLUE, BLUE, BLUE, - ORANGE, ORANGE, ORANGE, ORANGE, - WHITE, WHITE, WHITE, WHITE, - YELLOW, YELLOW, YELLOW, YELLOW) + model = arrayOf( + Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, + Color.RED, Color.RED, Color.RED, Color.RED, + Color.BLUE, Color.BLUE, Color.BLUE, Color.BLUE, + Color.ORANGE, Color.ORANGE, Color.ORANGE, Color.ORANGE, + Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE, + Color.YELLOW, Color.YELLOW, Color.YELLOW, Color.YELLOW + ) } constructor(randomMoves: Int) { @@ -59,7 +61,7 @@ class EdgeModel { this.model = doMoves.model } - constructor(model: ShortArray) { + constructor(model: Array) { this.model = model } @@ -180,66 +182,33 @@ class EdgeModel { return this.doMoves(f.toList()) } - fun crossSolved(color: Short): Boolean { - return when(color) { - WHITE -> { - model[WB] == WHITE && model[WG] == WHITE && model[WO] == WHITE && model[WR] == WHITE && - model[BW] == BLUE && model[GW] == GREEN && model[OW] == ORANGE && model[RW] == RED + fun crossSolved(color: Color): Boolean { + return when (color) { + Color.WHITE -> { + model[WB] == Color.WHITE && model[WG] == Color.WHITE && model[WO] == Color.WHITE && model[WR] == Color.WHITE && + model[BW] == Color.BLUE && model[GW] == Color.GREEN && model[OW] == Color.ORANGE && model[RW] == Color.RED } - YELLOW -> { - model[YB] == YELLOW && model[YG] == YELLOW && model[YO] == YELLOW && model[YR] == YELLOW && - model[BY] == BLUE && model[GY] == GREEN && model[OY] == ORANGE && model[RY] == RED + Color.YELLOW -> { + model[YB] == Color.YELLOW && model[YG] == Color.YELLOW && model[YO] == Color.YELLOW && model[YR] == Color.YELLOW && + model[BY] == Color.BLUE && model[GY] == Color.GREEN && model[OY] == Color.ORANGE && model[RY] == Color.RED } - RED -> { - model[RW] == RED && model[RG] == RED && model[RY] == RED && model[RB] == RED && - model[WR] == WHITE && model[GR] == GREEN && model[YR] == YELLOW && model[BR] == BLUE + Color.RED -> { + model[RW] == Color.RED && model[RG] == Color.RED && model[RY] == Color.RED && model[RB] == Color.RED && + model[WR] == Color.WHITE && model[GR] == Color.GREEN && model[YR] == Color.YELLOW && model[BR] == Color.BLUE } - BLUE -> { - model[BW] == BLUE && model[BR] == BLUE && model[BY] == BLUE && model[BO] == BLUE && - model[WB] == WHITE && model[RB] == RED && model[YB] == YELLOW && model[OB] == ORANGE + Color.BLUE -> { + model[BW] == Color.BLUE && model[BR] == Color.BLUE && model[BY] == Color.BLUE && model[BO] == Color.BLUE && + model[WB] == Color.WHITE && model[RB] == Color.RED && model[YB] == Color.YELLOW && model[OB] == Color.ORANGE } - GREEN -> { - model[GW] == GREEN && model[GO] == GREEN && model[GY] == GREEN && model[GR] == GREEN && - model[WG] == WHITE && model[OG] == ORANGE && model[YG] == YELLOW && model[RG] == RED + Color.GREEN -> { + model[GW] == Color.GREEN && model[GO] == Color.GREEN && model[GY] == Color.GREEN && model[GR] == Color.GREEN && + model[WG] == Color.WHITE && model[OG] == Color.ORANGE && model[YG] == Color.YELLOW && model[RG] == Color.RED } - ORANGE -> { - model[OW] == ORANGE && model[OB] == ORANGE && model[OY] == ORANGE && model[OG] == ORANGE && - model[WO] == WHITE && model[BO] == BLUE && model[YO] == YELLOW && model[GO] == GREEN - } - else -> { - throw IllegalStateException("6 color for lyfe!") + Color.ORANGE -> { + model[OW] == Color.ORANGE && model[OB] == Color.ORANGE && model[OY] == Color.ORANGE && model[OG] == Color.ORANGE && + model[WO] == Color.WHITE && model[BO] == Color.BLUE && model[YO] == Color.YELLOW && model[GO] == Color.GREEN } } } } -// sticker index constants for edges -const val GO = 3 -const val GW = 0 -const val GR = 1 -const val GY = 2 - -const val WG = 18 -const val WO = 19 -const val WB = 16 -const val WR = 17 - -const val YG = 20 -const val YO = 23 -const val YB = 22 -const val YR = 21 - -const val RG = 7 -const val RW = 4 -const val RB = 5 -const val RY = 6 - -const val BR = 11 -const val BW = 8 -const val BO = 9 -const val BY = 10 - -const val OB = 15 -const val OW = 12 -const val OG = 13 -const val OY = 14 diff --git a/src/main/kotlin/be/nielandt/EdgeModelConstants.kt b/src/main/kotlin/be/nielandt/EdgeModelConstants.kt new file mode 100644 index 0000000..78880cd --- /dev/null +++ b/src/main/kotlin/be/nielandt/EdgeModelConstants.kt @@ -0,0 +1,44 @@ +package be.nielandt + +/** + * These are the indices in the EdgeModel, given a readable name. + * GO = green orange, meaning the green sticker of the green/orange cubie, on the green face + */ +const val GO = 3 +const val GW = 0 +const val GR = 1 +const val GY = 2 + +const val WG = 18 +const val WO = 19 +const val WB = 16 +const val WR = 17 + +const val YG = 20 +const val YO = 23 +const val YB = 22 +const val YR = 21 + +const val RG = 7 +const val RW = 4 +const val RB = 5 +const val RY = 6 + +const val BR = 11 +const val BW = 8 +const val BO = 9 +const val BY = 10 + +const val OB = 15 +const val OW = 12 +const val OG = 13 +const val OY = 14 + +/** + * Six colors + */ +enum class Color(index: Int) { + WHITE(0), YELLOW(1), ORANGE(2), RED(3), GREEN(4), BLUE(5) +} + + diff --git a/src/test/kotlin/be/nielandt/EdgeModelTest.kt b/src/test/kotlin/be/nielandt/EdgeModelTest.kt index d0d525f..93c6eb0 100644 --- a/src/test/kotlin/be/nielandt/EdgeModelTest.kt +++ b/src/test/kotlin/be/nielandt/EdgeModelTest.kt @@ -1,8 +1,7 @@ package be.nielandt -import org.junit.Test - import org.junit.Assert.* +import org.junit.Test class EdgeModelTest { @@ -11,8 +10,8 @@ class EdgeModelTest { // try each move Move.values().forEach { move -> val doMove = EdgeModel().doMove(move) - val count = (0 until 6).map { color -> - val crossSolved = doMove.crossSolved(color.toShort()) + val count = Color.values().map { color -> + val crossSolved = doMove.crossSolved(color) crossSolved }.count { it } assertEquals(1, count) @@ -21,9 +20,9 @@ class EdgeModelTest { @Test fun findAtLeastOneCombo() { - val white = WHITE + val white = Color.WHITE val doMoves = EdgeModel().doMoves(Move.random(20)) - val crossMoveCount = crossMoveCount(doMoves, WHITE) + val crossMoveCount = crossMoveCount(doMoves, Color.WHITE) assertNotNull(crossMoveCount) assertTrue(crossMoveCount!!.size<9) println(doMoves)