Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,96 @@
begin
% we have 12 statements to determine the truth/falsehood of (see task) %
logical array stmt, expected( 1 :: 12 );
% logical (boolean) to integer utility procedure %
integer procedure toInteger ( logical value v ) ; if v then 1 else 0;
% procedure to determine whether the statements are true or not %
procedure findExpectedValues ;
begin
expected( 1 ) := true;
expected( 2 ) := 3 = ( toInteger( stmt( 7 ) ) + toInteger( stmt( 8 ) )
+ toInteger( stmt( 9 ) ) + toInteger( stmt( 10 ) )
+ toInteger( stmt( 11 ) ) + toInteger( stmt( 12 ) )
);
expected( 3 ) := 2 = ( toInteger( stmt( 2 ) ) + toInteger( stmt( 4 ) )
+ toInteger( stmt( 6 ) ) + toInteger( stmt( 8 ) )
+ toInteger( stmt( 10 ) ) + toInteger( stmt( 12 ) )
);
expected( 4 ) := ( not stmt( 5 ) ) or ( stmt( 6 ) and stmt( 7 ) );
expected( 5 ) := not ( stmt( 2 ) or stmt( 3 ) or stmt( 4 ) );
expected( 6 ) := 4 = ( toInteger( stmt( 1 ) ) + toInteger( stmt( 3 ) )
+ toInteger( stmt( 5 ) ) + toInteger( stmt( 7 ) )
+ toInteger( stmt( 9 ) ) + toInteger( stmt( 11 ) )
);
expected( 7 ) := stmt( 2 ) not = stmt( 3 );
expected( 8 ) := ( not stmt( 7 ) ) or ( stmt( 5 ) and stmt( 6 ) );
expected( 9 ) := 3 = ( toInteger( stmt( 1 ) ) + toInteger( stmt( 2 ) )
+ toInteger( stmt( 3 ) ) + toInteger( stmt( 4 ) )
+ toInteger( stmt( 5 ) ) + toInteger( stmt( 6 ) )
);
expected( 10 ) := stmt( 11 ) and stmt( 12 );
expected( 11 ) := 1 = ( toInteger( stmt( 7 ) )
+ toInteger( stmt( 8 ) )
+ toInteger( stmt( 9 ) )
);
expected( 12 ) := 4 = ( toInteger( stmt( 1 ) ) + toInteger( stmt( 2 ) )
+ toInteger( stmt( 3 ) ) + toInteger( stmt( 4 ) )
+ toInteger( stmt( 5 ) ) + toInteger( stmt( 6 ) )
+ toInteger( stmt( 7 ) ) + toInteger( stmt( 8 ) )
+ toInteger( stmt( 9 ) ) + toInteger( stmt( 10 ) )
+ toInteger( stmt( 11 ) )
);
end expected ;
% clearly, statement 1 is true, however to enumerate the near %
% solutions, we need to consider "solutions" where statement 1 is false %
% we iterate through the possibilities for the statements, %
% looking for a non-contradictory set of values %
% we print the solutions with allowedContradictions contradictions %
procedure printSolutions ( integer value allowedContradictions
; string(60) value heading
) ;
begin
logical array wrong( 1 :: 12 );
write( heading );
write( " 1 2 3 4 5 6 7 8 9 10 11 12" );
write( " ====================================" );
% there are 12 statements, so we have 2^12 possible combinations %
for solution := 1 until 4096 do begin
integer n, incorrect;
% convert the number to the set of true/false values %
n := solution;
for dPos := 1 until 12 do begin
stmt( dPos ) := odd( n );
n := n div 2;
end for_dPos ;
% get the expected values of the statements, based on the %
% suggested values %
findExpectedValues;
% count the contradictions, if we have the required number, %
% print the solution %
incorrect := 0;
for dPos := 1 until 12 do begin
wrong( dPos ) := expected( dPos ) not = stmt( dPos );
incorrect := incorrect + toInteger( wrong( dPos ) );
end for_dPos ;
if incorrect = allowedContradictions then begin
% have a solution %
write( " " );
for s := 1 until 12 do writeon( s_w := 0
, " "
, if stmt( s ) then "T" else "-"
, if wrong( s ) then "*" else " "
);
end ;
end for_solution ;
end printSolutions ;
% find complete solutions %
printSolutions( 0, "Solutions" );
% find near solutions %
printSolutions( 1, "Near solutions (incorrect values marked ""*"")" );
end.

View file

@ -0,0 +1,179 @@
class
APPLICATION
create
make
feature
make
-- Possible solutions.
do
create s.make_filled (False, 1, 12)
s [1] := True
recurseAll (2)
io.put_string (counter.out + " solution found. ")
end
feature {NONE}
s: ARRAY [BOOLEAN]
check2: BOOLEAN
-- Is statement 2 fulfilled?
local
count: INTEGER
do
across
7 |..| 12 as c
loop
if s [c.item] then
count := count + 1
end
end
Result := s [2] = (count = 3)
end
check3: BOOLEAN
-- Is statement 3 fulfilled?
local
count, i: INTEGER
do
from
i := 2
until
i > 12
loop
if s [i] then
count := count + 1
end
i := i + 2
end
Result := s [3] = (count = 2)
end
check4: BOOLEAN
-- Is statement 4 fulfilled?
do
Result := s [4] = ((not s [5]) or (s [6] and s [7]))
end
check5: BOOLEAN
-- Is statement 5 fulfilled?
do
Result := s [5] = ((not s [2]) and (not s [3]) and (not s [4]))
end
check6: BOOLEAN
-- Is statement 6 fulfilled?
local
count, i: INTEGER
do
from
i := 1
until
i > 11
loop
if s [i] then
count := count + 1
end
i := i + 2
end
Result := s [6] = (count = 4)
end
check7: BOOLEAN
-- Is statement 7 fulfilled?
do
Result := s [7] = ((s [2] or s [3]) and not (s [2] and s [3]))
end
check8: BOOLEAN
-- Is statement 8 fulfilled?
do
Result := s [8] = (not s [7] or (s [5] and s [6]))
end
check9: BOOLEAN
-- Is statement 9 fulfilled?
local
count: INTEGER
do
across
1 |..| 6 as c
loop
if s [c.item] then
count := count + 1
end
end
Result := s [9] = (count = 3)
end
check10: BOOLEAN
-- Is statement 10 fulfilled?
do
Result := s [10] = (s [11] and s [12])
end
check11: BOOLEAN
-- Is statement 11 fulfilled?
local
count: INTEGER
do
across
7 |..| 9 as c
loop
if s [c.item] then
count := count + 1
end
end
Result := s [11] = (count = 1)
end
check12: BOOLEAN
-- Is statement 12 fulfilled?
local
count: INTEGER
do
across
1 |..| 11 as c
loop
if s [c.item] then
count := count + 1
end
end
Result := (s [12] = (count = 4))
end
counter: INTEGER
checkit
-- Check if all statements are correctly solved.
do
if check2 and check3 and check4 and check5 and check6 and check7 and check8 and check9 and check10 and check11 and check12 then
across
1 |..| 12 as c
loop
if s [c.item] then
io.put_string (c.item.out + "%T")
end
end
io.new_line
counter := counter + 1
end
end
recurseAll (k: INTEGER)
-- All possible True and False combinations to check for a solution.
do
if k = 13 then
checkit
else
s [k] := False
recurseAll (k + 1)
s [k] := True
recurseAll (k + 1)
end
end
end

View file

@ -0,0 +1,18 @@
offby1=: 1=+/errors
'Statement ',"1 (":1+I.|: offby1 #"1 errors),"1 ' is inconsistent with exactly ',"1 ((1":@:+I.)"1 #:I.offby1),"1 ' being true'
Statement 1 is inconsistent with exactly 5 8 11 being true
Statement 1 is inconsistent with exactly 5 8 10 11 12 being true
Statement 1 is inconsistent with exactly 4 8 10 11 12 being true
Statement 8 is inconsistent with exactly 1 5 being true
Statement 11 is inconsistent with exactly 1 5 8 being true
Statement 12 is inconsistent with exactly 1 5 8 11 being true
Statement 12 is inconsistent with exactly 1 5 8 10 11 12 being true
Statement 8 is inconsistent with exactly 1 5 6 9 11 being true
Statement 8 is inconsistent with exactly 1 4 being true
Statement 12 is inconsistent with exactly 1 4 8 10 11 12 being true
Statement 6 is inconsistent with exactly 1 4 6 8 9 being true
Statement 7 is inconsistent with exactly 1 3 4 8 9 being true
Statement 9 is inconsistent with exactly 1 3 4 6 7 9 being true
Statement 12 is inconsistent with exactly 1 2 4 7 9 12 being true
Statement 10 is inconsistent with exactly 1 2 4 7 9 10 being true
Statement 8 is inconsistent with exactly 1 2 4 7 8 9 being true

View file

@ -1,16 +1,16 @@
S=: <;._2 (0 :0)
12&=@#
3=+/@:{.~&_6
2= +/@:{~&1 3 5 7 9 11
4&{=*./@:{~&4 5 6
0=+/@:{~&1 2 3
4=+/@:{~&0 2 4 6 8 10
1=+/@:{~&1 2
6&{=*./@:{~&4 5 6
3=+/@:{.~&6
2=+/@:{~&10 11
1=+/@:{~&6 7 8
4=+/@:{.~&11
12&=@# NB. 1. This is a numbered list of twelve statements.
3=+/@:{.~&_6 NB. 2. Exactly 3 of the last 6 statements are true.
2= +/@:{~&1 3 5 7 9 11 NB. 3. Exactly 2 of the even-numbered statements are true.
4&{=*./@:{~&4 5 6 NB. 4. If statement 5 is true, then statements 6 and 7 are both true.
0=+/@:{~&1 2 3 NB. 5. The 3 preceding statements are all false.
4=+/@:{~&0 2 4 6 8 10 NB. 6. Exactly 4 of the odd-numbered statements are true.
1=+/@:{~&1 2 NB. 7. Either statement 2 or 3 is true, but not both.
6&{=*./@:{~&4 5 6 NB. 8. If statement 7 is true, then 5 and 6 are both true.
3=+/@:{.~&6 NB. 9. Exactly 3 of the first 6 statements are true.
2=+/@:{~&10 11 NB. 10. The next two statements are both true.
1=+/@:{~&6 7 8 NB. 11. Exactly 1 of statements 7, 8 and 9 are true.
4=+/@:{.~&11 NB. 12. Exactly 4 of the preceding statements are true.
)
testall=: (];"1 0<@I.@:(]~:(apply&><))"1) #:@i.@(2&^)@#

View file

@ -1,34 +1,2 @@
(#~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 │
└───────────────────────┴──┘
1+I.;(#~0=#@{::~&_1"1) testall S
1 3 4 6 7 11

View file

@ -1,2 +1,34 @@
(-N)&{. #: S <:@]^:((]-.@-:(apply&><)"1) (-N)&{.@#:@])^:(_) 2^N=.#S
1 0 1 1 0 1 1 0 0 0 1 0
(#~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 │
└───────────────────────┴──┘

View file

@ -0,0 +1,2 @@
(-N)&{. #: S <:@]^:((]-.@-:(apply&><)"1) (-N)&{.@#:@])^:(_) 2^N=.#S
1 0 1 1 0 1 1 0 0 0 1 0

View file

@ -0,0 +1,16 @@
true=:1 :'(m-1)&{'
S=: <;._2 (0 :0)
12 = # NB. 1. This is a numbered list of twelve statements.
3 (= +/) _6&{. NB. 2. Exactly 3 of the last 6 statements are true.
2 (= +/) (12$0 1)&# NB. 3. Exactly 2 of the even-numbered statements are true.
5 true (<: */) 6 7 true NB. 4. If statement 5 is true, then statements 6 and 7 are both true.
0 (= +/) 2 3 4 true NB. 5. The 3 preceding statements are all false.
4 (= +/) (12$1 0)&# NB. 6. Exactly 4 of the odd-numbered statements are true.
1 (= +/) 2 3 true NB. 7. Either statement 2 or 3 is true, but not both.
7 true (<: */) 5 6 true NB. 8. If statement 7 is true, then 5 and 6 are both true.
3 (= +/) 6&{. NB. 9. Exactly 3 of the first 6 statements are true.
*/@(11 12 true) NB. 10. The next two statements are both true.
1 (= +/) 7 8 9 true NB. 11. Exactly 1 of statements 7, 8 and 9 are true.
4 (= +/) }: NB. 12. Exactly 4 of the preceding statements are true.
)

View file

@ -0,0 +1,17 @@
'sum not mask'=: |:".;._2(0 :0)
0; 0; 0 0 0 0 0 0 0 0 0 0 0 0 NB. 1. This is a numbered list of twelve statements.
3; 0; 0 0 0 0 0 0 1 1 1 1 1 1 NB. 2. Exactly 3 of the last 6 statements are true.
2; 0; 0 1 0 1 0 1 0 1 0 1 0 1 NB. 3. Exactly 2 of the even-numbered statements are true.
2; 5; 0 0 0 0 0 1 1 0 0 0 0 0 NB. 4. If statement 5 is true, then statements 6 and 7 are both true.
0; 0; 0 1 1 1 0 0 0 0 0 0 0 0 NB. 5. The 3 preceding statements are all false.
4; 0; 1 0 1 0 1 0 1 0 1 0 1 0 NB. 6. Exactly 4 of the odd-numbered statements are true.
1; 0; 0 1 1 0 0 0 0 0 0 0 0 0 NB. 7. Either statement 2 or 3 is true, but not both.
2; 7; 0 0 0 0 1 1 0 0 0 0 0 0 NB. 8. If statement 7 is true, then 5 and 6 are both true.
3; 0; 1 1 1 1 1 1 0 0 0 0 0 0 NB. 9. Exactly 3 of the first 6 statements are true.
2; 0; 0 0 0 0 0 0 0 0 0 0 1 1 NB. 10. The next two statements are both true.
1; 0; 0 0 0 0 0 0 1 1 1 0 0 0 NB. 11. Exactly 1 of statements 7, 8 and 9 are true.
4; 0; 1 1 1 1 1 1 1 1 1 1 1 0 NB. 12. Exactly 4 of the preceding statements are true.
)
propositions=: |:#:i.2^#sum
errors=: propositions~:(1 - not { 1,propositions) >. sum = mask +/ .*propositions

View file

@ -0,0 +1,4 @@
#:I.0=+/errors
1 0 1 1 0 1 1 0 0 0 1 0
1+I.#:I.0=+/errors NB. true propositions for the consistent case
1 3 4 6 7 11

View file

@ -0,0 +1,54 @@
function showflaggedbits{T<:BitArray{1}}(a::T, f::T)
tf = map(x->x ? "T" : "F", a)
flg = map(x->x ? "*" : " ", f)
join(tf .* flg, " ")
end
const props = [s -> length(s) == 12,
s -> sum(s[7:12]) == 3,
s -> sum(s[2:2:end]) == 2,
s -> !s[5] || (s[6] & s[7]),
s -> !any(s[2:4]),
s -> sum(s[1:2:end]) == 4,
s -> s[2] $ s[3],
s -> !s[7] || (s[5] & s[6]),
s -> sum(s[1:6]) == 3,
s -> s[11] & s[12],
s -> sum(s[7:9]) == 1,
s -> sum(s[1:end-1]) == 4]
const NDIG = length(props)
NDIG < WORD_SIZE || println("WARNING, too many propositions!")
mhist = zeros(Int, NDIG+1)
println("Checking the ", NDIG, " statements against all possibilities.\n")
print(" "^15)
for i in 1:NDIG
print(@sprintf "%3d" i)
end
println()
for i in 0:(2^NDIG-1)
s = bitpack(digits(i, 2, NDIG))
t = bitpack([p(s) for p in props])
misses = s$t
mcnt = sum(misses)
mhist[NDIG-mcnt+1] += 1
mcnt < 2 || mcnt == NDIG || continue
if mcnt == 0
print(" Exact Match: ")
elseif mcnt == NDIG
print(" Total Miss: ")
else
print(" Near Miss: ")
end
println(showflaggedbits(t, misses))
end
println()
println("Distribution of matches")
println(" Matches Cases")
for i in (NDIG+1):-1:1
println(@sprintf " %2d => %4d" i-1 mhist[i])
end

View file

@ -20,7 +20,7 @@ my @ugly;
for reverse 0 ..^ 2**12 -> $i {
my @b = $i.fmt("%012b").comb;
my @assert = True, @b.map: { .so }
my @assert = True, | @b.map: { 1 == $_ }
my @result = @tests.map: { .(@assert).so }
my @s = ( $_ if $_ and @assert[$_] for 1..12 );
if @result eqv @assert {