2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -3,25 +3,31 @@
{{omit from|Openscad}}
{{omit from|TPP}}
This puzzle is borrowed from [http://math-frolic.blogspot.co.uk/2012/08/mind-wrenching.html here].
This puzzle is borrowed from   [http://math-frolic.blogspot.co.uk/2012/08/mind-wrenching.html math-frolic.blogspot].
Given the following twelve statements, which of them are true?
<pre>1. This is a numbered list of twelve statements.
2. Exactly 3 of the last 6 statements are true.
3. Exactly 2 of the even-numbered statements are true.
4. If statement 5 is true, then statements 6 and 7 are both true.
5. The 3 preceding statements are all false.
6. Exactly 4 of the odd-numbered statements are true.
7. Either statement 2 or 3 is true, but not both.
8. If statement 7 is true, then 5 and 6 are both true.
9. Exactly 3 of the first 6 statements are true.
<pre>
1. This is a numbered list of twelve statements.
2. Exactly 3 of the last 6 statements are true.
3. Exactly 2 of the even-numbered statements are true.
4. If statement 5 is true, then statements 6 and 7 are both true.
5. The 3 preceding statements are all false.
6. Exactly 4 of the odd-numbered statements are true.
7. Either statement 2 or 3 is true, but not both.
8. If statement 7 is true, then 5 and 6 are both true.
9. Exactly 3 of the first 6 statements are true.
10. The next two statements are both true.
11. Exactly 1 of statements 7, 8 and 9 are true.
12. Exactly 4 of the preceding statements are true.</pre>
12. Exactly 4 of the preceding statements are true.
</pre>
;Task:
When you get tired of trying to figure it out in your head,
write a program to solve it, and print the correct answer or answers.
Extra credit: also print out a table of near misses, that is,
solutions that are contradicted by only a single statement.
;Extra credit:
Print out a table of near misses, that is, solutions that are contradicted by only a single statement.
<br><br>

View file

@ -0,0 +1,116 @@
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
// convert int (0 or 1) to string (F or T)
inline
string str(int n)
{
return n ? "T": "F";
}
int main(void)
{
int solution_list_number = 1;
vector<string> st;
st = {
" 1. This is a numbered list of twelve statements.",
" 2. Exactly 3 of the last 6 statements are true.",
" 3. Exactly 2 of the even-numbered statements are true.",
" 4. If statement 5 is true, then statements 6 and 7 are both true.",
" 5. The 3 preceding statements are all false.",
" 6. Exactly 4 of the odd-numbered statements are true.",
" 7. Either statement 2 or 3 is true, but not both.",
" 8. If statement 7 is true, then 5 and 6 are both true.",
" 9. Exactly 3 of the first 6 statements are true.",
" 10. The next two statements are both true.",
" 11. Exactly 1 of statements 7, 8 and 9 are true.",
" 12. Exactly 4 of the preceding statements are true."
}; // Good solution is: 1 3 4 6 7 11 are true
int n = 12; // Number of statements.
int nTemp = (int)pow(2, n); // Number of solutions to check.
for (int counter = 0; counter < nTemp; counter++)
{
vector<int> s;
for (int k = 0; k < n; k++)
{
s.push_back((counter >> k) & 0x1);
}
vector<int> test(12);
int sum = 0;
// check each of the nTemp solutions for match.
// 1. This is a numbered list of twelve statements.
test[0] = s[0];
// 2. Exactly 3 of the last 6 statements are true.
sum = s[6]+ s[7]+s[8]+s[9]+s[10]+s[11];
test[1] = ((sum == 3) == s[1]);
// 3. Exactly 2 of the even-numbered statements are true.
sum = s[1]+s[3]+s[5]+s[7]+s[9]+s[11];
test[2] = ((sum == 2) == s[2]);
// 4. If statement 5 is true, then statements 6 and 7 are both true.
test[3] = ((s[4] ? (s[5] && s[6]) : true) == s[3]);
// 5. The 3 preceding statements are all false.
test[4] = (((s[1] + s[2] + s[3]) == 0) == s[4]);
// 6. Exactly 4 of the odd-numbered statements are true.
sum = s[0] + s[2] + s[4] + s[6] + s[8] + s[10];
test[5] = ((sum == 4) == s[5]);
// 7. Either statement 2 or 3 is true, but not both.
test[6] = (((s[1] + s[2]) == 1) == s[6]);
// 8. If statement 7 is true, then 5 and 6 are both true.
test[7] = ((s[6] ? (s[4] && s[5]) : true) == s[7]);
// 9. Exactly 3 of the first 6 statements are true.
sum = s[0]+s[1]+s[2]+s[3]+s[4]+s[5];
test[8] = ((sum == 3) == s[8]);
// 10. The next two statements are both true.
test[9] = ((s[10] && s[11]) == s[9]);
// 11. Exactly 1 of statements 7, 8 and 9 are true.
sum = s[6]+ s[7] + s[8];
test[10] = ((sum == 1) == s[10]);
// 12. Exactly 4 of the preceding statements are true.
sum = s[0]+s[1]+s[2]+s[3]+s[4]+s[5]+s[6]+s[7]+s[8]+s[9]+s[10];
test[11] = ((sum == 4) == s[11]);
// Check test results and print solution if 11 or 12 are true
int resultsTrue = 0;
for(unsigned int i = 0; i < test.size(); i++){
resultsTrue += test[i];
}
if(resultsTrue == 11 || resultsTrue == 12){
cout << solution_list_number++ << ". " ;
string output = "1:"+str(s[0])+" 2:"+str(s[1])+" 3:"+str(s[2])
+" 4:"+str(s[3])+" 5:"+str(s[4])+" 6:"+ str(s[5])
+" 7:"+str(s[6])+" 8:"+str(s[7])+" 9:"+str(s[8])
+" 10:"+str(s[9])+" 11:"+str(s[10])+" 12:"+ str(s[11]);
if (resultsTrue == 12) {
cout << "Full Match, good solution!" << endl;
cout << "\t" << output << endl;
}
else if(resultsTrue == 11){
int i;
for(i = 0; i < 12; i++){
if(test[i] == 0){
break;
}
}
cout << "Missed by one statement: " << st[i] << endl;
cout << "\t" << output << endl;
}
}
}
}

View file

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
public static class TwelveStatements
{
public static void Main() {
Func<Statements, bool>[] checks = {
st => st[1],
st => st[2] == (7.To(12).Count(i => st[i]) == 3),
st => st[3] == (2.To(12, by: 2).Count(i => st[i]) == 2),
st => st[4] == st[5].Implies(st[6] && st[7]),
st => st[5] == (!st[2] && !st[3] && !st[4]),
st => st[6] == (1.To(12, by: 2).Count(i => st[i]) == 4),
st => st[7] == (st[2] != st[3]),
st => st[8] == st[7].Implies(st[5] && st[6]),
st => st[9] == (1.To(6).Count(i => st[i]) == 3),
st => st[10] == (st[11] && st[12]),
st => st[11] == (7.To(9).Count(i => st[i]) == 1),
st => st[12] == (1.To(11).Count(i => st[i]) == 4)
};
for (Statements statements = new Statements(0); statements.Value < 4096; statements++) {
int count = 0;
int falseIndex = 0;
for (int i = 0; i < checks.Length; i++) {
if (checks[i](statements)) count++;
else falseIndex = i;
}
if (count == 0) Console.WriteLine($"{"All wrong:", -13}{statements}");
else if (count == 11) Console.WriteLine($"{$"Wrong at {falseIndex + 1}:", -13}{statements}");
else if (count == 12) Console.WriteLine($"{"All correct:", -13}{statements}");
}
}
struct Statements
{
public Statements(int value) : this() { Value = value; }
public int Value { get; }
public bool this[int index] => (Value & (1 << index - 1)) != 0;
public static Statements operator ++(Statements statements) => new Statements(statements.Value + 1);
public override string ToString() {
Statements copy = this; //Cannot access 'this' in anonymous method...
return string.Join(" ", from i in 1.To(12) select copy[i] ? "T" : "F");
}
}
//Extension methods
static bool Implies(this bool x, bool y) => !x || y;
static IEnumerable<int> To(this int start, int end, int by = 1) {
while (start <= end) {
yield return start;
start += by;
}
}
}

View file

@ -14,7 +14,7 @@ immutable texts = [
"exactly 1 of statements 7, 8 and 9 are true",
"exactly 4 of the preceding statements are true"];
immutable pure @safe /*@nogc*/ bool function(in bool[])[12] predicates = [
immutable pure @safe @nogc bool function(in bool[])[12] predicates = [
s => s.length == 12,
s => s[$ - 6 .. $].sum == 3,
s => s.dropOne.stride(2).sum == 2,
@ -28,7 +28,7 @@ immutable pure @safe /*@nogc*/ bool function(in bool[])[12] predicates = [
s => s[6 .. 9].sum == 1,
s => s[0 .. 11].sum == 4];
void main() {
void main() @safe {
enum nStats = predicates.length;
foreach (immutable n; 0 .. 2 ^^ nStats) {

View file

@ -0,0 +1,59 @@
#import system.
#import system'routines.
#import extensions.
#class(extension)op
{
#method printSolution : bits
= self zip:bits &into:
(:s:b) [ s iif:"T":"F" + (s xor:b) iif:"* ":" " ] summarize:(String new).
}
#symbol puzzle =
(
bits [ bits length == 12 ],
bits [ bits last:6 select &each:x [ x iif:1:0 ] summarize == 3 ],
bits [ bits zip:(RangeEnumerator new &from:1 &to:12)
&into:(:x:i) [ (i int is &even)and:x iif:1:0 ] summarize == 2 ],
bits [ bits@4 iif:(bits@5 && bits@6):true ],
bits [ (bits@1 || bits@2 || bits@3) not ],
bits [ bits zip:(RangeEnumerator new &from:1 &to:12)
&into:(:x:i) [ (i int is &odd)and:x iif:1:0 ] summarize == 4 ],
bits [ (bits@1) xor:(bits@2) ],
bits [ bits@6 iif:(bits@5 && bits@4):true ],
bits [ bits top:6 select &each:x [ x iif:1:0 ] summarize == 3 ],
bits [ bits@10 && bits@11 ],
bits [ ((bits@6) iif:1:0 + (bits@7) iif:1:0 + (bits@8) iif:1:0)==1 ],
bits [ bits top:11 select &each:x [ x iif:1:0 ] summarize == 4 ]
).
#symbol program =
[
console writeLine:"".
0 till:(2 power &int:12) &doEach:n
[
#var bits := BitArray32 new:n top:12 toArray.
#var results := puzzle select &each: r [ r eval:bits ] toArray.
#var counts := bits zip:results &into:(:b:r) [ b xor:r iif:1:0 ] summarize.
counts =>
0 ? [ console writeLine:"Total hit :":(results printSolution:bits). ]
1 ? [ console writeLine:"Near miss :":(results printSolution:bits). ]
12 ? [ console writeLine:"Total miss:":(results printSolution:bits). ].
].
console readChar.
].

View file

@ -1,6 +1,6 @@
sub infix:<> ($protasis,$apodosis) { !$protasis or $apodosis }
sub infix:<> ($protasis, $apodosis) { !$protasis or $apodosis }
my @tests = { True }, # (there's no 0th statement)
my @tests =
{ .end == 12 and all(.[1..12]) === any(True, False) },
{ 3 == [+] .[7..12] },
{ 2 == [+] .[2,4...12] },
@ -12,31 +12,24 @@ my @tests = { True }, # (there's no 0th statement)
{ 3 == [+] .[1..6] },
{ all .[11,12] },
{ one .[7,8,9] },
{ 4 == [+] .[1..11] };
{ 4 == [+] .[1..11] },
;
my @good;
my @bad;
my @ugly;
my @solutions;
my @misses;
for reverse 0 ..^ 2**12 -> $i {
my @b = $i.fmt("%012b").comb;
my @assert = True, | @b.map: { 1 == $_ }
my @result = @tests.map: { .(@assert).so }
my @s = ( $_ if $_ and @assert[$_] for 1..12 );
if @result eqv @assert {
push @good, "<{@s}> is consistent.";
}
else {
my @cons = gather for 1..12 {
if @assert[$_] !eqv @result[$_] {
take @result[$_] ?? $_ !! "¬$_";
}
}
my $mess = "<{@s}> implies {@cons}.";
if @cons == 1 { push @bad, $mess } else { push @ugly, $mess }
for [X] (True, False) xx 12 {
my @assert = Nil, |$_;
my @result = Nil, |@tests.map({ ?.(@assert) });
my @true = @assert.grep(?*, :k);
my @cons = (@assert Z=== @result).grep(!*, :k);
given @cons {
when 0 { push @solutions, "<{@true}> is consistent."; }
when 1 { push @misses, "<{@true}> implies { "¬" if !@result[~$_] }$_." }
}
}
.say for @good;
say "\nNear misses:";
.say for @bad;
.say for @solutions;
say "";
say "Near misses:";
.say for @misses;

View file

@ -1,32 +1,31 @@
/*REXX program solves the "Twelve Statement Puzzle". */
q=12; @stmt=right('statement',20) /*number of statements in puzzle.*/
m=0 /*[↓] statement 1 is TRUE by fiat*/
do pass=1 for 2 /*find the maximum number trues. */
do e=0 for 2**(q-1); n = '1'right(x2b(d2x(e)), q-1, 0)
do b=1 for q /*define the various bits. */
@.b=substr(n,b,1) /*define a particular @ bit. */
/*REXX program solves the "Twelve Statement Puzzle". */
q=12; @stmt=right('statement',20) /*number of statements in the puzzle. */
m=0 /*[↓] statement one is TRUE by fiat.*/
do pass=1 for 2 /*find the maximum number of "trues". */
do e=0 for 2**(q-1); n = '1'right( x2b( d2x( e ) ), q-1, 0)
do b=1 for q /*define various bits in the number Q.*/
@.b=substr(n, b, 1) /*define a particular @ bit (in Q).*/
end /*b*/
if @.1 then if yeses(1,1) \==1 then iterate
if @.2 then if yeses(7,12) \==3 then iterate
if @.3 then if yeses(2,12,2) \==2 then iterate
if @.4 then if yeses(5,5) then if yeses(6,7) \==2 then iterate
if @.5 then if yeses(2,4) \==0 then iterate
if @.6 then if yeses(1,12,2) \==4 then iterate
if @.7 then if yeses(2,3) \==1 then iterate
if @.8 then if yeses(7,7) then if yeses(5,6) \==2 then iterate
if @.9 then if yeses(1,6) \==3 then iterate
if @.10 then if yeses(11,12) \==2 then iterate
if @.11 then if yeses(7,9) \==1 then iterate
if @.12 then if yeses(1,11) \==4 then iterate
_=yeses(1,12)
if pass==1 then do; m=max(m,_); iterate; end
else if _\==m then iterate
do j=1 for q; _=substr(n,j,1)
if _ then say @stmt right(j,2) " is " word('false true',1+_)
if @.1 then if yeses(1, 1) \==1 then iterate
if @.2 then if yeses(7, 12) \==3 then iterate
if @.3 then if yeses(2, 12,2) \==2 then iterate
if @.4 then if yeses(5, 5) then if yeses(6, 7) \==2 then iterate
if @.5 then if yeses(2, 4) \==0 then iterate
if @.6 then if yeses(1, 12,2) \==4 then iterate
if @.7 then if yeses(2, 3) \==1 then iterate
if @.8 then if yeses(7, 7) then if yeses(5,6) \==2 then iterate
if @.9 then if yeses(1, 6) \==3 then iterate
if @.10 then if yeses(11,12) \==2 then iterate
if @.11 then if yeses(7, 9) \==1 then iterate
if @.12 then if yeses(1, 11) \==4 then iterate
g=yeses(1, 12)
if pass==1 then do; m=max(m,g); iterate; end
else if g\==m then iterate
do j=1 for q; z=substr(n, j, 1)
if z then say @stmt right(j, 2) " is " word('false true', 1 + z)
end /*tell*/
end /*e*/
end /*pass*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────YESES subroutine────────────────────*/
yeses: parse arg L,H,B; #=0; do i=L to H by word(B 1,1); #=#+@.i; end
return #
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
yeses: parse arg L,H,B; #=0; do i=L to H by word(B 1, 1); #=#+@.i; end; return #

View file

@ -1,10 +1,10 @@
/*REXX program solves the "Twelve Statement Puzzle". */
q=12; @stmt=right('statement',20) /*number of statements in puzzle.*/
m=0 /*[↓] statement 1 is TRUE by fiat*/
do pass=1 for 2 /*find the maximum number trues. */
do e=0 for 2**(q-1); n = '1'right(x2b(d2x(e)), q-1, 0)
do b=1 for q /*define the various bits. */
@.b=substr(n,b,1) /*define a particular @ bit. */
/*REXX program solves the "Twelve Statement Puzzle". */
q=12; @stmt=right('statement',20) /*number of statements in the puzzle. */
m=0 /*[↓] statement one is TRUE by fiat.*/
do pass=1 for 2 /*find the maximum number of "trues". */
do e=0 for 2**(q-1); n = '1'right( x2b( d2x( e ) ), q-1, 0)
do b=1 for q /*define various bits in the number Q.*/
@.b=substr(n, b, 1) /*define a particular @ bit (in Q).*/
end /*b*/
if @.1 then if \ @.1 then iterate
if @.2 then if @.7+@.8+@.9+@.10+@.11+@.12 \==3 then iterate
@ -17,14 +17,13 @@ m=0 /*[↓] statement 1 is TRUE by fiat*/
if @.9 then if @.1+@.2+@.3+@.4+@.5+@.6 \==3 then iterate
if @.10 then if \ (@.11 & @.12) then iterate
if @.11 then if @.7+@.8+@.9 \==1 then iterate
_=@.1+@.2+@.3+@.4+@.5+@.6+@.7+@.8+@.9+@.10+@.11
if @.12 then if _ \==4 then iterate
_=_+@.12
if pass==1 then do; m=max(m,_); iterate; end
else if _\==m then iterate
do j=1 for q
if @.j then say @stmt right(j,2) " is " word('false true',1+@.j)
end /*j*/
end /*e*/
end /*pass*/
/*stick a fork in it, we're done.*/
g=@.1 +@.2 +@.3 +@.4 +@.5 +@.6 +@.7 +@.8+ @.9 +@.10 +@.11
if @.12 then if g \==4 then iterate
g=g + @.12
if pass==1 then do; m=max(m,g); iterate; end
else if g\==m then iterate
do j=1 for q; z=substr(n, j, 1)
if z then say @stmt right(j, 2) " is " word('false true', 1+z)
end /*tell*/
end /*e*/
end /*pass*/ /*stick a fork in it, we're all done. */

View file

@ -1,10 +1,10 @@
/*REXX program solves the "Twelve Statement Puzzle". */
q=12; @stmt=right('statement',20) /*number of statements in puzzle.*/
m=0 /*[↓] statement 1 is TRUE by fiat*/
do pass=1 for 2 /*find the maximum number trues. */
do e=0 for 2**(q-1); n = '1'right(x2b(d2x(e)), q-1, 0)
parse var n @1 2 @2 3 @3 4 @4 5 @5 6 @6 7 @7 8 @8 9 @9 10 @10 11 @11 12 @12
/*███ if @1 then if \ @1 then iterate ███*/
/*REXX program solves the "Twelve Statement Puzzle". */
q=12; @stmt=right('statement',20) /*number of statements in the puzzle. */
m=0 /*[↓] statement one is TRUE by fiat.*/
do pass=1 for 2 /*find the maximum number of "trues". */
do e=0 for 2**(q-1); n = '1'right( x2b( d2x( e ) ), q-1, 0)
parse var n @1 2 @2 3 @3 4 @4 5 @5 6 @6 7 @7 8 @8 9 @9 10 @10 11 @11 12 @12
/*▒▒▒▒ if @1 then if \ @1 then iterate ▒▒▒▒*/
if @2 then if @7+@8+@9+@10+@11+@12 \==3 then iterate
if @3 then if @2+@4+@6+@8+@10+@12 \==2 then iterate
if @4 then if @5 then if \(@6 & @7) then iterate
@ -15,14 +15,13 @@ m=0 /*[↓] statement 1 is TRUE by fiat*/
if @9 then if @1+@2+@3+@4+@5+@6 \==3 then iterate
if @10 then if \ (@11 & @12) then iterate
if @11 then if @7+@8+@9 \==1 then iterate
_=@1+@2+@3+@4+@5+@6+@7+@8+@9+@10+@11 /*shortcut*/
if @12 then if _ \==4 then iterate
_=_+@12
if pass==1 then do; m=max(m,_); iterate; end
else if _\==m then iterate
do j=1 for q; _=substr(n,j,1)
if _ then say @stmt right(j,2) " is " word('false true',1+_)
g=@1 + @2 + @3 + @4 + @5 + @6 + @7 + @8 + @9 + @10 + @11
if @12 then if g \==4 then iterate
g=g + @12
if pass==1 then do; m=max(m,g); iterate; end
else if g\==m then iterate
do j=1 for q; z=substr(n, j, 1)
if z then say @stmt right(j, 2) " is " word('false true', 1+z)
end /*j*/
end /*e*/
end /*pass*/
/*stick a fork in it, we're done.*/
end /*pass*/ /*stick a fork in it, we're all done. */

View file

@ -1,37 +1,36 @@
@(do
(defmacro defconstraints (name size-name (var) . forms)
^(progn (defvar ,size-name ,(length forms))
(defun ,name (,var)
(list ,*forms))))
(defmacro defconstraints (name size-name (var) . forms)
^(progn (defvar ,size-name ,(length forms))
(defun ,name (,var)
(list ,*forms))))
(defconstraints con con-count (s)
(= (length s) con-count) ;; tautology
(= (countq t [s -6..t]) 3)
(= (countq t (mapcar (op if (evenp @1) @2) (range 1) s)) 2)
(if [s 4] (and [s 5] [s 6]) t)
(none [s 1..3])
(= (countq t (mapcar (op if (oddp @1) @2) (range 1) s)) 4)
(and (or [s 1] [s 2]) (not (and [s 1] [s 2])))
(if [s 6] (and [s 4] [s 5]) t)
(= (countq t [s 0..6]) 3)
(and [s 10] [s 11])
(= (countq t [s 6..9]) 1)
(= (countq t [s 0..con-count]) 4))
(defconstraints con con-count (s)
(= (length s) con-count) ;; tautology
(= (countq t [s -6..t]) 3)
(= (countq t (mapcar (op if (evenp @1) @2) (range 1) s)) 2)
(if [s 4] (and [s 5] [s 6]) t)
(none [s 1..3])
(= (countq t (mapcar (op if (oddp @1) @2) (range 1) s)) 4)
(and (or [s 1] [s 2]) (not (and [s 1] [s 2])))
(if [s 6] (and [s 4] [s 5]) t)
(= (countq t [s 0..6]) 3)
(and [s 10] [s 11])
(= (countq t [s 6..9]) 1)
(= (countq t [s 0..con-count]) 4))
(defun true-indices (truths)
(mappend (do if @1 ^(,@2)) truths (range 1)))
(defun true-indices (truths)
(mappend (do if @1 ^(,@2)) truths (range 1)))
(defvar results
(append-each ((truths (rperm '(nil t) con-count)))
(let* ((vals (con truths))
(consist [mapcar eq truths vals])
(wrong-count (countq nil consist))
(pos-wrong (+ 1 (or (posq nil consist) -2))))
(cond
((zerop wrong-count)
^((:----> ,*(true-indices truths))))
((= 1 wrong-count)
^((:close ,*(true-indices truths) (:wrong ,pos-wrong))))))))
(defvar results
(append-each ((truths (rperm '(nil t) con-count)))
(let* ((vals (con truths))
(consist [mapcar eq truths vals])
(wrong-count (countq nil consist))
(pos-wrong (+ 1 (or (posq nil consist) -2))))
(cond
((zerop wrong-count)
^((:----> ,*(true-indices truths))))
((= 1 wrong-count)
^((:close ,*(true-indices truths) (:wrong ,pos-wrong))))))))
(each ((r results))
(put-line `@r`)))
(each ((r results))
(put-line `@r`))