Solve a Rubik's cube: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Phix}}: syntax coloured)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(3 intermediate revisions by 2 users not shown)
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.
<langsyntaxhighlight lang="go">/**********************************************************************
*
* A cube 'state' is an int array with 40 entries, the first 20
Line 310:
fmt.Println("\nAverage number of moves =", float64(aggregateMoves)/float64(lineCount))
fmt.Println("\nAverage time =", elapsedTime/int64(lineCount), "milliseconds")
}</langsyntaxhighlight>
 
{{out}}
Line 320:
=={{header|Julia}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="julia">#=**********************************************************************
*
* A cube 'state' is a vector<int> with 40 entries, the first 20
Line 511:
 
pochmann("rubikdata.txt")
</langsyntaxhighlight>{{out}}
Using the 100-line database:
<pre style="height:45ex">
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.
<langsyntaxhighlight lang="scala">// version 1.2.21
 
/**********************************************************************
Line 859:
println("\nAverage number of moves = ${aggregateMoves.toDouble() / lineCount}")
println("\nAverage time = ${elapsedTime / lineCount} milliseconds")
}</langsyntaxhighlight>
 
{{out}}
Line 987:
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang="nim">#[
<lang Nim>#[
**********************************************************************
*
Line 1,190:
let elapsedTime = cpuTime() - startTime
echo &"\nAverage number of moves = {aggregateMoves / lineCount}"
echo &"\nAverage time = {elapsedTime * 1000 / lineCount.toFloat:.2f} milliseconds"</langsyntaxhighlight>
 
{{out}}
Line 1,319:
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.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\rubik_cfop.exw
Line 1,708:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{Out}}
The "hardest case" from http://www.cube20.org with a high threshold. You can try this manually.
Line 1,774:
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...
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\rubik_tomas.exw
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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,997:
The distributed copy of demo\rosetta\rubik_cfop.exw also contains routines to convert between my 136-character cube and reid notation,
and demo\rosetta\rubik_tomas.exw also contains the full 100-long test set from the original competition.
 
=={{header|Raku}}==
This is a translation of the [https://tomas.rokicki.com/cubecontest/stefan2.txt Perl] [https://tomas.rokicki.com/cubecontest/ competition] code, by Stefan Pochmann.
<syntaxhighlight lang="raku" line># 20230401 Raku programming solution
 
my @data=<UL DL RF UB FD BR DB UF DR UR BL FL FDR BLU DLB URB RUF FLD BRD FUL>;
my @goal=<UF UR UB UL DF DR DB DL FR FL BR BL UFR URB UBL ULF DRF DFL DLB DBR>;
 
sub printAlg ($x) { #--- Algorithms.
 
my $algo = 'x0F0DN0EB0H00N0B0R0KB0L0QB0G1A1M11C1I1E1OFI2'~
'DN2IEOB2H22N2B2GRM2MKGB2GLM2QBK23D3EN3E3B3N33H3';
$algo ~~ s:g
!(\D*)(\d)!{
$0 ~ <ILOFCLKHRNQCL OBIRALOBIRAL CEJHPEIMIG DFNRHRDKMQ>[$1]
# ~ [~] reverse map { TR/G..R/M..X/ }, " $0".comb }!;
~ " $0".trans( ['G'..'R'] => ['M'..'X'] ).flip }!;
 
my @algo = $algo.split: ' ';
 
#`[[[ or use the following to save some CPU power and time # my @algo = <
xILOFCLKHRNQCLx FILOFCLKHRNQCLF DNILOFCLKHRNQCLTD EBILOFCLKHRNQCLBE
HILOFCLKHRNQCLN ILOFCLKHRNQCL NILOFCLKHRNQCLT BILOFCLKHRNQCLB
RILOFCLKHRNQCLX KBILOFCLKHRNQCLBQ LILOFCLKHRNQCLR QBILOFCLKHRNQCLBW
GOBIRALOBIRALM AOBIRALOBIRALA MOBIRALOBIRALS OBIRALOBIRAL
COBIRALOBIRALC IOBIRALOBIRALO EOBIRALOBIRALE OFICEJHPEIMIGOFU
DNCEJHPEIMIGTD IEOBCEJHPEIMIGBUEO HCEJHPEIMIGN CEJHPEIMIG
NCEJHPEIMIGT BCEJHPEIMIGB GRMCEJHPEIMIGSXM MKGBCEJHPEIMIGBMQS
GLMCEJHPEIMIGSRM QBKCEJHPEIMIGQBW DFNRHRDKMQ DDFNRHRDKMQD
ENDFNRHRDKMQTE EDFNRHRDKMQE BDFNRHRDKMQB NDFNRHRDKMQT
DFNRHRDKMQ HDFNRHRDKMQN >; #]]]
 
say [~] my @moves = map {
substr('UDFBLR', (my $ord=.ord-65) % 6, 1) ~ substr("2 '", $ord div 6, 1)
}, @algo[$x].comb;
return @moves.elems
}
 
my $total = 0;
 
for 1..18 -> $x { #--- Orient.
until @data[$x] ∈ @goal {
$total += printAlg $x;
@data[$x] ~~ s/(.)(.+)/$1$0/;
@data[$x < 12 ?? 0 !! 19] ~~ s/(.+)(.)/$1$0/;
}
}
 
for ^41 { #--- Permute.
for ^20 -> $w {
next if @data[$w] eq @goal[$w];
my $x = 0;
until @data[$w] eq @goal[$x] { $x++ };
$x < 12 ?? ( @data[0,$x,12,15] = @data[$x,0,15,12] )
!! ( @data[12,$x] = @data[$x,12] );
$total += printAlg $x+=18 and last
}
}
 
say "\nTotal number of moves : $total";</syntaxhighlight>
{{out}}
<pre>B2D'F R F'R2F2R L D R'D'L'F2R DB2
D F R F'R2F2R L D R'D'L'F2R D'
U F'D2F R'U2R F'D2F R'U2R U'
U2F'D2F R'U2R F'D2F R'U2R U2
U2F'D2F R'U2R F'D2F R'U2R U2
F2F'D2F R'U2R F'D2F R'U2R F2
F F'D2F R'U2R F'D2F R'U2R F'
F F'D2F R'U2R F'D2F R'U2R F'
L2F'D2F R'U2R F'D2F R'U2R L2
L2F'D2F R'U2R F'D2F R'U2R L2
F L2F'D2F2L2B D B'L2F U'F U D2FL2F'
B2D'F2L2B D B'L2F U'F U DB2
U R'U'F2L2B D B'L2F U'F U URU'
F2L2B D B'L2F U'F U
U R U'F2L2B D B'L2F U'F U UR'U'
L'D2L F2L2B D B'L2F U'F U L'D2L
U'L U D2F2L2B D B'L2F U'F U D2U'L'U
F'R2F F2L2B D B'L2F U'F U F'R2F
D2F2L2B D B'L2F U'F U D2
B2B2R2D'R'D R'B2L U'L'B2
L2D'B2R2D'R'D R'B2L U'L'DL2
B2R2D'R'D R'B2L U'L'
D B2R2D'R'D R'B2L U'L'D'
L2B2R2D'R'D R'B2L U'L'L2
D2B2R2D'R'D R'B2L U'L'D2
 
Total number of moves : 352</pre>
 
=={{header|Wren}}==
Line 2,003 ⟶ 2,092:
 
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.
<langsyntaxhighlight ecmascriptlang="wren">/**********************************************************************
*
* A cube 'state' is an int array with 40 entries, the first 20
Line 2,260 ⟶ 2,349:
var elapsedTime = ((endTime - startTime) * 1000).round
System.print("\nAverage number of moves = %(aggregateMoves/lineCount)")
System.print("\nAverage time = %(elapsedTime/lineCount) milliseconds")</langsyntaxhighlight>
 
{{out}}
9,485

edits