Twelve statements: Difference between revisions
Content deleted Content added
→{{header|REXX}}: added the REXX language. -- ~~~~ |
Move Java so it is inserted in alphabetical order. |
||
Line 504: | Line 504: | ||
statements = [ (==12) . length, |
statements = [ (==12) . length, |
||
3 |
3 ? [length statements-6..], |
||
2 |
2 ? [1,3..], |
||
4 |
4 ? [4..6], |
||
0 |
0 ? [1..3], |
||
4 |
4 ? [0,2..], |
||
1 |
1 ? [1,2], |
||
6 |
6 ? [4..6], |
||
3 |
3 ? [0..5], |
||
2 |
2 ? [10,11], |
||
1 |
1 ? [6,7,8], |
||
4 |
4 ? [0..10] |
||
] where |
] where |
||
(s |
(s ? x) b = s == (sum . map (b!!) . takeWhile (< length b)) x |
||
(a |
(a ? x) b = (b!!a == 0) || all ((==1).(b!!)) x |
||
testall s n = [(b, w) | b <- tf s, w <- [wrongness b s], length w == n] |
testall s n = [(b, w) | b <- tf s, w <- [wrongness b s], length w == n] |
||
Line 579: | Line 579: | ||
'''All true''' |
'''All true''' |
||
<lang j> (#~0=#@{::~&_1"1) testall S |
<lang j> (#~0=#@{::~&_1"1) testall S |
||
+------------------------+ |
|||
┌───────────────────────┬┐ |
|||
¦1 0 1 1 0 1 1 0 0 0 1 0¦¦ |
|||
+------------------------+</lang> |
|||
└───────────────────────┴┘</lang> |
|||
'''Near misses''' |
'''Near misses''' |
||
<lang j> (#~1=#@{::~&_1"1) testall S |
<lang j> (#~1=#@{::~&_1"1) testall S |
||
+--------------------------+ |
|||
┌───────────────────────┬──┐ |
|||
¦0 0 0 0 1 0 0 1 0 0 1 0¦0 ¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦0 0 0 0 1 0 0 1 0 1 1 1¦0 ¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦0 0 0 1 0 0 0 1 0 1 1 1¦0 ¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦1 0 0 0 1 0 0 0 0 0 0 0¦7 ¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦1 0 0 0 1 0 0 1 0 0 0 0¦10¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦1 0 0 0 1 0 0 1 0 0 1 0¦11¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦1 0 0 0 1 0 0 1 0 1 1 1¦11¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦1 0 0 0 1 1 0 0 1 0 1 0¦7 ¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦1 0 0 1 0 0 0 0 0 0 0 0¦7 ¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦1 0 0 1 0 0 0 1 0 1 1 1¦11¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦1 0 0 1 0 1 0 1 1 0 0 0¦5 ¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦1 0 1 1 0 0 0 1 1 0 0 0¦6 ¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦1 0 1 1 0 1 1 0 1 0 0 0¦8 ¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦1 1 0 1 0 0 1 0 1 0 0 1¦11¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦1 1 0 1 0 0 1 0 1 1 0 0¦9 ¦ |
|||
+-----------------------+--¦ |
|||
├───────────────────────┼──┤ |
|||
¦1 1 0 1 0 0 1 1 1 0 0 0¦7 ¦ |
|||
+--------------------------+</lang> |
|||
└───────────────────────┴──┘</lang> |
|||
'''Iterative for all true''' |
'''Iterative for all true''' |
||
Line 622: | Line 622: | ||
<lang j> (-N)&{. #: S <:@]^:((]-.@-:(apply&><)"1) (-N)&{.@#:@])^:(_) 2^N=.#S |
<lang j> (-N)&{. #: S <:@]^:((]-.@-:(apply&><)"1) (-N)&{.@#:@])^:(_) 2^N=.#S |
||
1 0 1 1 0 1 1 0 0 0 1 0</lang> |
1 0 1 1 0 1 1 0 0 0 1 0</lang> |
||
=={{header|Java}}== |
|||
The following Java code uses brute force. It tries to translate the logical statements as naturally as possible. The run time is almost zero. --[[User:Mga010|Rene Grothmann]] 10:33, 28 October 2012 (UTC) |
|||
<lang Java> |
|||
public class LogicPuzzle |
|||
{ |
|||
boolean S[] = new boolean[13]; |
|||
int Count = 0; |
|||
public boolean check2 () |
|||
{ |
|||
int count = 0; |
|||
for (int k = 7; k <= 12; k++) |
|||
if (S[k]) count++; |
|||
return S[2] == (count == 3); |
|||
} |
|||
public boolean check3 () |
|||
{ |
|||
int count = 0; |
|||
for (int k = 2; k <= 12; k += 2) |
|||
if (S[k]) count++; |
|||
return S[3] == (count == 2); |
|||
} |
|||
public boolean check4 () |
|||
{ |
|||
return S[4] == ( !S[5] || S[6] && S[7]); |
|||
} |
|||
public boolean check5 () |
|||
{ |
|||
return S[5] == ( !S[2] && !S[3] && !S[4]); |
|||
} |
|||
public boolean check6 () |
|||
{ |
|||
int count = 0; |
|||
for (int k = 1; k <= 11; k += 2) |
|||
if (S[k]) count++; |
|||
return S[6] == (count == 4); |
|||
} |
|||
public boolean check7 () |
|||
{ |
|||
return S[7] == ((S[2] || S[3]) && !(S[2] && S[3])); |
|||
} |
|||
public boolean check8 () |
|||
{ |
|||
return S[8] == ( !S[7] || S[5] && S[6]); |
|||
} |
|||
public boolean check9 () |
|||
{ |
|||
int count = 0; |
|||
for (int k = 1; k <= 6; k++) |
|||
if (S[k]) count++; |
|||
return S[9] == (count == 3); |
|||
} |
|||
public boolean check10 () |
|||
{ |
|||
return S[10] == (S[11] && S[12]); |
|||
} |
|||
public boolean check11 () |
|||
{ |
|||
int count = 0; |
|||
for (int k = 7; k <= 9; k++) |
|||
if (S[k]) count++; |
|||
return S[11] == (count == 1); |
|||
} |
|||
public boolean check12 () |
|||
{ |
|||
int count = 0; |
|||
for (int k = 1; k <= 11; k++) |
|||
if (S[k]) count++; |
|||
return S[12] == (count == 4); |
|||
} |
|||
public void check () |
|||
{ |
|||
if (check2() && check3() && check4() && check5() && check6() |
|||
&& check7() && check8() && check9() && check10() && check11() |
|||
&& check12()) |
|||
{ |
|||
for (int k = 1; k <= 12; k++) |
|||
if (S[k]) System.out.print(k + " "); |
|||
System.out.println(); |
|||
Count++; |
|||
} |
|||
} |
|||
public void recurseAll (int k) |
|||
{ |
|||
if (k == 13) |
|||
check(); |
|||
else |
|||
{ |
|||
S[k] = false; |
|||
recurseAll(k + 1); |
|||
S[k] = true; |
|||
recurseAll(k + 1); |
|||
} |
|||
} |
|||
public static void main (String args[]) |
|||
{ |
|||
LogicPuzzle P = new LogicPuzzle(); |
|||
P.S[1] = true; |
|||
P.recurseAll(2); |
|||
System.out.println(); |
|||
System.out.println(P.Count + " Solutions found."); |
|||
} |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
1 3 4 6 7 11 |
|||
1 Solutions found. |
|||
</pre> |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
<lang perl6>sub infix:< |
<lang perl6>sub infix:<?> ($protasis,$apodosis) { !$protasis or $apodosis } |
||
my @tests = { True }, # (there's no 0th statement) |
my @tests = { True }, # (there's no 0th statement) |
||
Line 630: | Line 756: | ||
{ 3 == [+] .[7..12] }, |
{ 3 == [+] .[7..12] }, |
||
{ 2 == [+] .[2,4...12] }, |
{ 2 == [+] .[2,4...12] }, |
||
{ .[5] |
{ .[5] ? all .[6,7] }, |
||
{ none .[2,3,4] }, |
{ none .[2,3,4] }, |
||
{ 4 == [+] .[1,3...11] }, |
{ 4 == [+] .[1,3...11] }, |
||
{ one .[2,3] }, |
{ one .[2,3] }, |
||
{ .[7] |
{ .[7] ? all .[5,6] }, |
||
{ 3 == [+] .[1..6] }, |
{ 3 == [+] .[1..6] }, |
||
{ all .[11,12] }, |
{ all .[11,12] }, |
||
Line 827: | Line 953: | ||
Missed by one statement: 8, If statement 7 is true, then 5 and 6 are both true: |
Missed by one statement: 8, If statement 7 is true, then 5 and 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</pre> |
1:T, 2:T, 3:F, 4:T, 5:F, 6:F, 7:T, 8:T, 9:T, 10:F, 11:F, 12:F</pre> |
||
=={{header|Java}}== |
|||
The following Java code uses brute force. It tries to translate the logical statements as naturally as possible. The run time is almost zero. --[[User:Mga010|Rene Grothmann]] 10:33, 28 October 2012 (UTC) |
|||
<lang Java> |
|||
public class LogicPuzzle |
|||
{ |
|||
boolean S[] = new boolean[13]; |
|||
int Count = 0; |
|||
public boolean check2 () |
|||
{ |
|||
int count = 0; |
|||
for (int k = 7; k <= 12; k++) |
|||
if (S[k]) count++; |
|||
return S[2] == (count == 3); |
|||
} |
|||
public boolean check3 () |
|||
{ |
|||
int count = 0; |
|||
for (int k = 2; k <= 12; k += 2) |
|||
if (S[k]) count++; |
|||
return S[3] == (count == 2); |
|||
} |
|||
public boolean check4 () |
|||
{ |
|||
return S[4] == ( !S[5] || S[6] && S[7]); |
|||
} |
|||
public boolean check5 () |
|||
{ |
|||
return S[5] == ( !S[2] && !S[3] && !S[4]); |
|||
} |
|||
public boolean check6 () |
|||
{ |
|||
int count = 0; |
|||
for (int k = 1; k <= 11; k += 2) |
|||
if (S[k]) count++; |
|||
return S[6] == (count == 4); |
|||
} |
|||
public boolean check7 () |
|||
{ |
|||
return S[7] == ((S[2] || S[3]) && !(S[2] && S[3])); |
|||
} |
|||
public boolean check8 () |
|||
{ |
|||
return S[8] == ( !S[7] || S[5] && S[6]); |
|||
} |
|||
public boolean check9 () |
|||
{ |
|||
int count = 0; |
|||
for (int k = 1; k <= 6; k++) |
|||
if (S[k]) count++; |
|||
return S[9] == (count == 3); |
|||
} |
|||
public boolean check10 () |
|||
{ |
|||
return S[10] == (S[11] && S[12]); |
|||
} |
|||
public boolean check11 () |
|||
{ |
|||
int count = 0; |
|||
for (int k = 7; k <= 9; k++) |
|||
if (S[k]) count++; |
|||
return S[11] == (count == 1); |
|||
} |
|||
public boolean check12 () |
|||
{ |
|||
int count = 0; |
|||
for (int k = 1; k <= 11; k++) |
|||
if (S[k]) count++; |
|||
return S[12] == (count == 4); |
|||
} |
|||
public void check () |
|||
{ |
|||
if (check2() && check3() && check4() && check5() && check6() |
|||
&& check7() && check8() && check9() && check10() && check11() |
|||
&& check12()) |
|||
{ |
|||
for (int k = 1; k <= 12; k++) |
|||
if (S[k]) System.out.print(k + " "); |
|||
System.out.println(); |
|||
Count++; |
|||
} |
|||
} |
|||
public void recurseAll (int k) |
|||
{ |
|||
if (k == 13) |
|||
check(); |
|||
else |
|||
{ |
|||
S[k] = false; |
|||
recurseAll(k + 1); |
|||
S[k] = true; |
|||
recurseAll(k + 1); |
|||
} |
|||
} |
|||
public static void main (String args[]) |
|||
{ |
|||
LogicPuzzle P = new LogicPuzzle(); |
|||
P.S[1] = true; |
|||
P.recurseAll(2); |
|||
System.out.println(); |
|||
System.out.println(P.Count + " Solutions found."); |
|||
} |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
1 3 4 6 7 11 |
|||
1 Solutions found. |
|||
</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 989: | Line 989: | ||
end /*pass*/ |
end /*pass*/ |
||
exit /*stick a fork in it, we're done.*/ |
exit /*stick a fork in it, we're done.*/ |
||
/*----------------------------------YESES subroutine--------------------*/ |
|||
/*──────────────────────────────────YESES subroutine────────────────────*/ |
|||
yeses: parse arg L,H,B; #=0 |
yeses: parse arg L,H,B; #=0 |
||
do i=L to H by word(B 1,1); #=#+@.i; end /*i*/ |
do i=L to H by word(B 1,1); #=#+@.i; end /*i*/ |
||
Line 1,049: | Line 1,049: | ||
do e=0 for 2**(q-1); n='1'right(x2b(d2x(e)), q-1, 0) |
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 |
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 @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 @3 then if @2+@4+@6+@8+@10+@12 \==2 then iterate |
||
Line 1,152: | Line 1,152: | ||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
exact match (S(1) |
exact match (S(1)?¬S(2)?S(3)?S(4)?¬S(5)?S(6)?S(7)?¬S(8)?¬S(9)?¬S(10)?S(11)?¬S(12)) |
||
almost found (¬S(1) |
almost found (¬S(1)?¬S(2)?¬S(3)?¬S(4)?S(5)?¬S(6)?¬S(7)?S(8)?¬S(9)?¬S(10)?S(11)?¬S(12)) ? S(1) |
||
almost found (¬S(1) |
almost found (¬S(1)?¬S(2)?¬S(3)?¬S(4)?S(5)?¬S(6)?¬S(7)?S(8)?¬S(9)?S(10)?S(11)?S(12)) ? S(1) |
||
almost found (¬S(1) |
almost found (¬S(1)?¬S(2)?¬S(3)?S(4)?¬S(5)?¬S(6)?¬S(7)?S(8)?¬S(9)?S(10)?S(11)?S(12)) ? S(1) |
||
almost found (S(1) |
almost found (S(1)?¬S(2)?¬S(3)?¬S(4)?S(5)?¬S(6)?¬S(7)?¬S(8)?¬S(9)?¬S(10)?¬S(11)?¬S(12)) ? S(8) |
||
almost found (S(1) |
almost found (S(1)?¬S(2)?¬S(3)?¬S(4)?S(5)?¬S(6)?¬S(7)?S(8)?¬S(9)?¬S(10)?¬S(11)?¬S(12)) ? S(11) |
||
almost found (S(1) |
almost found (S(1)?¬S(2)?¬S(3)?¬S(4)?S(5)?¬S(6)?¬S(7)?S(8)?¬S(9)?¬S(10)?S(11)?¬S(12)) ? S(12) |
||
almost found (S(1) |
almost found (S(1)?¬S(2)?¬S(3)?¬S(4)?S(5)?¬S(6)?¬S(7)?S(8)?¬S(9)?S(10)?S(11)?S(12)) ? ¬S(12) |
||
almost found (S(1) |
almost found (S(1)?¬S(2)?¬S(3)?¬S(4)?S(5)?S(6)?¬S(7)?¬S(8)?S(9)?¬S(10)?S(11)?¬S(12)) ? S(8) |
||
almost found (S(1) |
almost found (S(1)?¬S(2)?¬S(3)?S(4)?¬S(5)?¬S(6)?¬S(7)?¬S(8)?¬S(9)?¬S(10)?¬S(11)?¬S(12)) ? S(8) |
||
almost found (S(1) |
almost found (S(1)?¬S(2)?¬S(3)?S(4)?¬S(5)?¬S(6)?¬S(7)?S(8)?¬S(9)?S(10)?S(11)?S(12)) ? ¬S(12) |
||
almost found (S(1) |
almost found (S(1)?¬S(2)?¬S(3)?S(4)?¬S(5)?S(6)?¬S(7)?S(8)?S(9)?¬S(10)?¬S(11)?¬S(12)) ? ¬S(6) |
||
almost found (S(1) |
almost found (S(1)?¬S(2)?S(3)?S(4)?¬S(5)?¬S(6)?¬S(7)?S(8)?S(9)?¬S(10)?¬S(11)?¬S(12)) ? S(7) |
||
almost found (S(1) |
almost found (S(1)?¬S(2)?S(3)?S(4)?¬S(5)?S(6)?S(7)?¬S(8)?S(9)?¬S(10)?¬S(11)?¬S(12)) ? ¬S(9) |
||
almost found (S(1) |
almost found (S(1)?S(2)?¬S(3)?S(4)?¬S(5)?¬S(6)?S(7)?¬S(8)?S(9)?¬S(10)?¬S(11)?S(12)) ? ¬S(12) |
||
almost found (S(1) |
almost found (S(1)?S(2)?¬S(3)?S(4)?¬S(5)?¬S(6)?S(7)?¬S(8)?S(9)?S(10)?¬S(11)?¬S(12)) ? ¬S(10) |
||
almost found (S(1) |
almost found (S(1)?S(2)?¬S(3)?S(4)?¬S(5)?¬S(6)?S(7)?S(8)?S(9)?¬S(10)?¬S(11)?¬S(12)) ? ¬S(8) |
||
</pre> |
</pre> |