Solve triangle solitaire puzzle: Difference between revisions

m
(Added Wren)
m (→‎{{header|Wren}}: Minor tidy)
(28 intermediate revisions by 9 users not shown)
Line 23:
 
Reference picture:   http://www.joenord.com/puzzles/peggame/
<br/>Updated link (June 2021): &nbsp;
https://www.joenord.com/triangle-peg-board-game-solutions-to-amaze-your-friends/
 
 
Line 30 ⟶ 32:
Start with empty peg in &nbsp; '''X''' &nbsp; and solve with one peg in position &nbsp; '''Y'''.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F DrawBoard(board)
V peg = [‘’] * 16
L(n) 1.<16
peg[n] = ‘.’
I n C board
peg[n] = hex(n)
print(‘ #.’.format(peg[1]))
print(‘ #. #.’.format(peg[2], peg[3]))
print(‘ #. #. #.’.format(peg[4], peg[5], peg[6]))
print(‘ #. #. #. #.’.format(peg[7], peg[8], peg[9], peg[10]))
print(‘ #. #. #. #. #.’.format(peg[11], peg[12], peg[13], peg[14], peg[15]))
 
F RemovePeg(&board, n)
board.remove(n)
 
F AddPeg(&board, n)
board.append(n)
 
F IsPeg(board, n)
R n C board
 
V JumpMoves = [1 = [(2, 4), (3, 6)],
2 = [(4, 7), (5, 9)],
3 = [(5, 8), (6, 10)],
4 = [(2, 1), (5, 6), (7, 11), (8, 13)],
5 = [(8, 12), (9, 14)],
6 = [(3, 1), (5, 4), (9, 13), (10, 15)],
7 = [(4, 2), (8, 9)],
8 = [(5, 3), (9, 10)],
9 = [(5, 2), (8, 7)],
10 = [(9, 8)],
11 = [(12, 13)],
12 = [(8, 5), (13, 14)],
13 = [(8, 4), (9, 6), (12, 11), (14, 15)],
14 = [(9, 5), (13, 12)],
15 = [(10, 6), (14, 13)]]
 
[(Int, Int, Int)] Solution
 
F Solve(=board)
I board.len == 1
R board
 
L(peg) 1.<16
I IsPeg(board, peg)
V movelist = JumpMoves[peg]
L(over, land) movelist
I IsPeg(board, over) & !IsPeg(board, land)
V saveboard = copy(board)
RemovePeg(&board, peg)
RemovePeg(&board, over)
AddPeg(&board, land)
 
Solution.append((peg, over, land))
 
board = Solve(board)
I board.len == 1
R board
board = copy(saveboard)
Solution.pop()
 
R board
 
F InitSolve(empty)
V board = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
RemovePeg(&board, empty)
Solve(board)
 
V empty_start = 1
InitSolve(empty_start)
 
V board = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
RemovePeg(&board, empty_start)
L(peg, over, land) Solution
RemovePeg(&board, peg)
RemovePeg(&board, over)
AddPeg(&board, land)
DrawBoard(board)
print("Peg #. jumped over #. to land on #.\n".format(hex(peg), hex(over), hex(land)))</syntaxhighlight>
 
{{out}}
<pre>
1
. 3
. 5 6
7 8 9 A
B C D E F
Peg 4 jumped over 2 to land on 1
 
1
. 3
4 . .
7 8 9 A
B C D E F
Peg 6 jumped over 5 to land on 4
 
.
. .
4 . 6
7 8 9 A
B C D E F
Peg 1 jumped over 3 to land on 6
 
.
2 .
. . 6
. 8 9 A
B C D E F
Peg 7 jumped over 4 to land on 2
 
.
2 .
. 5 6
. . 9 A
B . D E F
Peg C jumped over 8 to land on 5
 
.
2 .
. 5 6
. . 9 A
B C . . F
Peg E jumped over D to land on C
 
.
2 .
. 5 .
. . . A
B C D . F
Peg 6 jumped over 9 to land on D
 
.
. .
. . .
. . 9 A
B C D . F
Peg 2 jumped over 5 to land on 9
 
.
. .
. . .
. . 9 A
B . . E F
Peg C jumped over D to land on E
 
.
. .
. . 6
. . 9 .
B . . E .
Peg F jumped over A to land on 6
 
.
. .
. . .
. . . .
B . D E .
Peg 6 jumped over 9 to land on D
 
.
. .
. . .
. . . .
B C . . .
Peg E jumped over D to land on C
 
.
. .
. . .
. . . .
. . D . .
Peg B jumped over C to land on D
 
</pre>
 
=={{header|D}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="d">import std.stdio, std.array, std.string, std.range, std.algorithm;
 
immutable N = [0,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
Line 85 ⟶ 265:
}
writeln(l.empty ? "No solution found." : l);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 185 ⟶ 365:
0 0 1 0 0
Solved</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
brd$[] = strchars "
┏━━━━━━━━━┓
┃ · ┃
┃ ● ● ┃
┃ ● ● ● ┃
┃ ● ● ● ● ┃
┃● ● ● ● ●┃
┗━━━━━━━━━┛"
proc solve . solution$ .
solution$ = ""
for pos = 1 to len brd$[]
if brd$[pos] = "●"
npegs += 1
for dir in [ -13 -11 2 13 11 -2 ]
if brd$[pos + dir] = "●" and brd$[pos + 2 * dir] = "·"
brd$[pos] = "·"
brd$[pos + dir] = "·"
brd$[pos + 2 * dir] = "●"
solve solution$
brd$[pos] = "●"
brd$[pos + dir] = "●"
brd$[pos + 2 * dir] = "·"
if solution$ <> ""
solution$ = strjoin brd$[] & solution$
return
.
.
.
.
.
if npegs = 1
solution$ = strjoin brd$[]
.
.
solve solution$
print solution$
</syntaxhighlight>
 
=={{header|Elixir}}==
Inspired by Ruby
<langsyntaxhighlight lang="elixir">defmodule IQ_Puzzle do
def task(i \\ 0, n \\ 5) do
fmt = Enum.map_join(1..n, fn i ->
Line 230 ⟶ 450:
end
 
IQ_Puzzle.task</langsyntaxhighlight>
 
{{out}}
Line 335 ⟶ 555:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 449 ⟶ 669:
fmt.Printf("Peg %X jumped over %X to land on %X\n\n", peg, over, land)
}
}</langsyntaxhighlight>
 
{{out}}
Line 457 ⟶ 677:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
NB. This is a direct translation of the python program,
NB. except for the display which by move is horizontal.
Line 639 ⟶ 859:
NB. Solution NB. return Solution however Solution is global.
)
</syntaxhighlight>
</lang>
Example linux session with program in file CrackerBarrel.ijs
<pre>
Line 678 ⟶ 898:
Print one possible solution.
 
<syntaxhighlight lang="java">
<lang Java>
import java.util.ArrayList;
import java.util.Arrays;
Line 866 ⟶ 1,086:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 904 ⟶ 1,124:
=={{header|Julia}}==
{{trans|Raku}}
<langsyntaxhighlight lang="julia">moves = [[1, 2, 4], [1, 3, 6], [2, 4, 7], [2, 5, 9], [3, 5, 8], [3, 6, 10], [4, 5, 6],
[4, 7, 11], [4, 8, 13], [5, 8, 12], [5, 9, 14], [6, 9, 13], [6, 10, 15],
[7, 8, 9], [8, 9, 10], [11, 12, 13], [12, 13, 14], [13, 14, 15]]
Line 940 ⟶ 1,160:
end
end
</langsyntaxhighlight>{{out}}
<pre>
Starting board:
Line 1,043 ⟶ 1,263:
=={{header|Kotlin}}==
{{trans|Python}}
<langsyntaxhighlight lang="scala">// version 1.1.3
 
data class Solution(val peg: Int, val over: Int, val land: Int)
Line 1,118 ⟶ 1,338:
println("Peg %X jumped over %X to land on %X\n".format(peg, over, land))
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,220 ⟶ 1,440:
Peg B jumped over C to land on D
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[Showstate]
Showstate[state_List, pos_] := Module[{p, e},
p = {#, FirstPosition[pos, #, Missing[], {2}]} & /@ state;
e = Complement[Flatten[pos], state];
e = {"_", FirstPosition[pos, #, Missing[], {2}]} & /@ e;
p = Join[p, e];
p = DeleteMissing[p, 1, \[Infinity]];
p[[All, 2]] //= Map[Reverse];
p[[All, 2, 2]] *= -1;
p[[All, 2, 1]] += p[[All, 2, 2]] 0.5;
Graphics[Text @@@ p, ImageSize -> 150]
]
pos = TakeList[Range[15], Range[5]];
moves1 = Catenate[If[Length[#] >= 3, Partition[#, 3, 1], {}] & /@ pos];
moves2 = Catenate[If[Length[#] >= 3, Partition[#, 3, 1], {}] & /@ Flatten[pos, {{2}, {1}}]];
moves3 = Catenate[If[Length[#] >= 3, Partition[#, 3, 1], {}] & /@ Flatten[Reverse /@ pos, {{2}, {1}}]];
moves = Join[moves1, moves2, moves3];
moves = Join[moves, Reverse /@ moves];
moves = <|Sort[{#1, #2} -> #3 & @@@ moves]|>;
ClearAll[SolvePuzzle]
SolvePuzzle[{state_List, history_List}, goal_] := Module[{k, newstate},
If[continue,
k = Keys[moves];
k = Select[k, ContainsAll[state, #] &];
k = Select[k, FreeQ[state, moves[#]] &];
k = {#, moves[#]} & /@ k;
Do[
newstate = state;
newstate = DeleteCases[newstate, Alternatives @@ move[[1]]];
AppendTo[newstate, move[[2]]];
If[newstate =!= goal,
SolvePuzzle[{newstate, Append[history, state]}, goal]
,
Print[FlipView[Showstate[#, pos] & /@ Append[Append[history, state], goal]]];
continue = False;
]
,
{move, k}
]
]
]
x = 1;
y = 13;
state = DeleteCases[Range[15], x];
continue = True;
SolvePuzzle[{state, {}}, {y}]</syntaxhighlight>
{{out}}
Outputs a graphical overview, by clicking one can go through the different states.
 
=={{header|Nim}}==
{{trans|Go}}
<syntaxhighlight lang="nim">import sequtils, strutils
 
type
Solution = tuple[peg, over, land: int]
Board = array[16, bool]
 
 
const
EmptyStart = 1
JumpMoves = [@[],
@[(2, 4), (3, 6)],
@[(4, 7), (5, 9)],
@[(5, 8), (6, 10)],
@[(2, 1), (5, 6), (7, 11), (8, 13)],
@[(8, 12), (9, 14)],
@[(3, 1), (5, 4), (9, 13), (10, 15)],
@[(4, 2), (8, 9)],
@[(5, 3), (9, 10)],
@[(5, 2), (8, 7)],
@[(9, 8)],
@[(12, 13)],
@[(8, 5), (13, 14)],
@[(8, 4), (9, 6), (12, 11), (14, 15)],
@[(9, 5), (13, 12)],
@[(10, 6), (14, 13)]]
 
 
func initBoard(): Board =
for i in 1..15: result[i] = true
result[EmptyStart] = false
 
 
proc draw(board: Board) =
var pegs: array[16, char]
for peg in pegs.mitems: peg = '-'
for i in 1..15:
if board[i]:
pegs[i] = i.toHex(1)[0]
echo " $#".format(pegs[1])
echo " $# $#".format(pegs[2], pegs[3])
echo " $# $# $#".format(pegs[4], pegs[5], pegs[6])
echo " $# $# $# $#".format(pegs[7], pegs[8], pegs[9], pegs[10])
echo " $# $# $# $# $#".format(pegs[11], pegs[12], pegs[13], pegs[14], pegs[15])
 
 
func solved(board: Board): bool = board.count(true) == 1
 
 
proc solve(board: var Board; solutions: var seq[Solution]) =
if board.solved: return
for peg in 1..15:
if board[peg]:
for (over, land) in JumpMoves[peg]:
if board[over] and not board[land]:
let saveBoard = board
board[peg] = false
board[over] = false
board[land] = true
solutions.add (peg, over, land)
board.solve(solutions)
if board.solved: return # otherwise back-track.
board = saveBoard
discard solutions.pop()
 
var board = initBoard()
var solutions: seq[Solution]
board.solve(solutions)
board = initBoard()
board.draw()
echo "Starting with peg $# removed\n".format(EmptyStart.toHex(1))
for (peg, over, land) in solutions:
board[peg] = false
board[over] = false
board[land] = true
board.draw()
echo "Peg $1 jumped over $2 to land on $3\n".format(peg.toHex(1), over.toHex(1), land.toHex(1))</syntaxhighlight>
 
{{out}}
<pre> -
2 3
4 5 6
7 8 9 A
B C D E F
Starting with peg 1 removed
 
1
- 3
- 5 6
7 8 9 A
B C D E F
Peg 4 jumped over 2 to land on 1
 
1
- 3
4 - -
7 8 9 A
B C D E F
Peg 6 jumped over 5 to land on 4
 
-
- -
4 - 6
7 8 9 A
B C D E F
Peg 1 jumped over 3 to land on 6
 
-
2 -
- - 6
- 8 9 A
B C D E F
Peg 7 jumped over 4 to land on 2
 
-
2 -
- 5 6
- - 9 A
B - D E F
Peg C jumped over 8 to land on 5
 
-
2 -
- 5 6
- - 9 A
B C - - F
Peg E jumped over D to land on C
 
-
2 -
- 5 -
- - - A
B C D - F
Peg 6 jumped over 9 to land on D
 
-
- -
- - -
- - 9 A
B C D - F
Peg 2 jumped over 5 to land on 9
 
-
- -
- - -
- - 9 A
B - - E F
Peg C jumped over D to land on E
 
-
- -
- - 6
- - 9 -
B - - E -
Peg F jumped over A to land on 6
 
-
- -
- - -
- - - -
B - D E -
Peg 6 jumped over 9 to land on D
 
-
- -
- - -
- - - -
B C - - -
Peg E jumped over D to land on C
 
-
- -
- - -
- - - -
- - D - -
Peg B jumped over C to land on D</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">@start = qw<
0
1 1
Line 1,274 ⟶ 1,722:
 
print $result ? $result : "No solution found";
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:60ex;overflow:scroll;">Starting with
Line 1,391 ⟶ 1,839:
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
Twee brute-force string-based solution. Backtracks a mere 366 times, whereas starting with the 5th peg missing backtracks 19388 times (all in 0s, obvs).
 
<lang Phix>--
<!--<syntaxhighlight lang="phix">-->
-- demo\rosetta\IQpuzzle.exw
<span style="color: #000080;font-style:italic;">-- demo\rosetta\IQpuzzle.exw</span>
--
<span style="color: #008080;">constant</span> <span style="color: #000000;">moves</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}</span>
constant moves = {-11,-9,2,11,9,-2}
<span style="color: #008080;">function</span> <span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">)</span>
function solve(string board, integer left)
<span style="color: #008080;">if</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008000;">""</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if left=1 then return "" end if
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for i=1 to length(board) do
<span style="color: #008080;">if</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'1'</span> <span style="color: #008080;">then</span>
if board[i]='1' then
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">moves</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for j=1 to length(moves) do
<span style="color: #004080;">integer</span> <span style="color: #000000;">mj</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">moves</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">over</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">mj</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tgt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">mj</span>
integer mj = moves[j], over = i+mj, tgt = i+2*mj
<span style="color: #008080;">if</span> <span style="color: #000000;">tgt</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">tgt</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board</span><span style="color: #0000FF;">)</span>
if tgt>=1 and tgt<=length(board)
<span style="color: #008080;">and</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">tgt</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">over</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'1'</span> <span style="color: #008080;">then</span>
and board[tgt]='0' and board[over]='1' then
<span style="color: #0000FF;">{</span><span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">over</span><span style="color: #0000FF;">],</span><span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">tgt</span><span style="color: #0000FF;">]}</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"001"</span>
{board[i],board[over],board[tgt]} = "001"
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board</span><span style="color: #0000FF;">,</span><span style="color: #000000;">left</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
string res = solve(board,left-1)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">4</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">&</span><span style="color: #000000;">res</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if length(res)!=4 then return board&res end if
<span style="color: #0000FF;">{</span><span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">over</span><span style="color: #0000FF;">],</span><span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">tgt</span><span style="color: #0000FF;">]}</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"110"</span>
{board[i],board[over],board[tgt]} = "110"
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">return</span> <span style="color: #008000;">"oops"</span>
return "oops"
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end function
<span style="color: #004080;">sequence</span> <span style="color: #000000;">start</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
sequence start = """
----0----
---1-1---
--1-1-1--
-1-1-1-1-
1-1-1-1-1
"""</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">start</span><span style="color: #0000FF;">&</span><span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">start</span><span style="color: #0000FF;">,</span><span style="color: #000000;">14</span><span style="color: #0000FF;">),</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">),</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"-"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span>
puts(1,substitute(join_by(split(start&solve(start,14),'\n'),5,7),"-"," "))</lang>
<!--</syntaxhighlight>-->
 
{{out}}
<pre>
Line 1,437 ⟶ 1,888:
1 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0 1 0 1 0 1 1 0 1 1 0 0 0 0 0 1 0 0
</pre>
Adapted to the English game (also in demo\rosetta\IQpuzzle.exw):
 
<lang Phix>constant moves = {-2,15,2,-15}
<!--<syntaxhighlight lang="phix">-->
function solve(string board, integer left)
<span style="color: #008080;">constant</span> <span style="color: #000000;">moves</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">15</span><span style="color: #0000FF;">}</span>
if left=1 then
<span style="color: #008080;">function</span> <span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">)</span>
-- return "" -- (leaves it on the edge)
<span style="color: #008080;">if</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
if board[3*15+8]='.' then return "" end if
<span style="color: #000080;font-style:italic;">-- return "" -- (leaves it on the edge)</span>
return "oops"
<span style="color: #008080;">if</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">15</span><span style="color: #0000FF;">+</span><span style="color: #000000;">8</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'.'</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008000;">""</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">return</span> <span style="color: #008000;">"oops"</span>
for i=1 to length(board) do
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if board[i]='.' then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for j=1 to length(moves) do
<span style="color: #008080;">if</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'.'</span> <span style="color: #008080;">then</span>
integer mj = moves[j], over = i+mj, tgt = i+2*mj
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">moves</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if tgt>=1 and tgt<=length(board)
<span style="color: #004080;">integer</span> <span style="color: #000000;">mj</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">moves</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">over</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">mj</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tgt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">mj</span>
and board[tgt]='o' and board[over]='.' then
<span style="color: #008080;">if</span> <span style="color: #000000;">tgt</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">tgt</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board</span><span style="color: #0000FF;">)</span>
{board[i],board[over],board[tgt]} = "oo."
<span style="color: #008080;">and</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">tgt</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'o'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">over</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'.'</span> <span style="color: #008080;">then</span>
string res = solve(board,left-1)
<span style="color: #0000FF;">{</span><span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">over</span><span style="color: #0000FF;">],</span><span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">tgt</span><span style="color: #0000FF;">]}</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"oo."</span>
if length(res)!=4 then return board&res end if
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board</span><span style="color: #0000FF;">,</span><span style="color: #000000;">left</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
{board[i],board[over],board[tgt]} = "..o"
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">4</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">&</span><span style="color: #000000;">res</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #0000FF;">{</span><span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">over</span><span style="color: #0000FF;">],</span><span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">tgt</span><span style="color: #0000FF;">]}</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"..o"</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return "oops"
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #008000;">"oops"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
sequence start = """
-----.-.-.----
<span style="color: #004080;">sequence</span> <span style="color: #000000;">start</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
-----.-.-.----
-.-.-.-.-.-.-.----
-.-.-.-o-.-.-.----
-.-.-.-.-.-.-.
-.-.-.-o-.-.-.----
-.-.--.-.-.-.----.
-----.-.-.----
"""
-----.-.-.----
puts(1,substitute(join_by(split(start&solve(start,32),'\n'),7,8),"-"," "))</lang>
"""</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">start</span><span style="color: #0000FF;">&</span><span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">start</span><span style="color: #0000FF;">,</span><span style="color: #000000;">32</span><span style="color: #0000FF;">),</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">),</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"-"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
 
{{out}}
<pre>
Line 1,506 ⟶ 1,961:
o . . . o o o o o o o o o o o o o o o o o o o o
</pre>
 
=={{header|Picat}}==
This version use the constraint solver (cp).
<syntaxhighlight lang="picat">import cp.
 
go =>
% Solve the puzzle
puzzle(1,N,NumMoves,X,Y),
println("Show the moves (Move from .. over .. to .. ):"),
foreach({From,Over,To} in X)
println([from=From,over=Over,to=To])
end,
nl,
println("Show the list at each move (0 is an empty hole):"),
foreach(Row in Y)
foreach(J in 1..15)
printf("%2d ", Row[J])
end,
nl
end,
nl,
 
println("And an verbose version:"),
foreach(Move in 1..NumMoves)
if Move > 1 then
printf("Move from %d over %d to %d\n",X[Move-1,1],X[Move-1,2],X[Move-1,3])
end,
nl,
print_board([Y[Move,J] : J in 1..N]),
nl
end,
nl,
% fail, % uncomment to see all solutions
nl.
 
puzzle(Empty,N,NumMoves,X,Y) =>
N = 15,
 
% Peg 1 can move over 2 and end at 4, etc
% (for table_in/2)
moves(Moves),
ValidMoves = [],
foreach(From in 1..N)
foreach([Over,To] in Moves[From])
ValidMoves := ValidMoves ++ [{From,Over,To}]
end
end,
 
NumMoves = N-1,
 
% which move to make
X = new_array(NumMoves-1,3),
X :: 1..N,
 
% The board at move Move
Y = new_array(NumMoves,N),
Y :: 0..N,
 
% Initialize for row
Y[1,Empty] #= 0,
foreach(J in 1..N)
if J != Empty then
Y[1,J] #= J
end
end,
 
% make the moves
foreach(Move in 2..NumMoves)
sum([Y[Move,J] #=0 : J in 1..N]) #= Move,
table_in({From,Over,To}, ValidMoves),
 
% Get this move and update the rows
element(To,Y[Move-1],0),
element(From,Y[Move-1],FromVal), FromVal #!= 0,
element(Over,Y[Move-1],OverVal), OverVal #!= 0,
 
element(From,Y[Move],0),
element(To,Y[Move],To),
element(Over,Y[Move],0),
 
foreach(J in 1..N)
(J #!= From #/\ J #!= Over #/\ J #!= To) #=>
Y[Move,J] #= Y[Move-1,J]
end,
X[Move-1,1] #= From,
X[Move-1,2] #= Over,
X[Move-1,3] #= To
end,
 
Vars = Y.vars() ++ X.vars(),
solve($[split],Vars).
 
%
% The valid moves:
% Peg 1 can move over 2 and end at 4, etc.
%
moves(Moves) =>
Moves = [
[[2,4],[3,6]], % 1
[[4,7],[5,9]], % 2
[[5,8],[6,10]], % 3
[[2,1],[5,6],[7,11],[8,13]], % 4
[[8,12],[9,14]], % 5
[[3,1],[5,4],[9,13],[10,15]], % 6
[[4,2],[8,9]], % 7
[[5,3],[9,10]], % 8
[[5,2],[8,7]], % 9
[[6,3],[9,8]], % 10
[[7,4],[12,13]], % 11
[[8,5],[13,14]], % 12
[[8,4],[9,6],[12,11],[14,15]], % 13
[[9,5],[13,12]], % 14
[[10,6],[14,13]] % 15
].
 
%
% Print the board:
%
% 1
% 2 3
% 4 5 6
% 7 8 9 10
% 11 12 13 14 15
%
print_board(B) =>
printf(" %2d\n", B[1]),
printf(" %2d %2d\n", B[2],B[3]),
printf(" %2d %2d %2d\n", B[4],B[5],B[6]),
printf(" %2d %2d %2d %2d\n",B[7],B[8],B[9],B[10]),
printf(" %2d %2d %2d %2d %2d\n",B[11],B[12],B[13],B[14],B[15]),
nl.</syntaxhighlight>
 
{{out}}
<pre>Show the moves (Move from .. over .. to .. ):
[from = 4,over = 2,to = 1]
[from = 6,over = 5,to = 4]
[from = 1,over = 3,to = 6]
[from = 12,over = 8,to = 5]
[from = 14,over = 13,to = 12]
[from = 6,over = 9,to = 13]
[from = 12,over = 13,to = 14]
[from = 15,over = 10,to = 6]
[from = 7,over = 4,to = 2]
[from = 2,over = 5,to = 9]
[from = 6,over = 9,to = 13]
[from = 14,over = 13,to = 12]
[from = 11,over = 12,to = 13]
 
Show the list at each move (0 is an empty hole):
0 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 0 3 0 5 6 7 8 9 10 11 12 13 14 15
1 0 3 4 0 0 7 8 9 10 11 12 13 14 15
0 0 0 4 0 6 7 8 9 10 11 12 13 14 15
0 0 0 4 5 6 7 0 9 10 11 0 13 14 15
0 0 0 4 5 6 7 0 9 10 11 12 0 0 15
0 0 0 4 5 0 7 0 0 10 11 12 13 0 15
0 0 0 4 5 0 7 0 0 10 11 0 0 14 15
0 0 0 4 5 6 7 0 0 0 11 0 0 14 0
0 2 0 0 5 6 0 0 0 0 11 0 0 14 0
0 0 0 0 0 6 0 0 9 0 11 0 0 14 0
0 0 0 0 0 0 0 0 0 0 11 0 13 14 0
0 0 0 0 0 0 0 0 0 0 11 12 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 13 0 0
 
And an verbose version:
 
0
2 3
4 5 6
7 8 9 10
11 12 13 14 15
 
 
Move from 4 over 2 to 1
 
1
0 3
0 5 6
7 8 9 10
11 12 13 14 15
 
 
Move from 6 over 5 to 4
 
1
0 3
4 0 0
7 8 9 10
11 12 13 14 15
 
 
Move from 1 over 3 to 6
 
0
0 0
4 0 6
7 8 9 10
11 12 13 14 15
 
 
Move from 12 over 8 to 5
 
0
0 0
4 5 6
7 0 9 10
11 0 13 14 15
 
 
Move from 14 over 13 to 12
 
0
0 0
4 5 6
7 0 9 10
11 12 0 0 15
 
 
Move from 6 over 9 to 13
 
0
0 0
4 5 0
7 0 0 10
11 12 13 0 15
 
 
Move from 12 over 13 to 14
 
0
0 0
4 5 0
7 0 0 10
11 0 0 14 15
 
 
Move from 15 over 10 to 6
 
0
0 0
4 5 6
7 0 0 0
11 0 0 14 0
 
 
Move from 7 over 4 to 2
 
0
2 0
0 5 6
0 0 0 0
11 0 0 14 0
 
 
Move from 2 over 5 to 9
 
0
0 0
0 0 6
0 0 9 0
11 0 0 14 0
 
 
Move from 6 over 9 to 13
 
0
0 0
0 0 0
0 0 0 0
11 0 13 14 0
 
 
Move from 14 over 13 to 12
 
0
0 0
0 0 0
0 0 0 0
11 12 0 0 0
 
 
Move from 11 over 12 to 13
 
0
0 0
0 0 0
0 0 0 0
0 0 13 0 0</pre>
 
=={{header|Prolog}}==
Works with SWI-Prolog and module(lambda).
 
<langsyntaxhighlight Prologlang="prolog">:- use_module(library(lambda)).
 
iq_puzzle :-
Line 1,606 ⟶ 2,350:
select(End, Free, F1),
display(Tail, [Start, Middle | F1]).
</syntaxhighlight>
</lang>
Output :
<pre> ?- iq_puzzle.
Line 1,728 ⟶ 2,472:
=={{header|Python}}==
 
<syntaxhighlight lang="python">#
<lang Python>#
# Draw board triangle in ascii
#
Line 1,823 ⟶ 2,567:
AddPeg(board,land) # board order changes!
DrawBoard(board)
print "Peg %X jumped over %X to land on %X\n" % (peg,over,land)</langsyntaxhighlight>
 
{{out}}
Line 1,928 ⟶ 2,672:
Oh and there are some useful triangle numbers functions thrown in for free!
 
<langsyntaxhighlight lang="racket">#lang racket
(define << arithmetic-shift)
(define bwbs? bitwise-bit-set?)
Line 2,006 ⟶ 2,750:
 
;; Solve #1 missing -> #13 left alone
(for-each display-board (find-path (flip-peg 1 full-board) (flip-peg 13 empty-board)))</langsyntaxhighlight>
 
{{out}}
Line 2,093 ⟶ 2,837:
{{trans|Sidef}}
 
<syntaxhighlight lang="raku" line>
<lang perl6>
constant @start = <
0
Line 2,143 ⟶ 2,887:
last if $result
};
say $result ?? $result !! "No solution found";</langsyntaxhighlight>
{{out}}
<pre style="height:60ex;overflow:scroll;">Starting with
Line 2,247 ⟶ 2,991:
=={{header|Ruby}}==
 
<langsyntaxhighlight lang="ruby"># Solitaire Like Puzzle Solver - Nigel Galloway: October 18th., 2014
G = [[0,1,3],[0,2,5],[1,3,6],[1,4,8],[2,4,7],[2,5,9],[3,4,5],[3,6,10],[3,7,12],[4,7,11],[4,8,13],[5,8,12],[5,9,14],[6,7,8],[7,8,9],[10,11,12],[11,12,13],[12,13,14],
[3,1,0],[5,2,0],[6,3,1],[8,4,1],[7,4,2],[9,5,2],[5,4,3],[10,6,3],[12,7,3],[11,7,4],[13,8,4],[12,8,5],[14,9,5],[8,7,6],[9,8,7],[12,11,10],[13,12,11],[14,13,12]]
Line 2,261 ⟶ 3,005:
l=false; G.each{|g| l=solve(N,N.inject(:+),g); break if l}
puts l ? l : "No solution found"
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:64ex;overflow:scroll">
Line 2,366 ⟶ 3,110:
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">const N = [0,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
 
const G = [
Line 2,405 ⟶ 3,149:
var r = ''
G.each {|g| (r = solve(N, 1, g)) && break }
say (r ? r : "No solution found")</langsyntaxhighlight>
 
{{out}}
Line 2,512 ⟶ 3,256:
'''Notes:'''
This program uses a brute-force method with a string of 25 characters to internally represent the 15 spots on the peg board. One can set the starting removed peg and intended last remaining peg by editing the header variable declarations named '''''Starting''''' and '''''Target'''''. If one doesn't care which spot the last peg lands on, the '''''Target''''' variable can be set to 0. The constant '''''n''''' can be changed for different sized peg boards, for example with '''''n = 6''''' the peg board would have 21 positions.
<langsyntaxhighlight lang="vbnet">
Imports System, Microsoft.VisualBasic.DateAndTime
 
Line 2,638 ⟶ 3,382:
If Diagnostics.Debugger.IsAttached Then Console.ReadLine()
End Sub
End Module</langsyntaxhighlight>
{{out}}
'''A full solution:'''
Line 2,744 ⟶ 3,488:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Conv, Fmt
 
var board = List.filled(16, true)
Line 2,823 ⟶ 3,567:
drawBoard.call()
Fmt.print("Peg $X jumped over $X to land on $X\n", peg, over, land)
}</langsyntaxhighlight>
 
{{out}}
Line 2,829 ⟶ 3,573:
Same as Kotlin entry.
</pre>
 
=={{header|Yabasic}}==
{{trans|Phix}}
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Solve_triangle_solitare_puzzle
// by Galileo, 04/2022
 
dim moves$(1)
 
nmov = token("-11,-9,2,11,9,-2", moves$(), ",")
 
sub solve$(board$, left)
local i, j, mj, over, tgt, res$
if left = 1 return ""
for i = 1 to len(board$)
if mid$(board$, i, 1) = "1" then
for j = 1 to nmov
mj = val(moves$(j)) : over = i + mj : tgt = i + 2 * mj
if tgt >= 1 and tgt <= len(board$) and mid$(board$, tgt, 1) = "0" and mid$(board$, over, 1) = "1" then
mid$(board$, i, 1) = "0" : mid$(board$, over, 1) = "0" : mid$(board$, tgt, 1) = "1"
res$ = solve$(board$, left - 1)
if len(res$) != 4 return board$+res$
mid$(board$, i, 1) = "1" : mid$(board$, over, 1) = "1" : mid$(board$, tgt, 1) = "0"
end if
next
end if
next
return "oops"
end sub
start$ = "\n\n 0 \n 1 1 \n 1 1 1 \n 1 1 1 1 \n1 1 1 1 1"
print start$, solve$(start$, 14)</syntaxhighlight>
{{out}}
<pre>
 
0
1 1
1 1 1
1 1 1 1
1 1 1 1 1
 
1
0 1
0 1 1
1 1 1 1
1 1 1 1 1
 
1
0 1
1 0 0
1 1 1 1
1 1 1 1 1
 
0
0 0
1 0 1
1 1 1 1
1 1 1 1 1
 
0
1 0
0 0 1
0 1 1 1
1 1 1 1 1
 
0
1 1
0 0 0
0 1 1 0
1 1 1 1 1
 
0
1 1
0 1 0
0 0 1 0
1 0 1 1 1
 
0
1 1
0 1 1
0 0 0 0
1 0 0 1 1
 
0
0 1
0 0 1
0 0 1 0
1 0 0 1 1
 
0
0 0
0 0 0
0 0 1 1
1 0 0 1 1
 
0
0 0
0 0 1
0 0 1 0
1 0 0 1 0
 
0
0 0
0 0 0
0 0 0 0
1 0 1 1 0
 
0
0 0
0 0 0
0 0 0 0
1 1 0 0 0
 
0
0 0
0 0 0
0 0 0 0
0 0 1 0 0
---Program done, press RETURN---</pre>
 
=={{header|zkl}}==
{{trans|D}}
{{Trans|Ruby}}
<langsyntaxhighlight lang="zkl">var N=T(0,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
var G=T( T(0,1, 3), T(0,2, 5), T(1,3, 6), T( 1, 4, 8), T( 2, 4, 7), T( 2, 5, 9),
T(3,4, 5), T(3,6,10), T(3,7,12), T( 4, 7,11), T( 4, 8,13), T( 5, 8,12),
Line 2,867 ⟶ 3,730:
reg l;
foreach g in (G){ if(l=solve(N,1,g)) break; }
println(l and l or "No solution found.");</langsyntaxhighlight>
{{out}}
<pre style="height:32ex;overflow:scroll">
9,476

edits