Twelve statements: Difference between revisions
Content deleted Content added
m →{{header|Haskell}}: phrasing: not so much "incorrect" as flipping them alone won't make the whole set consistent |
+ D entry |
||
Line 21: | Line 21: | ||
Extra credit: also print out a table of near misses, that is, solutions that are contradicted by only a single statement. |
Extra credit: also print out a table of near misses, that is, solutions that are contradicted by only a single statement. |
||
=={{header|D}}== |
|||
{{trans|Python}} |
|||
<lang d>import std.stdio, std.typecons, std.algorithm, std.range, |
|||
std.array, std.functional; |
|||
enum nStats = 12; |
|||
immutable texts = [ |
|||
"this is a numbered list of twelve statements", |
|||
"exactly 3 of the last 6 statements are true", |
|||
"exactly 2 of the even-numbered statements are true", |
|||
"if statement 5 is true, then statements 6 and 7 are both true", |
|||
"the 3 preceding statements are all false", |
|||
"exactly 4 of the odd-numbered statements are true", |
|||
"either statement 2 or 3 is true, but not both", |
|||
"if statement 7 is true, then 5 and 6 are both true", |
|||
"exactly 3 of the first 6 statements are true", |
|||
"the next two statements are both true", |
|||
"exactly 1 of statements 7, 8 and 9 are true", |
|||
"exactly 4 of the preceding statements are true"]; |
|||
alias curry!(reduce!q{a + b}, 0) sumi; |
|||
immutable bool function(in bool[])[] funcs = [ |
|||
s => s.length == 12, |
|||
s => sumi(s[$-6 .. $]) == 3, |
|||
s => sumi(s[1 .. $].stride(2)) == 2, |
|||
s => s[4] ? (s[5] && s[6]) : true, |
|||
s => sumi(s[1 .. 4]) == 0, |
|||
s => sumi(s[0 .. $].stride(2)) == 4, |
|||
s => sumi(s[1 .. 3]) == 1, |
|||
s => s[6] ? (s[4] && s[5]) : true, |
|||
s => sumi(s[0 .. 6]) == 3, |
|||
s => s[10] && s[11], |
|||
s => sumi(s[6 .. 9]) == 1, |
|||
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() { |
|||
assert(texts.length == nStats); |
|||
assert(funcs.length == nStats); |
|||
Tuple!(const bool[], const bool[])[] full, partial; |
|||
foreach (n; 0 .. 2 ^^ nStats) { |
|||
const st = iota(nStats).map!(i => !!(n & (2 ^^ i)))().array(); |
|||
auto truths = funcs.map!(f => f(st))(); |
|||
const matches = zip(st, truths) |
|||
.map!(s_t => s_t[0] == s_t[1])() |
|||
.array(); |
|||
immutable mCount = matches.sumi(); |
|||
if (mCount == nStats) |
|||
full ~= tuple(st, matches); |
|||
else if (mCount == nStats - 1) |
|||
partial ~= tuple(st, matches); |
|||
} |
|||
foreach (stm; full) |
|||
show(stm.tupleof, false); |
|||
foreach (stm; partial) |
|||
show(stm.tupleof, true); |
|||
}</lang> |
|||
{{out}} |
|||
<pre>Solution: |
|||
1:T 2:F 3:T 4:T 5:F 6:T 7:T 8:F 9:F 10:F 11:T 12:F |
|||
Missed by statement 8: "if statement 7 is true, then 5 && 6 are both true" |
|||
1:T 2:F 3:F 4:T 5:F 6:F 7:F 8:F 9:F 10:F 11:F 12:F |
|||
Missed by statement 8: "if statement 7 is true, then 5 && 6 are both true" |
|||
1:T 2:F 3:F 4:F 5:T 6:F 7:F 8:F 9:F 10:F 11:F 12:F |
|||
Missed by statement 11: "exactly 1 of statements 7, 8 && 9 are true" |
|||
1:T 2:F 3:F 4:F 5:T 6:F 7:F 8:T 9:F 10:F 11:F 12:F |
|||
Missed by statement 9: "exactly 3 of the first 6 statements are true" |
|||
1:T 2:F 3:T 4:T 5:F 6:T 7:T 8:F 9:T 10:F 11:F 12:F |
|||
Missed by statement 7: "either statement 2 or 3 is true, but not both" |
|||
1:T 2:F 3:T 4:T 5:F 6:F 7:F 8:T 9:T 10:F 11:F 12:F |
|||
Missed by statement 6: "exactly 4 of the odd-numbered statements are true" |
|||
1:T 2:F 3:F 4:T 5:F 6:T 7:F 8:T 9:T 10:F 11:F 12:F |
|||
Missed by statement 8: "if statement 7 is true, then 5 && 6 are both true" |
|||
1:T 2:T 3:F 4:T 5:F 6:F 7:T 8:T 9:T 10:F 11:F 12:F |
|||
Missed by statement 10: "the next two statements are both true" |
|||
1:T 2:T 3:F 4:T 5:F 6:F 7:T 8:F 9:T 10:T 11:F 12:F |
|||
Missed by statement 1: "this is a numbered list of twelve statements" |
|||
1:F 2:F 3:F 4:F 5:T 6:F 7:F 8:T 9:F 10:F 11:T 12:F |
|||
Missed by statement 12: "exactly 4 of the preceding statements are true" |
|||
1:T 2:F 3:F 4:F 5:T 6:F 7:F 8:T 9:F 10:F 11:T 12:F |
|||
Missed by statement 8: "if statement 7 is true, then 5 && 6 are both true" |
|||
1:T 2:F 3:F 4:F 5:T 6:T 7:F 8:F 9:T 10:F 11:T 12:F |
|||
Missed by statement 12: "exactly 4 of the preceding statements are true" |
|||
1:T 2:T 3:F 4:T 5:F 6:F 7:T 8:F 9:T 10:F 11:F 12:T |
|||
Missed by statement 1: "this is a numbered list of twelve statements" |
|||
1:F 2:F 3:F 4:T 5:F 6:F 7:F 8:T 9:F 10:T 11:T 12:T |
|||
Missed by statement 12: "exactly 4 of the preceding statements are true" |
|||
1:T 2:F 3:F 4:T 5:F 6:F 7:F 8:T 9:F 10:T 11:T 12:T |
|||
Missed by statement 1: "this is a numbered list of twelve statements" |
|||
1:F 2:F 3:F 4:F 5:T 6:F 7:F 8:T 9:F 10:T 11:T 12:T |
|||
Missed by statement 12: "exactly 4 of the preceding statements are true" |
|||
1:T 2:F 3:F 4:F 5:T 6:F 7:F 8:T 9:F 10:T 11:T 12:T </pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
<lang go>package main |
<lang go>package main |