Solve a Rubik's cube: Difference between revisions

Content added Content deleted
m (Thundergnat moved page Solve a Rubik's Cube to Solve a Rubik's cube: capitalization policy)
m (syntax highlighting fixup automation)
Line 23: Line 23:


For the single line example, typical timings are around 240 milliseconds which is much faster than Kotlin due, no doubt, to JVM warm up time.
For the single line example, typical timings are around 240 milliseconds which is much faster than Kotlin due, no doubt, to JVM warm up time.
<lang go>/**********************************************************************
<syntaxhighlight lang="go">/**********************************************************************
*
*
* A cube 'state' is an int array with 40 entries, the first 20
* A cube 'state' is an int array with 40 entries, the first 20
Line 310: Line 310:
fmt.Println("\nAverage number of moves =", float64(aggregateMoves)/float64(lineCount))
fmt.Println("\nAverage number of moves =", float64(aggregateMoves)/float64(lineCount))
fmt.Println("\nAverage time =", elapsedTime/int64(lineCount), "milliseconds")
fmt.Println("\nAverage time =", elapsedTime/int64(lineCount), "milliseconds")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 320: Line 320:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang julia>#=**********************************************************************
<syntaxhighlight lang="julia">#=**********************************************************************
*
*
* A cube 'state' is a vector<int> with 40 entries, the first 20
* A cube 'state' is a vector<int> with 40 entries, the first 20
Line 511: Line 511:


pochmann("rubikdata.txt")
pochmann("rubikdata.txt")
</lang>{{out}}
</syntaxhighlight>{{out}}
Using the 100-line database:
Using the 100-line database:
<pre style="height:45ex">
<pre style="height:45ex">
Line 627: Line 627:


To aid readability I've also inserted spaces between each move in the results and added the total moves needed for each line.
To aid readability I've also inserted spaces between each move in the results and added the total moves needed for each line.
<lang scala>// version 1.2.21
<syntaxhighlight lang="scala">// version 1.2.21


/**********************************************************************
/**********************************************************************
Line 859: Line 859:
println("\nAverage number of moves = ${aggregateMoves.toDouble() / lineCount}")
println("\nAverage number of moves = ${aggregateMoves.toDouble() / lineCount}")
println("\nAverage time = ${elapsedTime / lineCount} milliseconds")
println("\nAverage time = ${elapsedTime / lineCount} milliseconds")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 987: Line 987:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<syntaxhighlight lang="nim">#[
<lang Nim>#[
**********************************************************************
**********************************************************************
*
*
Line 1,190: Line 1,190:
let elapsedTime = cpuTime() - startTime
let elapsedTime = cpuTime() - startTime
echo &"\nAverage number of moves = {aggregateMoves / lineCount}"
echo &"\nAverage number of moves = {aggregateMoves / lineCount}"
echo &"\nAverage time = {elapsedTime * 1000 / lineCount.toFloat:.2f} milliseconds"</lang>
echo &"\nAverage time = {elapsedTime * 1000 / lineCount.toFloat:.2f} milliseconds"</syntaxhighlight>


{{out}}
{{out}}
Line 1,319: Line 1,319:
The final stage (pll) would probably benefit the most from being replaced with standard algorithms.<br>
The final stage (pll) would probably benefit the most from being replaced with standard algorithms.<br>
While technically this works under pwa/p2js, you should expect a blank screen for nearly 3 minutes.
While technically this works under pwa/p2js, you should expect a blank screen for nearly 3 minutes.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\rubik_cfop.exw
-- demo\rosetta\rubik_cfop.exw
Line 1,708: Line 1,708:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{Out}}
{{Out}}
The "hardest case" from http://www.cube20.org with a high threshold. You can try this manually.
The "hardest case" from http://www.cube20.org with a high threshold. You can try this manually.
Line 1,774: Line 1,774:
Translation/de-golf(hrumph) of Tomas Sirgedas' winning entry from http://tomas.rokicki.com/cubecontest as held in 2004.<br>
Translation/de-golf(hrumph) of Tomas Sirgedas' winning entry from http://tomas.rokicki.com/cubecontest as held in 2004.<br>
Faster and shorter solutions (in most cases) than cfop, however probably nigh on impossible to debug/enhance...
Faster and shorter solutions (in most cases) than cfop, however probably nigh on impossible to debug/enhance...
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\rubik_tomas.exw
-- demo\rosetta\rubik_tomas.exw
Line 1,990: Line 1,990:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s (%d move%s)\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tomas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"UL DL RF UB FD BR DB UF DR UR BL FL FDR BLU DLB URB RUF FLD BRD FUL"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s (%d move%s)\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tomas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"UL DL RF UB FD BR DB UF DR UR BL FL FDR BLU DLB URB RUF FLD BRD FUL"</span><span style="color: #0000FF;">))</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,003: Line 2,003:


Despite this, the script is taking an average of just over a second to calculate the number of moves for each line which is probably not too bad for an interpreted language.
Despite this, the script is taking an average of just over a second to calculate the number of moves for each line which is probably not too bad for an interpreted language.
<lang ecmascript>/**********************************************************************
<syntaxhighlight lang="ecmascript">/**********************************************************************
*
*
* A cube 'state' is an int array with 40 entries, the first 20
* A cube 'state' is an int array with 40 entries, the first 20
Line 2,260: Line 2,260:
var elapsedTime = ((endTime - startTime) * 1000).round
var elapsedTime = ((endTime - startTime) * 1000).round
System.print("\nAverage number of moves = %(aggregateMoves/lineCount)")
System.print("\nAverage number of moves = %(aggregateMoves/lineCount)")
System.print("\nAverage time = %(elapsedTime/lineCount) milliseconds")</lang>
System.print("\nAverage time = %(elapsedTime/lineCount) milliseconds")</syntaxhighlight>


{{out}}
{{out}}