15 puzzle solver: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 39: Line 39:
{{trans|Nim}}
{{trans|Nim}}


<lang 11l>-V
<syntaxhighlight lang=11l>-V
nr = [3, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3]
nr = [3, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3]
nc = [3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2]
nc = [3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2]
Line 122: Line 122:
0, 10, 7, 3,
0, 10, 7, 3,
13, 8, 5, 2])
13, 8, 5, 2])
solver.run()</lang>
solver.run()</syntaxhighlight>


{{out}}
{{out}}
Line 131: Line 131:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<lang AArch64 Assembly>
<syntaxhighlight lang=AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program puzzle15solvex64.s */
/* program puzzle15solvex64.s */
Line 1,151: Line 1,151:
/* for this file see task include a file in language AArch64 assembly */
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
<pre>
<pre>
File name : casR1.txt
File name : casR1.txt
Line 1,172: Line 1,172:
=={{header|Ada}}==
=={{header|Ada}}==
{{trans|C++}} Decoding actually...
{{trans|C++}} Decoding actually...
<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang=Ada>with Ada.Text_IO;


procedure Puzzle_15 is
procedure Puzzle_15 is
Line 1,363: Line 1,363:
(0, 10, 7, 3),
(0, 10, 7, 3),
(13, 8, 5, 2)));
(13, 8, 5, 2)));
end Puzzle_15;</lang>
end Puzzle_15;</syntaxhighlight>
{{out}}
{{out}}
<pre>Solved in 52 moves: RRRULDDLUUULDRURDDDRULLULURRRDDLDLUURDDLULURRULDRDRD</pre>
<pre>Solved in 52 moves: RRRULDDLUUULDRURDDDRULLULURRRDDLDLUURDDLULURRULDRDRD</pre>
Line 1,369: Line 1,369:
=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<lang ARM Assembly>
<syntaxhighlight lang=ARM Assembly>
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program puzzle15solver.s */
/* program puzzle15solver.s */
Line 2,311: Line 2,311:
/***************************************************/
/***************************************************/
.include "../affichage.inc"
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
<pre>
Nom du fichier : casR1.txt
Nom du fichier : casR1.txt
Line 2,332: Line 2,332:
=={{header|C}}==
=={{header|C}}==
===IDA*===
===IDA*===
<syntaxhighlight lang=C>
<lang C>
/**@file HybridIDA.c
/**@file HybridIDA.c
* @brief solve 4x4 sliding puzzle with IDA* algorithm
* @brief solve 4x4 sliding puzzle with IDA* algorithm
Line 2,619: Line 2,619:
}
}


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,659: Line 2,659:
[http://www.rosettacode.org/wiki/15_puzzle_solver/20_Random see] for an analysis of 20 randomly generated 15 puzzles solved with this solver.
[http://www.rosettacode.org/wiki/15_puzzle_solver/20_Random see] for an analysis of 20 randomly generated 15 puzzles solved with this solver.
====The Solver====
====The Solver====
<lang cpp>
<syntaxhighlight lang=cpp>
// Solve Random 15 Puzzles : Nigel Galloway - October 18th., 2017
// Solve Random 15 Puzzles : Nigel Galloway - October 18th., 2017
class fifteenSolver{
class fifteenSolver{
Line 2,701: Line 2,701:
void Solve(){for(;not fY();++_n);}
void Solve(){for(;not fY();++_n);}
};
};
</syntaxhighlight>
</lang>


====The Task====
====The Task====
<lang cpp>
<syntaxhighlight lang=cpp>
int main (){
int main (){
fifteenSolver start(8,0xfe169b4c0a73d852);
fifteenSolver start(8,0xfe169b4c0a73d852);
start.Solve();
start.Solve();
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,718: Line 2,718:


====Extra Credit====
====Extra Credit====
<lang cpp>
<syntaxhighlight lang=cpp>
int main (){
int main (){
fifteenSolver start(0,0x0c9dfbae37254861);
fifteenSolver start(0,0x0c9dfbae37254861);
start.Solve();
start.Solve();
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,737: Line 2,737:
Using an A* search algorithm which is good enough for the first task. I increased SBCL's dynamic memory to 2GB for the code to run smoothly.
Using an A* search algorithm which is good enough for the first task. I increased SBCL's dynamic memory to 2GB for the code to run smoothly.


<lang lisp>;;; Using a priority queue for the A* search
<syntaxhighlight lang=lisp>;;; Using a priority queue for the A* search
(eval-when (:load-toplevel :compile-toplevel :execute)
(eval-when (:load-toplevel :compile-toplevel :execute)
(ql:quickload "pileup"))
(ql:quickload "pileup"))
Line 3,068: Line 3,068:
(format t "Found the shortest path in ~D steps and ~3,2F seconds~%" result time))
(format t "Found the shortest path in ~D steps and ~3,2F seconds~%" result time))
(print-state goal-state))
(print-state goal-state))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 3,088: Line 3,088:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
===The Function===
===The Function===
<lang fsharp>
<syntaxhighlight lang=fsharp>
// A Naive 15 puzzle solver using no memory. Nigel Galloway: October 6th., 2017
// A Naive 15 puzzle solver using no memory. Nigel Galloway: October 6th., 2017
let Nr,Nc = [|3;0;0;0;0;1;1;1;1;2;2;2;2;3;3;3|],[|3;0;1;2;3;0;1;2;3;0;1;2;3;0;1;2|]
let Nr,Nc = [|3;0;0;0;0;1;1;1;1;2;2;2;2;3;3;3|],[|3;0;1;2;3;0;1;2;3;0;1;2;3;0;1;2|]
Line 3,111: Line 3,111:
solve {i=n;g=[L];e=g;l=0}
solve {i=n;g=[L];e=g;l=0}
let n = Seq.collect fN n
let n = Seq.collect fN n
</syntaxhighlight>
</lang>
===The Task===
===The Task===
<lang fsharp>
<syntaxhighlight lang=fsharp>
let test n g=match [1..15]|>Seq.tryPick(solve n g) with
let test n g=match [1..15]|>Seq.tryPick(solve n g) with
Some n->n|>List.rev|>List.iter(fun n->printf "%c" (match n with N->'d'|I->'u'|G->'r'|E->'l'|L->'\u0000'));printfn " (%n moves)" (List.length n)
Some n->n|>List.rev|>List.iter(fun n->printf "%c" (match n with N->'d'|I->'u'|G->'r'|E->'l'|L->'\u0000'));printfn " (%n moves)" (List.length n)
|_ ->printfn "No solution found"
|_ ->printfn "No solution found"
test 0xfe169b4c0a73d852UL 8
test 0xfe169b4c0a73d852UL 8
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,128: Line 3,128:


The code tested with gforth 0.7.2. It required a 64-bit system.
The code tested with gforth 0.7.2. It required a 64-bit system.
<lang forth>
<syntaxhighlight lang=forth>
#! /usr/bin/gforth
#! /usr/bin/gforth


Line 3,194: Line 3,194:
\ -1 0 hex 123456789afbde0c decimal 14 solve \ some trivial case, 3 moves
\ -1 0 hex 123456789afbde0c decimal 14 solve \ some trivial case, 3 moves
bye
bye
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 3,289: Line 3,289:
(The source below has the ABS outside the MOD: I don't care [[Talk:Carmichael_3_strong_pseudoprimes|what sort of mod]] is used here, just that negative numbers be as scrambled as positive) In both cases the array indexing is checkable by the compiler at compile time so that there need be no run-time checking on that. Such bound checking may be not as strict as might be expected when EQUIVALENCE tricks are involved. In tests with a variant board size of 3x4, the board position array was declared BOARD(N) where N = 12, but was still equivalenced to BORED(4) and so still allowed room for sixteen elements in BOARD. Subroutine UNPACK was not written to deal with anything other than a 4x4 board and so accessed elements 13, 14, 15, and 16 of BOARD that were outside its declared upper bound of 12, but no run-time objection was made. Similarly with a 3x3 board.
(The source below has the ABS outside the MOD: I don't care [[Talk:Carmichael_3_strong_pseudoprimes|what sort of mod]] is used here, just that negative numbers be as scrambled as positive) In both cases the array indexing is checkable by the compiler at compile time so that there need be no run-time checking on that. Such bound checking may be not as strict as might be expected when EQUIVALENCE tricks are involved. In tests with a variant board size of 3x4, the board position array was declared BOARD(N) where N = 12, but was still equivalenced to BORED(4) and so still allowed room for sixteen elements in BOARD. Subroutine UNPACK was not written to deal with anything other than a 4x4 board and so accessed elements 13, 14, 15, and 16 of BOARD that were outside its declared upper bound of 12, but no run-time objection was made. Similarly with a 3x3 board.


Granted a flexible pre-processor scheme (as in pl/i, say) one could imagine a menu of tricks being selected from according to the board shape specified, but without such a facility, the constants are merely named rather than literal. Any change, such as to a 3x4 board, can be made by adjusting the appropriate PARAMETER and many usages will adjust accordingly. Others will require adjustment by the diligent programmer for good results. In the absence of a pre-processor one could present the various possible code sequences surrounded by tests as in <lang Fortran>IF (NR.EQ.4) THEN
Granted a flexible pre-processor scheme (as in pl/i, say) one could imagine a menu of tricks being selected from according to the board shape specified, but without such a facility, the constants are merely named rather than literal. Any change, such as to a 3x4 board, can be made by adjusting the appropriate PARAMETER and many usages will adjust accordingly. Others will require adjustment by the diligent programmer for good results. In the absence of a pre-processor one could present the various possible code sequences surrounded by tests as in <syntaxhighlight lang=Fortran>IF (NR.EQ.4) THEN
code specialised for NR = 4
code specialised for NR = 4
ELSE IF (NR.EQ.3) THEN
ELSE IF (NR.EQ.3) THEN
code specialised for NR = 3
code specialised for NR = 3
END IF</lang>
END IF</syntaxhighlight>
and hope that the compiler would carry forward the actual value of <code>NR</code> into the IF-statement's conditional expression, recognise that the result is also constant (for the current compilation with a particular value of <code>NR</code>) and so generate code only for the case that the expression came out as ''true'', without any test to select this being employed in the compiled code. This soon becomes a tangle of combinations, and has not been attempted. And there could be difficulties too. One wonders if, say, with NR = 3, the specialised code for the NR = 4 case could contain a mention of element 4 of an array which is actually of size NR. Would the compiler regard this as an error, given that it will be discarding this code anyway? Even if one used NR rather than a literal such as 4 there could still be problems, such as code calling for a division by (NR - 3).
and hope that the compiler would carry forward the actual value of <code>NR</code> into the IF-statement's conditional expression, recognise that the result is also constant (for the current compilation with a particular value of <code>NR</code>) and so generate code only for the case that the expression came out as ''true'', without any test to select this being employed in the compiled code. This soon becomes a tangle of combinations, and has not been attempted. And there could be difficulties too. One wonders if, say, with NR = 3, the specialised code for the NR = 4 case could contain a mention of element 4 of an array which is actually of size NR. Would the compiler regard this as an error, given that it will be discarding this code anyway? Even if one used NR rather than a literal such as 4 there could still be problems, such as code calling for a division by (NR - 3).


Line 3,301: Line 3,301:


===Source===
===Source===
<lang Fortran> SUBROUTINE PROUST(T) !Remembrance of time passed.
<syntaxhighlight lang=Fortran> SUBROUTINE PROUST(T) !Remembrance of time passed.
DOUBLE PRECISION T !The time, in seconds. Positive only, please.
DOUBLE PRECISION T !The time, in seconds. Positive only, please.
DOUBLE PRECISION S !A copy I can mess with.
DOUBLE PRECISION S !A copy I can mess with.
Line 3,829: Line 3,829:
CALL PURPLE HAZE(FNAME(1:14))
CALL PURPLE HAZE(FNAME(1:14))


END</lang>
END</syntaxhighlight>


===The Results===
===The Results===
Line 4,571: Line 4,571:
=={{header|Go}}==
=={{header|Go}}==
{{trans|C++}}
{{trans|C++}}
<lang go>package main
<syntaxhighlight lang=go>package main


import "fmt"
import "fmt"
Line 4,812: Line 4,812:
fifteenSolver(8, 0xfe169b4c0a73d852)
fifteenSolver(8, 0xfe169b4c0a73d852)
solve()
solve()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 4,821: Line 4,821:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|C++}}
{{trans|C++}}
<lang julia>const Nr = [3, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3]
<syntaxhighlight lang=julia>const Nr = [3, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3]
const Nc = [3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2]
const Nc = [3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2]


Line 5,035: Line 5,035:
run() = (N0[1] = 8; _n[1] = 1; N2[1] = 0xfe169b4c0a73d852; solve(0))
run() = (N0[1] = 8; _n[1] = 1; N2[1] = 0xfe169b4c0a73d852; solve(0))
run()
run()
</lang>
</syntaxhighlight>
{{output}} <pre>
{{output}} <pre>
next iteration, _n[1] will be 2...
next iteration, _n[1] will be 2...
Line 5,050: Line 5,050:
=={{header|Lua}}==
=={{header|Lua}}==
===Original===
===Original===
<lang Lua>
<syntaxhighlight lang=Lua>
#!/usr/bin/lua
#!/usr/bin/lua
--[[
--[[
Line 5,275: Line 5,275:
print_tiles(Goal)
print_tiles(Goal)
solve(Tiles);
solve(Tiles);
</syntaxhighlight>
</lang>
{{output}} <pre>
{{output}} <pre>
FOUND SOLUTION of length 52 rrrulddluuuldrurdddrullulurrrddldluurddlulurruldrdrd
FOUND SOLUTION of length 52 rrrulddluuuldrurdddrullulurrrddldluurddlulurruldrdrd
Line 5,281: Line 5,281:


===Alternate (with extra credit)===
===Alternate (with extra credit)===
<lang Lua>
<syntaxhighlight lang=Lua>
----------
----------
-- SOLVER
-- SOLVER
Line 5,485: Line 5,485:
print("ELAPSED: " .. (os.clock()-sclock) .. "s")
print("ELAPSED: " .. (os.clock()-sclock) .. "s")
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 5,538: Line 5,538:
{{trans|C++}}
{{trans|C++}}
====The solver====
====The solver====
<lang Nim>
<syntaxhighlight lang=Nim>
# 15 puzzle.
# 15 puzzle.


Line 5,679: Line 5,679:
ms = ms mod d
ms = ms mod d
if ms > 0: result.add(' ')
if ms > 0: result.add(' ')
</syntaxhighlight>
</lang>


====The task====
====The task====
<lang Nim>
<syntaxhighlight lang=Nim>
let start = getTime()
let start = getTime()
var solver = initSolver([Value 15, 14, 1, 6,
var solver = initSolver([Value 15, 14, 1, 6,
Line 5,690: Line 5,690:
solver.run()
solver.run()
echo fmt"Execution time: {(getTime() - start).toString}."
echo fmt"Execution time: {(getTime() - start).toString}."
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 5,697: Line 5,697:
</pre>
</pre>
====Extra credit====
====Extra credit====
<lang Nim>
<syntaxhighlight lang=Nim>
let start = getTime()
let start = getTime()
var solver = initSolver([Value 0, 12, 9, 13,
var solver = initSolver([Value 0, 12, 9, 13,
Line 5,705: Line 5,705:
solver.run()
solver.run()
echo fmt"Execution time: {(getTime() - start).toString}."
echo fmt"Execution time: {(getTime() - start).toString}."
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 5,714: Line 5,714:
=={{header|Pascal}}==
=={{header|Pascal}}==
===The Solver===
===The Solver===
<lang pascal>
<syntaxhighlight lang=pascal>
unit FifteenSolverT;
unit FifteenSolverT;
\\ Solve 15 Puzzle. Nigel Galloway; February 1st., 2019.
\\ Solve 15 Puzzle. Nigel Galloway; February 1st., 2019.
Line 5,746: Line 5,746:
end;
end;
end.
end.
</syntaxhighlight>
</lang>
<lang pascal>
<syntaxhighlight lang=pascal>
// Threaded use of 15 solver Unit. Nigel Galloway; February 1st., 2019.
// Threaded use of 15 solver Unit. Nigel Galloway; February 1st., 2019.
program testFifteenSolver;
program testFifteenSolver;
Line 5,779: Line 5,779:
end;
end;
end.
end.
</syntaxhighlight>
</lang>
===The Task===
===The Task===
{{out}}
{{out}}
Line 5,797: Line 5,797:


=={{header|Picat}}==
=={{header|Picat}}==
<lang Picat>import planner.
<syntaxhighlight lang=Picat>import planner.


main =>
main =>
Line 5,854: Line 5,854:
{(R,C),(FR,FC)} in zip(Tiles,FTiles)]).
{(R,C),(FR,FC)} in zip(Tiles,FTiles)]).


</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 5,863: Line 5,863:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang=perl>use strict;
no warnings;
no warnings;


Line 5,924: Line 5,924:


($N0[0], $N2[0]) = (8, 0xfe169b4c0a73d852); # initial state
($N0[0], $N2[0]) = (8, 0xfe169b4c0a73d852); # initial state
while () { fY() or ++$m }</lang>
while () { fY() or ++$m }</syntaxhighlight>
{{out}}
{{out}}
<pre>Solution found in 52 moves: rrrulddluuuldrurdddrullulurrrddldluurddlulurruldrdrd</pre>
<pre>Solution found in 52 moves: rrrulddluuuldrurdddrullulurrrddldluurddlulurruldrdrd</pre>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>-->
<!--<syntaxhighlight lang=Phix>-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Solve15puzzle.exw</span>
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Solve15puzzle.exw</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">STM</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> <span style="color: #000080;font-style:italic;">-- single-tile metrics.</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">STM</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> <span style="color: #000080;font-style:italic;">-- single-tile metrics.</span>
Line 6,080: Line 6,080:
<span style="color: #0000FF;">{<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">}<span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- down</span>
<span style="color: #0000FF;">{<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">}<span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- down</span>
<span style="color: #0000FF;">{<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">}<span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- right
<span style="color: #0000FF;">{<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">0<span style="color: #0000FF;">}<span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- right
<!--</lang>-->
<!--</syntaxhighlight>-->


<!--<lang Phix>-->
<!--<syntaxhighlight lang=Phix>-->
<span style="color: #008080;">function</span> <span style="color: #000000;">iddfs<span style="color: #0000FF;">(<span style="color: #004080;">integer</span> <span style="color: #000000;">step<span style="color: #0000FF;">,</span> <span style="color: #000000;">lim<span style="color: #0000FF;">,</span> <span style="color: #000000;">space<span style="color: #0000FF;">,</span> <span style="color: #000000;">prevmv<span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">iddfs<span style="color: #0000FF;">(<span style="color: #004080;">integer</span> <span style="color: #000000;">step<span style="color: #0000FF;">,</span> <span style="color: #000000;">lim<span style="color: #0000FF;">,</span> <span style="color: #000000;">space<span style="color: #0000FF;">,</span> <span style="color: #000000;">prevmv<span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">time<span style="color: #0000FF;">(<span style="color: #0000FF;">)<span style="color: #0000FF;">><span style="color: #000000;">t1</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">time<span style="color: #0000FF;">(<span style="color: #0000FF;">)<span style="color: #0000FF;">><span style="color: #000000;">t1</span> <span style="color: #008080;">then</span>
Line 6,223: Line 6,223:
<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 style="color: #0000FF;">(<span style="color: #0000FF;">)
<span style="color: #000000;">main<span style="color: #0000FF;">(<span style="color: #0000FF;">)
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 6,240: Line 6,240:
-- (clearly optimal by exhaustively disproving all lesser n)
-- (clearly optimal by exhaustively disproving all lesser n)


<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo/rosetta/Solve15puzzle_simple.exw</span>
<span style="color: #000080;font-style:italic;">-- demo/rosetta/Solve15puzzle_simple.exw</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">left<span style="color: #0000FF;">,</span> <span style="color: #000000;">down<span style="color: #0000FF;">,</span> <span style="color: #000000;">up<span style="color: #0000FF;">,</span> <span style="color: #000000;">right</span> <span style="color: #000080;font-style:italic;">-- (nb 5-move flips it, as in down===5-up, etc)</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">left<span style="color: #0000FF;">,</span> <span style="color: #000000;">down<span style="color: #0000FF;">,</span> <span style="color: #000000;">up<span style="color: #0000FF;">,</span> <span style="color: #000000;">right</span> <span style="color: #000080;font-style:italic;">-- (nb 5-move flips it, as in down===5-up, etc)</span>
Line 6,368: Line 6,368:
<span style="color: #7060A8;">printf<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #008000;">"solution of %d moves found in %s: %s\n"<span style="color: #0000FF;">,</span>
<span style="color: #7060A8;">printf<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #008000;">"solution of %d moves found in %s: %s\n"<span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{<span style="color: #7060A8;">length<span style="color: #0000FF;">(<span style="color: #000000;">moves<span style="color: #0000FF;">)<span style="color: #0000FF;">,<span style="color: #7060A8;">elapsed<span style="color: #0000FF;">(<span style="color: #7060A8;">time<span style="color: #0000FF;">(<span style="color: #0000FF;">)<span style="color: #0000FF;">-<span style="color: #000000;">t0<span style="color: #0000FF;">)<span style="color: #0000FF;">,<span style="color: #000000;">moves<span style="color: #0000FF;">}<span style="color: #0000FF;">)
<span style="color: #0000FF;">{<span style="color: #7060A8;">length<span style="color: #0000FF;">(<span style="color: #000000;">moves<span style="color: #0000FF;">)<span style="color: #0000FF;">,<span style="color: #7060A8;">elapsed<span style="color: #0000FF;">(<span style="color: #7060A8;">time<span style="color: #0000FF;">(<span style="color: #0000FF;">)<span style="color: #0000FF;">-<span style="color: #000000;">t0<span style="color: #0000FF;">)<span style="color: #0000FF;">,<span style="color: #000000;">moves<span style="color: #0000FF;">}<span style="color: #0000FF;">)
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
uppercase indicates the non-free moves (in manhattan cost terms).
uppercase indicates the non-free moves (in manhattan cost terms).
Line 6,383: Line 6,383:
Works with PowerBASIC 6 Console Compiler
Works with PowerBASIC 6 Console Compiler
{{trans|Go}}
{{trans|Go}}
<lang PowerBASIC>' The program solve fe169b4c0a73d852 in 4-5 seconds (on Intel Core i7-3770 3.40 GHz, 16 GB RAM, Windows 10 Pro).
<syntaxhighlight lang=PowerBASIC>' The program solve fe169b4c0a73d852 in 4-5 seconds (on Intel Core i7-3770 3.40 GHz, 16 GB RAM, Windows 10 Pro).
' Test not completed with 0c9dfbae37254861 (still running after about 4 hours).
' Test not completed with 0c9dfbae37254861 (still running after about 4 hours).
' Most of initialization is done in procedure fifteenSolver(), so it's possible to call it many times from the main function.
' Most of initialization is done in procedure fifteenSolver(), so it's possible to call it many times from the main function.
Line 6,850: Line 6,850:
'call fifteenSolver(&h0c9dfbae37254861, 1000)
'call fifteenSolver(&h0c9dfbae37254861, 1000)
end function
end function
</syntaxhighlight>
</lang>


=={{header|Python}}==
=={{header|Python}}==
Line 6,860: Line 6,860:
Modified to run this task's 52 move problem.
Modified to run this task's 52 move problem.


<lang Python>
<syntaxhighlight lang=Python>
import random
import random


Line 7,047: Line 7,047:
print(", ".join({-1: "Left", 1: "Right", -4: "Up", 4: "Down"}[move[1]] for move in moves))
print(", ".join({-1: "Left", 1: "Right", -4: "Up", 4: "Down"}[move[1]] for move in moves))
print(cost, num_eval)
print(cost, num_eval)
</syntaxhighlight>
</lang>


Output - this solution of the problem for this task is the same as the second solution:
Output - this solution of the problem for this task is the same as the second solution:
Line 7,066: Line 7,066:
===A* with good heuristic===
===A* with good heuristic===
;File - astar.py
;File - astar.py
<lang Python>
<syntaxhighlight lang=Python>
"""
"""


Line 7,907: Line 7,907:
return strpath
return strpath
</syntaxhighlight>
</lang>


;File - testone.py
;File - testone.py


<lang Python>
<syntaxhighlight lang=Python>
"""
"""


Line 7,950: Line 7,950:
print(" ")
print(" ")
print("Run time in seconds: "+str(after - before))
print("Run time in seconds: "+str(after - before))
</syntaxhighlight>
</lang>
Output:
Output:


Line 7,973: Line 7,973:
</pre>
</pre>
=={{header|Racket}}==
=={{header|Racket}}==
<lang Racket>
<syntaxhighlight lang=Racket>
#lang racket
#lang racket


Line 8,294: Line 8,294:
(module+ main
(module+ main
(print-path (A* initial-state)))
(print-path (A* initial-state)))
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
{{trans|C++}}
{{trans|C++}}
<lang perl6># 20210623 Raku programming solution vCSMt9hkak0
<syntaxhighlight lang=perl6># 20210623 Raku programming solution vCSMt9hkak0


constant \Nr = <3 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3>;
constant \Nr = <3 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3>;
Line 8,366: Line 8,366:
fifteenSolver(8,0xfe169b4c0a73d852);
fifteenSolver(8,0xfe169b4c0a73d852);
loop { fY() or ++$m }
loop { fY() or ++$m }
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 8,374: Line 8,374:
=={{header|Rust}}==
=={{header|Rust}}==
{{trans|C++}}
{{trans|C++}}
<lang Rust>const NR: [i32; 16] = [3, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3];
<syntaxhighlight lang=Rust>const NR: [i32; 16] = [3, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3];
const NC: [i32; 16] = [3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2];
const NC: [i32; 16] = [3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2];


Line 8,575: Line 8,575:
fn main() {
fn main() {
FifteenSolver::new(8, 0xfe169b4c0a73d852).solve();
FifteenSolver::new(8, 0xfe169b4c0a73d852).solve();
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 8,581: Line 8,581:


=== alternative ===
=== alternative ===
<lang Rust>use keyed_priority_queue::KeyedPriorityQueue;
<syntaxhighlight lang=Rust>use keyed_priority_queue::KeyedPriorityQueue;
use std::cmp::{Ord, Ordering, PartialOrd, Reverse};
use std::cmp::{Ord, Ordering, PartialOrd, Reverse};


Line 8,746: Line 8,746:
closed_states.push(parent_order, Reverse(parent_state));
closed_states.push(parent_order, Reverse(parent_state));
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 8,769: Line 8,769:


===Implementation===
===Implementation===
<lang Scala>import scala.collection.mutable
<syntaxhighlight lang=Scala>import scala.collection.mutable
case class Board(table: Array[Int], r: Int, c: Int, parent: List[String] = List()) {
case class Board(table: Array[Int], r: Int, c: Int, parent: List[String] = List()) {
def cloneSwap(r: Int, c: Int, rr: Int, cc: Int) = {
def cloneSwap(r: Int, c: Int, rr: Int, cc: Int) = {
Line 8,866: Line 8,866:
val startHard = Board(Array(Array(0, 12, 9, 13), Array(15, 11, 10, 14), Array(3, 7, 2, 5), Array(4, 8, 6, 1)).flatten, 0, 0)
val startHard = Board(Array(Array(0, 12, 9, 13), Array(15, 11, 10, 14), Array(3, 7, 2, 5), Array(4, 8, 6, 1)).flatten, 0, 0)
}
}
</syntaxhighlight>
</lang>


===Results===
===Results===
Line 8,914: Line 8,914:
{{libheader|Wren-long}}
{{libheader|Wren-long}}
This works but is very slow (132 seconds).
This works but is very slow (132 seconds).
<lang ecmascript>import "/long" for ULong
<syntaxhighlight lang=ecmascript>import "/long" for ULong


var Nr = [3, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3]
var Nr = [3, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3]
Line 9,088: Line 9,088:


fifteenSolver.call(8, ULong.fromBaseString("fe169b4c0a73d852", 16))
fifteenSolver.call(8, ULong.fromBaseString("fe169b4c0a73d852", 16))
solve.call()</lang>
solve.call()</syntaxhighlight>


{{out}}
{{out}}