Twelve statements: Difference between revisions

Content added Content deleted
(Simpler D entry)
(Simpler D entry)
Line 180: Line 180:
=={{header|D}}==
=={{header|D}}==
{{trans|Python}}
{{trans|Python}}
<lang d>import std.stdio, std.typecons, std.algorithm, std.range,
<lang d>import std.stdio, std.typecons, std.algorithm, std.range,std.functional;
std.array, std.functional;


immutable texts = [
immutable texts = [
Line 212: Line 211:
s => sumi(s[6 .. 9]) == 1,
s => sumi(s[6 .. 9]) == 1,
s => sumi(s[0 .. 11]) == 4];
s => sumi(s[0 .. 11]) == 4];

void show(in bool[] st, in bool[] matches, in bool isPartial) {
if (isPartial) {
immutable pos = matches.countUntil(false);
writefln(`Missed by statement %d: "%s"`, pos + 1, texts[pos]);
} else
writeln("Solution:");
write(" ");
foreach (i, t; st)
writef("%d:%s ", i + 1, t ? "T" : "F");
writeln();
}


void main() {
void main() {
Line 245: Line 232:


foreach (sols, isPartial; zip([full, partial], [false, true]))
foreach (sols, isPartial; zip([full, partial], [false, true]))
foreach (stm; sols)
foreach (stm; sols) {
show(stm.tupleof, isPartial);
if (isPartial) {
immutable pos = stm[1].countUntil(false);
writefln(`Missed by statement %d: "%s"`,
pos + 1, texts[pos]);
} else
writeln("Solution:");
write(" ");
foreach (i, t; stm[0])
writef("%d:%s ", i + 1, t ? "T" : "F");
writeln();
}
}</lang>
}</lang>
{{out}}
{{out}}