Solve triangle solitaire puzzle: Difference between revisions

→‎{{header|Ruby}}: change of the termination condition
(Updated task)
(→‎{{header|Ruby}}: change of the termination condition)
Line 973:
 
<lang ruby># Solitaire Like Puzzle Solver - Nigel Galloway: October 18th., 2014
PEGSpegs = (N = [0,1,1,1,1,1,1,1,1,1,1,1,1,1,1]).inject(:+)
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]]
FORMAT = (1..5).map{|i| " "*(5-i)+"%d "*i+"\n"}.join+"\n"
def solve n,i,g
return "Solved" if i == PEGS1
return false unless n[g[1]]==1
if n[g[0]] == 0
return false unless n[g[2]]==1
s = "#{g[2]} to #{g[0]}\n"
else
return false unless n[g[2]]==0
Line 987:
end
a = n.clone; g.each{|n| a[n] = 1 - a[n]}
l=false; G.each{|g| l=solve(a,i+-1,g); break if l}
return l ? s + FORMAT % a + l : l
end
puts FORMAT % N
l=false; G.each{|g| l=solve(N,1pegs,g); break if l}
puts l ? l : "No solution found"</lang>
</lang>
{{out}}
<pre style="height:64ex;overflow:scroll">
Anonymous user