15 puzzle solver: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 39:
{{trans|Nim}}
 
<syntaxhighlight lang="11l">-V
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]
Line 131:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang=AArch64"aarch64 Assemblyassembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program puzzle15solvex64.s */
Line 1,172:
=={{header|Ada}}==
{{trans|C++}} Decoding actually...
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO;
 
procedure Puzzle_15 is
Line 1,369:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang=ARM"arm Assemblyassembly">
/* ARM assembly Raspberry PI */
/* program puzzle15solver.s */
Line 2,332:
=={{header|C}}==
===IDA*===
<syntaxhighlight lang=C"c">
/**@file HybridIDA.c
* @brief solve 4x4 sliding puzzle with IDA* algorithm
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.
====The Solver====
<syntaxhighlight lang="cpp">
// Solve Random 15 Puzzles : Nigel Galloway - October 18th., 2017
class fifteenSolver{
Line 2,704:
 
====The Task====
<syntaxhighlight lang="cpp">
int main (){
fifteenSolver start(8,0xfe169b4c0a73d852);
Line 2,718:
 
====Extra Credit====
<syntaxhighlight lang="cpp">
int main (){
fifteenSolver start(0,0x0c9dfbae37254861);
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.
 
<syntaxhighlight lang="lisp">;;; Using a priority queue for the A* search
(eval-when (:load-toplevel :compile-toplevel :execute)
(ql:quickload "pileup"))
Line 3,088:
=={{header|F_Sharp|F#}}==
===The Function===
<syntaxhighlight lang="fsharp">
// 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|]
Line 3,113:
</syntaxhighlight>
===The Task===
<syntaxhighlight lang="fsharp">
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)
Line 3,128:
 
The code tested with gforth 0.7.2. It required a 64-bit system.
<syntaxhighlight lang="forth">
#! /usr/bin/gforth
 
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.
 
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"fortran">IF (NR.EQ.4) THEN
code specialised for NR = 4
ELSE IF (NR.EQ.3) THEN
Line 3,301:
 
===Source===
<syntaxhighlight lang=Fortran"fortran"> SUBROUTINE PROUST(T) !Remembrance of time passed.
DOUBLE PRECISION T !The time, in seconds. Positive only, please.
DOUBLE PRECISION S !A copy I can mess with.
Line 4,571:
=={{header|Go}}==
{{trans|C++}}
<syntaxhighlight lang="go">package main
 
import "fmt"
Line 4,821:
=={{header|Julia}}==
{{trans|C++}}
<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]
 
Line 5,050:
=={{header|Lua}}==
===Original===
<syntaxhighlight lang=Lua"lua">
#!/usr/bin/lua
--[[
Line 5,281:
 
===Alternate (with extra credit)===
<syntaxhighlight lang=Lua"lua">
----------
-- SOLVER
Line 5,538:
{{trans|C++}}
====The solver====
<syntaxhighlight lang=Nim"nim">
# 15 puzzle.
 
Line 5,682:
 
====The task====
<syntaxhighlight lang=Nim"nim">
let start = getTime()
var solver = initSolver([Value 15, 14, 1, 6,
Line 5,697:
</pre>
====Extra credit====
<syntaxhighlight lang=Nim"nim">
let start = getTime()
var solver = initSolver([Value 0, 12, 9, 13,
Line 5,714:
=={{header|Pascal}}==
===The Solver===
<syntaxhighlight lang="pascal">
unit FifteenSolverT;
\\ Solve 15 Puzzle. Nigel Galloway; February 1st., 2019.
Line 5,747:
end.
</syntaxhighlight>
<syntaxhighlight lang="pascal">
// Threaded use of 15 solver Unit. Nigel Galloway; February 1st., 2019.
program testFifteenSolver;
Line 5,797:
 
=={{header|Picat}}==
<syntaxhighlight lang=Picat"picat">import planner.
 
main =>
Line 5,863:
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">use strict;
no warnings;
 
Line 5,929:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"phix">-->
<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>
Line 6,082:
<!--</syntaxhighlight>-->
 
<!--<syntaxhighlight lang=Phix"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;">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,240:
-- (clearly optimal by exhaustively disproving all lesser n)
 
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<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>
Line 6,383:
Works with PowerBASIC 6 Console Compiler
{{trans|Go}}
<syntaxhighlight lang=PowerBASIC"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).
' Most of initialization is done in procedure fifteenSolver(), so it's possible to call it many times from the main function.
Line 6,860:
Modified to run this task's 52 move problem.
 
<syntaxhighlight lang=Python"python">
import random
 
Line 7,066:
===A* with good heuristic===
;File - astar.py
<syntaxhighlight lang=Python"python">
"""
 
Line 7,911:
;File - testone.py
 
<syntaxhighlight lang=Python"python">
"""
 
Line 7,973:
</pre>
=={{header|Racket}}==
<syntaxhighlight lang=Racket"racket">
#lang racket
 
Line 8,298:
=={{header|Raku}}==
{{trans|C++}}
<syntaxhighlight lang=perl6"raku" line># 20210623 Raku programming solution vCSMt9hkak0
 
constant \Nr = <3 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3>;
Line 8,374:
=={{header|Rust}}==
{{trans|C++}}
<syntaxhighlight lang=Rust"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];
 
Line 8,581:
 
=== alternative ===
<syntaxhighlight lang=Rust"rust">use keyed_priority_queue::KeyedPriorityQueue;
use std::cmp::{Ord, Ordering, PartialOrd, Reverse};
 
Line 8,769:
 
===Implementation===
<syntaxhighlight lang=Scala"scala">import scala.collection.mutable
case class Board(table: Array[Int], r: Int, c: Int, parent: List[String] = List()) {
def cloneSwap(r: Int, c: Int, rr: Int, cc: Int) = {
Line 8,914:
{{libheader|Wren-long}}
This works but is very slow (132 seconds).
<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]
10,327

edits