From e679acbceced108b57d6ea504bf090824fa9d2a4 Mon Sep 17 00:00:00 2001 From: Joachim Date: Tue, 28 Aug 2018 19:24:47 +0200 Subject: [PATCH] edgemodel now uses intarray, it's faster! --- src/main/kotlin/be/nielandt/CrossSolver.kt | 5 +- .../kotlin/be/nielandt/CrossSolverIterator.kt | 2 +- src/main/kotlin/be/nielandt/EdgeModel.kt | 108 +++++++++++------- src/main/kotlin/be/nielandt/Move.kt | 4 +- src/test/kotlin/be/nielandt/EdgeModelTest.kt | 4 +- 5 files changed, 73 insertions(+), 50 deletions(-) diff --git a/src/main/kotlin/be/nielandt/CrossSolver.kt b/src/main/kotlin/be/nielandt/CrossSolver.kt index a6c03f0..5c88849 100644 --- a/src/main/kotlin/be/nielandt/CrossSolver.kt +++ b/src/main/kotlin/be/nielandt/CrossSolver.kt @@ -30,7 +30,8 @@ open abstract class CrossSolver { /** * Let's look for symmetries. * - * base times: 72s, 29s, 61s + * - no changes: base times: 72s, 29s, 61s + * - changed list in edgemodel to intarray: 51s, 21s, 35s */ fun main(args: Array) { @@ -78,7 +79,7 @@ fun main(args: Array) { println("Scramble: ${moves.map { decodeMove(it) }}") - val usedModel = EdgeModel(moves) + val usedModel = EdgeModel.withMoves(moves) println(usedModel) // val baseSolve = CrossSolverBase().solveCrossesTimed(usedModel) diff --git a/src/main/kotlin/be/nielandt/CrossSolverIterator.kt b/src/main/kotlin/be/nielandt/CrossSolverIterator.kt index 56c125d..5ca7633 100644 --- a/src/main/kotlin/be/nielandt/CrossSolverIterator.kt +++ b/src/main/kotlin/be/nielandt/CrossSolverIterator.kt @@ -45,7 +45,7 @@ class CrossSolverIterator : CrossSolver() { } fun main(args: Array) { - val start = EdgeModel().doMoves(randomMoves(20)) + val start = EdgeModel.solved().doMoves(randomMoves(20)) val solver = CrossSolverIterator() val solveCrossesTimed = solver.solveCrossesTimed(start) solveCrossesTimed.forEach { t, u -> diff --git a/src/main/kotlin/be/nielandt/EdgeModel.kt b/src/main/kotlin/be/nielandt/EdgeModel.kt index 985d75d..bceb268 100644 --- a/src/main/kotlin/be/nielandt/EdgeModel.kt +++ b/src/main/kotlin/be/nielandt/EdgeModel.kt @@ -26,50 +26,15 @@ package be.nielandt class EdgeModel { - val model: Array + lateinit var model: IntArray - private val F_indices = intArrayOf(13, 18, 7, 20, 3, 0, 1, 2) - private val B_indices = intArrayOf(8, 9, 10, 11, 16, 15, 22, 5) - private val L_indices = intArrayOf(3, 23, 9, 19, 12, 13, 14, 15) - private val U_indices = intArrayOf(16, 17, 18, 19, 0, 12, 8, 4) - private val D_indices = intArrayOf(2, 6, 10, 14, 20, 21, 22, 23) - private val R_indices = intArrayOf(4, 5, 6, 7, 1, 17, 11, 21) + /** + * Create an edgemodel through the companion object. + */ + private constructor() - constructor() { - // do a sanity check - val entries = mutableListOf(F_indices, B_indices, L_indices, U_indices, D_indices, R_indices).flatMap { it.asList() }.groupBy { it }.entries -// println("entries = ${entries}") - if (entries.any { - it.value.size != 2 - }) { - throw RuntimeException("each index should occur exactly twice in the arrays") - } - - model = arrayOf( - GREEN, GREEN, GREEN, GREEN, - RED, RED, RED, RED, - BLUE, BLUE, BLUE, BLUE, - ORANGE, ORANGE, ORANGE, ORANGE, - WHITE, WHITE, WHITE, WHITE, - YELLOW, YELLOW, YELLOW, YELLOW - ) - } - - constructor(randomMoves: Int) { - val edgeModel = EdgeModel() - val r: IntArray = randomMoves(randomMoves) - val doMoves = edgeModel.doMoves(r.toList()) - this.model = doMoves.model - } - - constructor(model: Array) { - this.model = model - } - - constructor(moves: List) { - val edgeModel = EdgeModel() - val newModel = edgeModel.doMoves(moves) - this.model = newModel.model + fun copyOf(model: EdgeModel): EdgeModel { + return EdgeModel.withModel(model.model) } /** @@ -146,7 +111,7 @@ class EdgeModel { D_ -> prime(D_indices) D2 -> double(D_indices) } - return EdgeModel(copyOf) + return EdgeModel.withModel(copyOf) } /** @@ -221,5 +186,62 @@ class EdgeModel { } } } + + companion object { + + private val F_indices = intArrayOf(13, 18, 7, 20, 3, 0, 1, 2) + private val B_indices = intArrayOf(8, 9, 10, 11, 16, 15, 22, 5) + private val L_indices = intArrayOf(3, 23, 9, 19, 12, 13, 14, 15) + private val U_indices = intArrayOf(16, 17, 18, 19, 0, 12, 8, 4) + private val D_indices = intArrayOf(2, 6, 10, 14, 20, 21, 22, 23) + private val R_indices = intArrayOf(4, 5, 6, 7, 1, 17, 11, 21) + + fun solved(): EdgeModel { + val edgeModel = EdgeModel() + // do a sanity check + val entries = mutableListOf(F_indices, B_indices, L_indices, U_indices, D_indices, R_indices).flatMap { it.asList() }.groupBy { it }.entries +// println("entries = ${entries}") + if (entries.any { + it.value.size != 2 + }) { + throw RuntimeException("each index should occur exactly twice in the arrays") + } + + val model = intArrayOf( + GREEN, GREEN, GREEN, GREEN, + RED, RED, RED, RED, + BLUE, BLUE, BLUE, BLUE, + ORANGE, ORANGE, ORANGE, ORANGE, + WHITE, WHITE, WHITE, WHITE, + YELLOW, YELLOW, YELLOW, YELLOW + ) + edgeModel.model = model + return edgeModel + } + + fun withRandomMoves(randomMoves: Int): EdgeModel { + val edgeModel = EdgeModel() + val r: IntArray = randomMoves(randomMoves) + val doMoves = edgeModel.doMoves(r.toList()) + edgeModel.model = doMoves.model + return edgeModel + } + + fun withModel(model: IntArray): EdgeModel { + val edgeModel = EdgeModel() + edgeModel.model = model + return edgeModel + } + + /** + * Pass a list of moves and do them on a solved edgemodel. + */ + fun withMoves(moves: IntArray): EdgeModel { + val edgeModel = EdgeModel.solved() + val newModel = edgeModel.doMoves(moves) + edgeModel.model = newModel.model + return edgeModel + } + } } diff --git a/src/main/kotlin/be/nielandt/Move.kt b/src/main/kotlin/be/nielandt/Move.kt index e6a9750..9677c04 100644 --- a/src/main/kotlin/be/nielandt/Move.kt +++ b/src/main/kotlin/be/nielandt/Move.kt @@ -58,8 +58,8 @@ fun classOf(move: Int): Int { return (move%6) / 2 } -fun parseMoves(s: String): List { - return s.split(" ", ",", ";").filter { it?.length > 0 }.map { parseMove(it) }.toList() +fun parseMoves(s: String): IntArray { + return s.split(" ", ",", ";").filter { it?.length > 0 }.map { parseMove(it) }.toIntArray() } fun parseMove(s: String): Int { diff --git a/src/test/kotlin/be/nielandt/EdgeModelTest.kt b/src/test/kotlin/be/nielandt/EdgeModelTest.kt index 5a905db..cb59cc8 100644 --- a/src/test/kotlin/be/nielandt/EdgeModelTest.kt +++ b/src/test/kotlin/be/nielandt/EdgeModelTest.kt @@ -7,7 +7,7 @@ class EdgeModelTest { @Test fun testSingleMove() { - val edgeModel = EdgeModel() + val edgeModel = EdgeModel.solved() (0..5).forEach { color -> assertTrue(edgeModel.crossSolved(color)) } @@ -15,7 +15,7 @@ class EdgeModelTest { @Test fun testSingleMoves() { - val edgeModel = EdgeModel() + val edgeModel = EdgeModel.solved() println(edgeModel) val final = edgeModel.doMove(F) println(final)