Solve triangle solitaire puzzle: Difference between revisions

m
m (→‎{{header|Picat}}: Added {{out}})
m (→‎{{header|Wren}}: Minor tidy)
(7 intermediate revisions by 3 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 34 ⟶ 36:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F DrawBoard(board)
V peg = [‘’] * 16
L(n) 1.<16
Line 112 ⟶ 114:
AddPeg(&board, land)
DrawBoard(board)
print("Peg #. jumped over #. to land on #.\n".format(hex(peg), hex(over), hex(land)))</langsyntaxhighlight>
 
{{out}}
Line 211 ⟶ 213:
=={{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 263 ⟶ 265:
}
writeln(l.empty ? "No solution found." : l);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 365 ⟶ 367:
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
<lang>brd$[] = strchars "
┏━━━━━━━━━┓
┃ · ┃
Line 373 ⟶ 376:
┃● ● ● ● ●┃
┗━━━━━━━━━┛"
funcproc solve . solution$ .
solution$ = ""
for pos range= 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] = "●"
call solve solution$
brd$[pos] = "●"
brd$[pos + dir] = "●"
brd$[pos + 2 * dir] = "·"
if solution$ <> ""
solution$ = strjoin brd$[] & solution$
break 3 return
.
.
.
.
.
if npegs = 1
.
solution$ = strjoin brd$[]
if npegs = 1
.
solution$ = strjoin brd$[]
.
.
call solve solution$
print solution$
</syntaxhighlight>
</lang>
 
=={{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 447 ⟶ 450:
end
 
IQ_Puzzle.task</langsyntaxhighlight>
 
{{out}}
Line 552 ⟶ 555:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 666 ⟶ 669:
fmt.Printf("Peg %X jumped over %X to land on %X\n\n", peg, over, land)
}
}</langsyntaxhighlight>
 
{{out}}
Line 674 ⟶ 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 856 ⟶ 859:
NB. Solution NB. return Solution however Solution is global.
)
</syntaxhighlight>
</lang>
Example linux session with program in file CrackerBarrel.ijs
<pre>
Line 895 ⟶ 898:
Print one possible solution.
 
<syntaxhighlight lang="java">
<lang Java>
import java.util.ArrayList;
import java.util.Arrays;
Line 1,083 ⟶ 1,086:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,121 ⟶ 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 1,157 ⟶ 1,160:
end
end
</langsyntaxhighlight>{{out}}
<pre>
Starting board:
Line 1,260 ⟶ 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,335 ⟶ 1,338:
println("Peg %X jumped over %X to land on %X\n".format(peg, over, land))
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,439 ⟶ 1,442:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Showstate]
Showstate[state_List, pos_] := Module[{p, e},
p = {#, FirstPosition[pos, #, Missing[], {2}]} & /@ state;
Line 1,484 ⟶ 1,487:
state = DeleteCases[Range[15], x];
continue = True;
SolvePuzzle[{state, {}}, {y}]</langsyntaxhighlight>
{{out}}
Outputs a graphical overview, by clicking one can go through the different states.
Line 1,490 ⟶ 1,493:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import sequtils, strutils
 
type
Line 1,565 ⟶ 1,568:
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))</langsyntaxhighlight>
 
{{out}}
Line 1,668 ⟶ 1,671:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">@start = qw<
0
1 1
Line 1,719 ⟶ 1,722:
 
print $result ? $result : "No solution found";
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:60ex;overflow:scroll;">Starting with
Line 1,839 ⟶ 1,842:
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).
 
<!--<langsyntaxhighlight Phixlang="phix">-->
<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>
Line 1,869 ⟶ 1,872:
"""</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>
<!--</langsyntaxhighlight>-->
 
{{out}}
Line 1,887 ⟶ 1,890:
Adapted to the English game (also in demo\rosetta\IQpuzzle.exw):
 
<!--<langsyntaxhighlight Phixlang="phix">-->
<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>
<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>
Line 1,922 ⟶ 1,925:
"""</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>
<!--</langsyntaxhighlight>-->
 
{{out}}
Line 1,961 ⟶ 1,964:
=={{header|Picat}}==
This version use the constraint solver (cp).
<langsyntaxhighlight Picatlang="picat">import cp.
 
go =>
Line 2,089 ⟶ 2,092:
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.</langsyntaxhighlight>
 
{{out}}
Line 2,251 ⟶ 2,254:
Works with SWI-Prolog and module(lambda).
 
<langsyntaxhighlight Prologlang="prolog">:- use_module(library(lambda)).
 
iq_puzzle :-
Line 2,347 ⟶ 2,350:
select(End, Free, F1),
display(Tail, [Start, Middle | F1]).
</syntaxhighlight>
</lang>
Output :
<pre> ?- iq_puzzle.
Line 2,469 ⟶ 2,472:
=={{header|Python}}==
 
<syntaxhighlight lang="python">#
<lang Python>#
# Draw board triangle in ascii
#
Line 2,564 ⟶ 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 2,669 ⟶ 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,747 ⟶ 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,834 ⟶ 2,837:
{{trans|Sidef}}
 
<syntaxhighlight lang="raku" line>
<lang perl6>
constant @start = <
0
Line 2,884 ⟶ 2,887:
last if $result
};
say $result ?? $result !! "No solution found";</langsyntaxhighlight>
{{out}}
<pre style="height:60ex;overflow:scroll;">Starting with
Line 2,988 ⟶ 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 3,002 ⟶ 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 3,107 ⟶ 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 3,146 ⟶ 3,149:
var r = ''
G.each {|g| (r = solve(N, 1, g)) && break }
say (r ? r : "No solution found")</langsyntaxhighlight>
 
{{out}}
Line 3,253 ⟶ 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 3,379 ⟶ 3,382:
If Diagnostics.Debugger.IsAttached Then Console.ReadLine()
End Sub
End Module</langsyntaxhighlight>
{{out}}
'''A full solution:'''
Line 3,485 ⟶ 3,488:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Conv, Fmt
 
var board = List.filled(16, true)
Line 3,564 ⟶ 3,567:
drawBoard.call()
Fmt.print("Peg $X jumped over $X to land on $X\n", peg, over, land)
}</langsyntaxhighlight>
 
{{out}}
Line 3,573 ⟶ 3,576:
=={{header|Yabasic}}==
{{trans|Phix}}
<langsyntaxhighlight Yabasiclang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Solve_triangle_solitare_puzzle
// by Galileo, 04/2022
 
Line 3,601 ⟶ 3,604:
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)</langsyntaxhighlight>
{{out}}
<pre>
Line 3,693 ⟶ 3,696:
{{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 3,727 ⟶ 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,483

edits