Solve triangle solitaire puzzle: Difference between revisions

Content added Content deleted
(→‎{{header|Ruby}}: change output format, each -> for (scope))
(+ D entry)
Line 15: Line 15:
solve with one
solve with one
peg in position Y.
peg in position Y.

=={{header|D}}==
{{trans|Ruby}}
<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];
immutable 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]];

string b2s(in int[] n) pure @safe {
static immutable fmt = iota(6)
.map!(i => " ".replicate(5 - i) ~ "%d ".replicate(i))
.join("\n");
return fmt.format(n[0], n[1], n[2], n[3], n[4], n[5], n[6],
n[7], n[8], n[9], n[10], n[11], n[12], n[13], n[14]);
}

string solve(in int[] n, in int i, in int[] g) pure @safe {
if (i == N.length - 1)
return "\nSolved";
if (n[g[1]] == 0)
return null;
string s;
if (n[g[0]] == 0) {
if (n[g[2]] == 0)
return null;
s = "\n%d to %d\n".format(g[2], g[0]);
} else {
if (n[g[2]] == 1)
return null;
s = "\n%d to %d\n".format(g[0], g[2]);
}

auto a = n.dup;
foreach (const gi; g)
a[gi] = 1 - a[gi];
string l;
foreach (const gi; G) {
l = solve(a, i + 1, gi);
if (!l.empty)
break;
}
return l.empty ? l : (s ~ b2s(a) ~ l);
}

void main() @safe {
b2s(N).write;
string l;
foreach (const g; G) {
l = solve(N, 1, g);
if (!l.empty)
break;
}
writeln(l.empty ? "No solution found." : l);
}</lang>
{{out}}
<pre>
0
1 1
1 1 1
1 1 1 1
1 1 1 1 1
3 to 0
1
0 1
0 1 1
1 1 1 1
1 1 1 1 1
8 to 1
1
1 1
0 0 1
1 1 0 1
1 1 1 1 1
10 to 3
1
1 1
1 0 1
0 1 0 1
0 1 1 1 1
1 to 6
1
0 1
0 0 1
1 1 0 1
0 1 1 1 1
11 to 4
1
0 1
0 1 1
1 0 0 1
0 0 1 1 1
2 to 7
1
0 0
0 0 1
1 1 0 1
0 0 1 1 1
9 to 2
1
0 1
0 0 0
1 1 0 0
0 0 1 1 1
0 to 5
0
0 0
0 0 1
1 1 0 0
0 0 1 1 1
6 to 8
0
0 0
0 0 1
0 0 1 0
0 0 1 1 1
13 to 11
0
0 0
0 0 1
0 0 1 0
0 1 0 0 1
5 to 12
0
0 0
0 0 0
0 0 0 0
0 1 1 0 1
11 to 13
0
0 0
0 0 0
0 0 0 0
0 0 0 1 1
14 to 12
0
0 0
0 0 0
0 0 0 0
0 0 1 0 0
Solved</pre>


=={{header|Python}}==
=={{header|Python}}==
Line 208: Line 363:
Peg B jumped over C to land on D
Peg B jumped over C to land on D
</pre>
</pre>

=={{header|Ruby}}==
=={{header|Ruby}}==