March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -1,4 +1,4 @@
import std.stdio, std.typecons, std.algorithm,std.range,std.functional;
import std.stdio, std.algorithm, std.range, std.functional;
immutable texts = [
"this is a numbered list of twelve statements",
@ -14,50 +14,35 @@ immutable texts = [
"exactly 1 of statements 7, 8 and 9 are true",
"exactly 4 of the preceding statements are true"];
alias sumi = curry!(reduce!q{a + b}, 0);
immutable bool function(in bool[])[] funcs = [
immutable pure bool function(in bool[])[12] predicates = [
s => s.length == 12,
s => sumi(s[$ - 6 .. $]) == 3,
s => sumi(s[1 .. $].stride(2)) == 2,
s => s[$ - 6 .. $].sum == 3,
s => s.dropOne.stride(2).sum == 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[1 .. 4].sum == 0,
s => s.stride(2).sum == 4,
s => s[1 .. 3].sum == 1,
s => s[6] ? (s[4] && s[5]) : true,
s => sumi(s[0 .. 6]) == 3,
s => s[0 .. 6].sum == 3,
s => s[10] && s[11],
s => sumi(s[6 .. 9]) == 1,
s => sumi(s[0 .. 11]) == 4];
s => s[6 .. 9].sum == 1,
s => s[0 .. 11].sum == 4];
void main() {
enum nStats = 12;
Tuple!(const(bool)[], const(bool)[])[] full, partial;
enum nStats = predicates.length;
foreach (immutable n; 0 .. 2 ^^ nStats) {
const st = nStats.iota.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 (sols, isPartial; zip([full, partial], [false, true]))
foreach (const stm; sols) {
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;
bool[nStats] st, matches;
nStats.iota.map!(i => !!(n & (2 ^^ i))).copy(st[]);
st[].zip(predicates[].map!(f => f(st)))
.map!(s_t => s_t[0] == s_t[1]).copy(matches[]);
if (matches[].sum >= nStats - 1) {
if (matches[].all)
">>> Solution:".writeln;
else
writefln("Missed by statement: %d",
matches[].countUntil(false) + 1);
writefln("%-(%s %)", st[].map!q{ "FT"[a] });
}
}
}