Solve triangle solitaire puzzle: Difference between revisions
m
→{{header|Phix}}: use pygments, simplified by using reinstate, which also fixes a couple of p2js violations
m (→{{header|ALGOL 68}}: Notes) |
m (→{{header|Phix}}: use pygments, simplified by using reinstate, which also fixes a couple of p2js violations) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 806:
</pre>
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Solve triangle solitaire puzzle. Nigel Galloway: May 28th., 2024
type hole= O|X
type cand={board:hole[];p2go:int;hist:list<hole[]*int*int*int>}
let 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]
let move n (from,over,To)=let g=Array.copy n.board in g[from]<-O; g[over]<-O; g[To]<-X; {board=g;p2go=n.p2go-1;hist=(n.board,from,over,To)::n.hist}
let moves (p:hole[])=G|>List.fold(fun n g->match g with from,over,To when p[over]=X&&p[from]=X&&p[To]=O->(from,over,To)::n |To,over,from when p[over]=X&&p[from]=X&&p[To]=O->(from,over,To)::n |_->n)[]
let rec fs=function []->None |n::g when n.p2go=0->Some((n.board,-1,-1,-1)::n.hist) |n::g->fs(((moves n.board)|>List.map(fun g->move n g))@g)
let solve n=fs [{board=n; p2go=13; hist=[]}]
let fN(g:hole[])=printfn " %A\n %A %A\n %A %A %A\n %A %A %A %A\n%A %A %A %A %A" g[0] g[1] g[2] g[3] g[4] g[5] g[6] g[7] g[8] g[9] g[10] g[11] g[12] g[13] g[14]
match solve [|O;X;X;X;X;X;X;X;X;X;X;X;X;X;X|] with Some n->n|>List.rev|>List.iter(fun(g,from,over,To)->fN g; if from> -1 then printfn "\nmove from %A over %A to %A\n" from over To) |_->printfn "No solution found"
</syntaxhighlight>
{{out}}
<pre>
O
X X
X X X
X X X X
X X X X X
move from 5 over 2 to 0
X
X O
X X O
X X X X
X X X X X
move from 14 over 9 to 5
X
X O
X X X
X X X O
X X X X O
move from 7 over 8 to 9
X
X O
X X X
X O O X
X X X X O
move from 9 over 5 to 2
X
X X
X X O
X O O O
X X X X O
move from 1 over 4 to 8
X
O X
X O O
X O X O
X X X X O
move from 13 over 8 to 4
X
O X
X X O
X O O O
X X X O O
move from 11 over 12 to 13
X
O X
X X O
X O O O
X O O X O
move from 2 over 4 to 7
X
O O
X O O
X X O O
X O O X O
move from 6 over 3 to 1
X
X O
O O O
O X O O
X O O X O
move from 0 over 1 to 3
O
O O
X O O
O X O O
X O O X O
move from 3 over 7 to 12
O
O O
O O O
O O O O
X O X X O
move from 13 over 12 to 11
O
O O
O O O
O O O O
X X O O O
move from 10 over 11 to 12
O
O O
O O O
O O O O
O O X O O
</pre>
=={{header|Go}}==
{{trans|Kotlin}}
Line 2,399 ⟶ 2,524:
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).
<!--(phixonline)-->
<syntaxhighlight lang="phix">
-- demo\rosetta\IQpuzzle.exw
with javascript_semantics
function solve(string board, integer left)
if left=1 then return "" end if
for i=1 to length(board) do
if board[i]='1' then
for mj in {-11,-9,2,11,9,-2} do
integer over = i+mj, tgt = i+2*mj
if tgt>=1 and tgt<=length(board)
and board[tgt]='0' and board[over]='1' then
board = reinstate(board,{i,over,tgt},"001")
string res = solve(board,left-1)
if length(res)!=4 then return board&res end if
board = reinstate(board,{i,over,tgt},"110")
end for
end if
end for
return "oops"
end function
sequence start = """
1-1-1-1-1
"""
puts(1,substitute(join_by(split(start&solve(start,14),'\n'),5,7),"-"," "))
</syntaxhighlight>
{{out}}
Line 2,447 ⟶ 2,573:
Adapted to the English game (also in demo\rosetta\IQpuzzle.exw):
function solveE(string board, integer left)
if left=1 then
-- return "" -- (leaves it on the edge)
if board[3*15+8]='.' then return "" end if
return "oops"
end if
for i=1 to length(board) do
if board[i]='.' then
for mj in {-2,15,2,-15} do
integer over = i+mj, tgt = i+2*mj
if tgt>=1 and tgt<=length(board)
and board[tgt]='o' and board[over]='.' then
board = reinstate(board,{i,over,tgt},"oo.")
string res = solveE(board,left-1)
if length(res)!=4 then return board&res end if
board = reinstate(board,{i,over,tgt},"..o")
end if
end for
end if
end for
return "oops"
end function
string estart = """
-----.-.-.----
"""
puts(1,substitute(join_by(split(estart&solveE(estart,32),'\n'),7,8),"-"," "))
</syntaxhighlight>
{{out}}
<pre style="font-size: 12px">
. . . . . . . . . o . . . o o . o o . o o . o .
. . . . o . . o . o o . o o . o o . o o . o o o
|