Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
265
Task/Twelve-statements/Bracmat/twelve-statements.bracmat
Normal file
265
Task/Twelve-statements/Bracmat/twelve-statements.bracmat
Normal file
|
|
@ -0,0 +1,265 @@
|
|||
(
|
||||
( number
|
||||
= n done ntest oldFT
|
||||
. !arg:(?done.(=?ntest).?oldFT)
|
||||
& 0:?n
|
||||
& ( !done
|
||||
: ?
|
||||
( !ntest
|
||||
. !oldFT&1+!n:?n&~
|
||||
)
|
||||
?
|
||||
| !n
|
||||
)
|
||||
)
|
||||
& ( STATEMENTS
|
||||
= ( (1."This is a numbered list of twelve statements.")
|
||||
. 1
|
||||
. (
|
||||
= n nr done toDo
|
||||
. !arg:(?done.?toDo)
|
||||
& 0:?n
|
||||
& whl
|
||||
' ( !done:(?nr.?) ?done
|
||||
& 1+!n:!nr:?n
|
||||
)
|
||||
& whl
|
||||
' ( !toDo:((?nr.?).?) ?toDo
|
||||
& 1+!n:!nr:?n
|
||||
)
|
||||
& (!n:12&true|false)
|
||||
)
|
||||
)
|
||||
( (2."Exactly 3 of the last 6 statements are true.")
|
||||
. end
|
||||
. (
|
||||
= done toDo lastSix
|
||||
. !arg:(?done.?toDo)
|
||||
& !done:? [-7 ?lastSix
|
||||
& ( number$(!lastSix.(=?).true):3
|
||||
& true
|
||||
| false
|
||||
)
|
||||
)
|
||||
)
|
||||
( (3."Exactly 2 of the even-numbered statements are true.")
|
||||
. end
|
||||
. (
|
||||
= done toDo ii
|
||||
. !arg:(?done.?toDo)
|
||||
& ( number
|
||||
$ ( !done
|
||||
. (=?ii&!ii*1/2:~/)
|
||||
. true
|
||||
)
|
||||
: 2
|
||||
& true
|
||||
| false
|
||||
)
|
||||
)
|
||||
)
|
||||
( (4."If statement 5 is true, then statements 6 and 7 are both true.")
|
||||
. 7
|
||||
. (
|
||||
= done toDo
|
||||
. !arg:(?done.?toDo)
|
||||
& ( !done
|
||||
: ( ? (5.false) ?
|
||||
| ? (6.true) ?
|
||||
: ? (7.true) ?
|
||||
)
|
||||
& true
|
||||
| false
|
||||
)
|
||||
)
|
||||
)
|
||||
( (5."The 3 preceding statements are all false.")
|
||||
. 5
|
||||
. (
|
||||
= done toDo
|
||||
. !arg:(?done.?toDo)
|
||||
& ( !done
|
||||
: ?
|
||||
(?.false)
|
||||
(?.false)
|
||||
(?.false)
|
||||
(?.?)
|
||||
& true
|
||||
| false
|
||||
)
|
||||
)
|
||||
)
|
||||
( (6."Exactly 4 of the odd-numbered statements are true.")
|
||||
. end
|
||||
. (
|
||||
= done toDo i
|
||||
. !arg:(?done.?toDo)
|
||||
& ( number
|
||||
$ ( !done
|
||||
. (=?i&!i*1/2:/)
|
||||
. true
|
||||
)
|
||||
: 4
|
||||
& true
|
||||
| false
|
||||
)
|
||||
)
|
||||
)
|
||||
( (7."Either statement 2 or 3 is true, but not both.")
|
||||
. 7
|
||||
. (
|
||||
= done toDo
|
||||
. !arg:(?done.?toDo)
|
||||
& ( number
|
||||
$ (!done.(=2|3).true)
|
||||
: 1
|
||||
& true
|
||||
| false
|
||||
)
|
||||
)
|
||||
)
|
||||
( (8."If statement 7 is true, then 5 and 6 are both true.")
|
||||
. 8
|
||||
. (
|
||||
= done toDo
|
||||
. !arg:(?done.?toDo)
|
||||
& ( !done
|
||||
: ( ? (7.false) ?
|
||||
| ? (5.true) ?
|
||||
: ? (6.true) ?
|
||||
)
|
||||
& true
|
||||
| false
|
||||
)
|
||||
)
|
||||
)
|
||||
( (9."Exactly 3 of the first 6 statements are true.")
|
||||
. 9
|
||||
. (
|
||||
= done toDo firstSix
|
||||
. !arg:(?done.?toDo)
|
||||
& !done:?firstSix [6 ?
|
||||
& ( number$(!firstSix.(=?).true):3
|
||||
& true
|
||||
| false
|
||||
)
|
||||
)
|
||||
)
|
||||
( (10."The next two statements are both true.")
|
||||
. 12
|
||||
. (
|
||||
= done toDo
|
||||
. !arg:(?done.?toDo)
|
||||
& ( !done:? (?.true) (?.true)
|
||||
& true
|
||||
| false
|
||||
)
|
||||
)
|
||||
)
|
||||
( (11."Exactly 1 of statements 7, 8 and 9 are true.")
|
||||
. 11
|
||||
. (
|
||||
= done toDo
|
||||
. !arg:(?done.?toDo)
|
||||
& ( number
|
||||
$ ( !done
|
||||
. (=7|8|9)
|
||||
. true
|
||||
)
|
||||
: 1
|
||||
& true
|
||||
| false
|
||||
)
|
||||
)
|
||||
)
|
||||
( (12."Exactly 4 of the preceding statements are true.")
|
||||
. 12
|
||||
. (
|
||||
= done toDo preceding
|
||||
. !arg:(?done.?toDo)
|
||||
& !done:?preceding (?.?)
|
||||
& ( number$(!preceding.(=?).true):4
|
||||
& true
|
||||
| false
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
& ( TestTruth
|
||||
= done toDo postponedTests testToBePostponed
|
||||
, n when test FT oldFT A Z text
|
||||
, postponedTest testNow
|
||||
. !arg:(?done.?toDo.?postponedTests)
|
||||
& ( !toDo:
|
||||
& "We have come to the end of the list of tests.
|
||||
Perform any tests that had to be postponed until now."
|
||||
& whl
|
||||
' ( !postponedTests
|
||||
: (?.?oldFT.(=?postponedTest)) ?A
|
||||
& postponedTest$(!done.):!oldFT
|
||||
& !A:?postponedTests
|
||||
)
|
||||
& !postponedTests:
|
||||
& out$("Solution:" !done)
|
||||
& ~
|
||||
| !toDo
|
||||
: ((?n.?text).?when.(=?test)) ?toDo
|
||||
& "'false' and 'true' are just two symbols, not 'boolean values'.
|
||||
You can choose other symbols if you like.
|
||||
The program first guesses the first symbol and assigns it to the variable FT.
|
||||
After backtracking, the second symbol is guessed and assigned to FT.
|
||||
This is done for each statement."
|
||||
& false true
|
||||
: ?
|
||||
%@?FT
|
||||
( ?
|
||||
& 1+!guesses:?guesses
|
||||
& (!n.!FT):?testNow
|
||||
& "Do all tests that had to be postponed until now, unless one of those tests
|
||||
fails. Remove the successful tests from the list of postponed tests."
|
||||
& whl
|
||||
' ( !postponedTests
|
||||
: ?A
|
||||
(!n.?oldFT.(=?postponedTest))
|
||||
?Z
|
||||
& postponedTest$(!done !testNow.!toDo)
|
||||
: !oldFT
|
||||
& !A !Z:?postponedTests
|
||||
)
|
||||
& "Check that all tests that had to be postponed until now are removed from
|
||||
the list of postponed tests. Only then go on with looking at testing
|
||||
the current statement. Backtrack if a test failed."
|
||||
& !postponedTests:~(? (!n.?) ?)
|
||||
& ( !when:>!n
|
||||
& "The current statement cannot be tested right now. Postpone it to
|
||||
the earliest coming statement where the current statement can be
|
||||
tested.
|
||||
(The earliest statement, denoted by 'when', is computed manually.)"
|
||||
& (!when.!FT.'$test):?testToBePostponed
|
||||
| "No need to postpone. Test the current statement now."
|
||||
& :?testToBePostponed
|
||||
& "If the test fails, backtrack. If it succeeds, go on to the next
|
||||
statement."
|
||||
& test$(!done !testNow.!toDo):!FT
|
||||
)
|
||||
& "So far so good. Test the next statements. (recursively)"
|
||||
& TestTruth
|
||||
$ ( !done !testNow
|
||||
. !toDo
|
||||
. !testToBePostponed !postponedTests
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
& 0:?guesses
|
||||
& TestTruth$(.!STATEMENTS.)
|
||||
| out
|
||||
$ ( str
|
||||
$ ( "That's it. I made "
|
||||
!guesses
|
||||
" true/false guesses in all. (A brute force method needs 2^12="
|
||||
2^12
|
||||
" guesses."
|
||||
)
|
||||
)
|
||||
);
|
||||
43
Task/Twelve-statements/Forth/twelve-statements.fth
Normal file
43
Task/Twelve-statements/Forth/twelve-statements.fth
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
: lastbit ( n1 -- n2)
|
||||
dup if 1 swap begin dup 1 <> while swap 1+ swap 1 rshift repeat drop then
|
||||
;
|
||||
|
||||
: bit 1 swap lshift and 0<> ; ( n1 n2 -- f)
|
||||
: bitcount 0 swap begin dup while dup 1- and swap 1+ swap repeat drop ;
|
||||
|
||||
12 constant #stat \ number of statements
|
||||
\ encoding of the statements
|
||||
: s1 >r #stat 12 = r> 0 bit = ; \ heavy use of binary
|
||||
: s2 >r r@ 4032 and bitcount 3 = r> 1 bit = ;
|
||||
: s3 >r r@ 2730 and bitcount 2 = r> 2 bit = ;
|
||||
: s4 >r r@ 4 bit 0= 96 r@ over and = or r> 3 bit = ;
|
||||
: s5 >r r@ 14 and 0= r> 4 bit = ;
|
||||
: s6 >r r@ 1365 and bitcount 4 = r> 5 bit = ;
|
||||
: s7 >r r@ 1 bit r@ 2 bit xor r> 6 bit = ;
|
||||
: s8 >r r@ 6 bit 0= 48 r@ over and = or r> 7 bit = ;
|
||||
: s9 >r r@ 63 and bitcount 3 = r> 8 bit = ;
|
||||
: s10 >r 3072 r@ over and = r> 9 bit = ;
|
||||
: s11 >r r@ 448 and bitcount 1 = r> 10 bit = ;
|
||||
: s12 >r r@ 2047 and bitcount 4 = r> 11 bit = ;
|
||||
: list #stat 0 do dup i bit if i 1+ . then loop drop ;
|
||||
|
||||
: nearmiss? \ do we have a near miss?
|
||||
over #stat 1- = if ( true-pattern #true stat-pattern)
|
||||
." Near miss with statements " dup list ." true (failed "
|
||||
>r over invert 1 #stat lshift 1- and lastbit 0 .r ." )" cr r>
|
||||
then \ extract the failed statement
|
||||
;
|
||||
\ have we found a solution?
|
||||
: solution? ( true-pattern #true stat-pattern)
|
||||
over #stat = if ." Solution! with statements " dup list ." true." cr then
|
||||
;
|
||||
|
||||
: 12statements \ test the twelve patterns
|
||||
1 #stat lshift 0 do \ create another bit pattern
|
||||
i s12 2* i s11 + 2* i s10 + 2* i s9 + 2* i s8 + 2* i s7 + 2*
|
||||
i s6 + 2* i s5 + 2* i s4 + 2* i s3 + 2* i s2 + 2* i s1 +
|
||||
abs dup bitcount i solution? nearmiss? drop drop drop
|
||||
loop \ count number of bytes and evaluate
|
||||
;
|
||||
|
||||
12statements
|
||||
|
|
@ -5,20 +5,20 @@ tf = mapM (\_ -> [1,0])
|
|||
wrongness b = findIndices id . zipWith (/=) b . map (fromEnum . ($ b))
|
||||
|
||||
statements = [ (==12) . length,
|
||||
3 ? [length statements-6..],
|
||||
2 ? [1,3..],
|
||||
4 ? [4..6],
|
||||
0 ? [1..3],
|
||||
4 ? [0,2..],
|
||||
1 ? [1,2],
|
||||
6 ? [4..6],
|
||||
3 ? [0..5],
|
||||
2 ? [10,11],
|
||||
1 ? [6,7,8],
|
||||
4 ? [0..10]
|
||||
3 ⊂ [length statements-6..],
|
||||
2 ⊂ [1,3..],
|
||||
4 → [4..6],
|
||||
0 ⊂ [1..3],
|
||||
4 ⊂ [0,2..],
|
||||
1 ⊂ [1,2],
|
||||
6 → [4..6],
|
||||
3 ⊂ [0..5],
|
||||
2 ⊂ [10,11],
|
||||
1 ⊂ [6,7,8],
|
||||
4 ⊂ [0..10]
|
||||
] where
|
||||
(s ? x) b = s == (sum . map (b!!) . takeWhile (< length b)) x
|
||||
(a ? x) b = (b!!a == 0) || all ((==1).(b!!)) x
|
||||
(s ⊂ x) b = s == (sum . map (b!!) . takeWhile (< length b)) x
|
||||
(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]
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
(#~0=#@{::~&_1"1) testall S
|
||||
+------------------------+
|
||||
¦1 0 1 1 0 1 1 0 0 0 1 0¦¦
|
||||
+------------------------+
|
||||
┌───────────────────────┬┐
|
||||
│1 0 1 1 0 1 1 0 0 0 1 0││
|
||||
└───────────────────────┴┘
|
||||
|
|
|
|||
|
|
@ -1,34 +1,34 @@
|
|||
(#~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 ¦
|
||||
+--------------------------+
|
||||
┌───────────────────────┬──┐
|
||||
│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 │
|
||||
└───────────────────────┴──┘
|
||||
|
|
|
|||
28
Task/Twelve-statements/Perl/twelve-statements.pl
Normal file
28
Task/Twelve-statements/Perl/twelve-statements.pl
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
use List::Util 'sum';
|
||||
|
||||
my @condition = (
|
||||
sub { 0 }, # dummy sub for index 0
|
||||
sub { 13==@_ },
|
||||
sub { 3==sum @_[7..12] },
|
||||
sub { 2==sum @_[2,4,6,8,10,12] },
|
||||
sub { $_[5] ? ($_[6] and $_[7]) : 1 },
|
||||
sub { !$_[2] and !$_[3] and !$_[4] },
|
||||
sub { 4==sum @_[1,3,5,7,9,11] },
|
||||
sub { $_[2]==1-$_[3] },
|
||||
sub { $_[7] ? ($_[5] and $_[6]) : 1 },
|
||||
sub { 3==sum @_[1..6] },
|
||||
sub { 2==sum @_[11..12] },
|
||||
sub { 1==sum @_[7,8,9] },
|
||||
sub { 4==sum @_[1..11] },
|
||||
);
|
||||
|
||||
sub miss {
|
||||
return grep { $condition[$_]->(@_) != $_[$_] } 1..12;
|
||||
}
|
||||
|
||||
for (0..2**12-1) {
|
||||
my @truth = split //, sprintf "0%012b", $_;
|
||||
my @no = miss @truth;
|
||||
print "Solution: true statements are ", join( " ", grep { $truth[$_] } 1..12), "\n" if 0 == @no;
|
||||
print "1 miss (",$no[0],"): true statements are ", join( " ", grep { $truth[$_] } 1..12), "\n" if 1 == @no;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue