Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,79 @@
with Ada.Text_IO, Logic;
procedure Twelve_Statements is
package L is new Logic(Number_Of_Statements => 12); use L;
-- formally define the 12 statements as expression function predicates
function P01(T: Table) return Boolean is (T'Length = 12); -- list of 12 statements
function P02(T: Table) return Boolean is (Sum(T(7 .. 12)) = 3); -- three of last six
function P03(T: Table) return Boolean is (Sum(Half(T, Even)) = 2); -- two of the even
function P04(T: Table) return Boolean is (if T(5) then T(6) and T(7)); -- if 5 is true, then ...
function P05(T: Table) return Boolean is
( (not T(2)) and (not T(3)) and (not T(4)) ); -- none of preceding three
function P06(T: Table) return Boolean is (Sum(Half(T, Odd)) = 4); -- four of the odd
function P07(T: Table) return Boolean is (T(2) xor T(3)); -- either 2 or 3, not both
function P08(T: Table) return Boolean is (if T(7) then T(5) and T(6)); -- if 7 is true, then ...
function P09(T: Table) return Boolean is (Sum(T(1 .. 6)) = 3); -- three of first six
function P10(T: Table) return Boolean is (T(11) and T(12)); -- next two
function P11(T: Table) return Boolean is (Sum(T(7..9)) = 1); -- one of 7, 8, 9
function P12(T: Table) return Boolean is (Sum(T(1 .. 11)) = 4); -- four of the preding
-- define a global list of statements
Statement_List: constant Statements :=
(P01'Access, P02'Access, P03'Access, P04'Access, P05'Access, P06'Access,
P07'Access, P08'Access, P09'Access, P10'Access, P11'Access, P12'Access);
-- try out all 2^12 possible choices for the table
procedure Try(T: Table; Fail: Natural; Idx: Indices'Base := Indices'First) is
procedure Print_Table(T: Table) is
use Ada.Text_IO;
begin
Put(" ");
if Fail > 0 then
Put("(wrong at");
for J in T'Range loop
if Statement_List(J)(T) /= T(J) then
Put(Integer'Image(J) & (if J < 10 then ") " else ") "));
end if;
end loop;
end if;
if T = (1..12 => False) then
Put_Line("All false!");
else
Put("True are");
for J in T'Range loop
if T(J) then
Put(Integer'Image(J));
end if;
end loop;
New_Line;
end if;
end Print_Table;
Wrong_Entries: Natural := 0;
begin
if Idx <= T'Last then
Try(T(T'First .. Idx-1) & False & T(Idx+1 .. T'Last), Fail, Idx+1);
Try(T(T'First .. Idx-1) & True & T(Idx+1 .. T'Last), Fail, Idx+1);
else -- now Index > T'Last and we have one of the 2^12 choices to test
for J in T'Range loop
if Statement_List(J)(T) /= T(J) then
Wrong_Entries := Wrong_Entries + 1;
end if;
end loop;
if Wrong_Entries = Fail then
Print_Table(T);
end if;
end if;
end Try;
begin
Ada.Text_IO.Put_Line("Exact hits:");
Try(T => (1..12 => False), Fail => 0);
Ada.Text_IO.New_Line;
Ada.Text_IO.Put_Line("Near Misses:");
Try(T => (1..12 => False), Fail => 1);
end Twelve_Statements;

View file

@ -0,0 +1,16 @@
generic
Number_Of_Statements: Positive;
package Logic is
--types
subtype Indices is Natural range 1 .. Number_Of_Statements;
type Table is array(Indices range <>) of Boolean;
type Predicate is access function(T: Table) return Boolean;
type Statements is array(Indices) of Predicate;
type Even_Odd is (Even, Odd);
-- convenience functions
function Sum(T: Table) return Natural;
function Half(T: Table; Which: Even_Odd) return Table;
end Logic;

View file

@ -0,0 +1,27 @@
package body Logic is
function Sum(T: Table) return Natural is
Result: Natural := 0;
begin
for I in T'Range loop
if T(I) then
Result := Result + 1;
end if;
end loop;
return Result;
end Sum;
function Half(T: Table; Which: Even_Odd) return Table is
Result: Table(T'Range);
Last: Natural := Result'First - 1;
begin
for I in T'Range loop
if I mod 2 = (if (Which=Odd) then 1 else 0) then
Last := Last+1;
Result(Last) := T(I);
end if;
end loop;
return Result(Result'First .. Last);
end Half;
end Logic;

View file

@ -0,0 +1,165 @@
module statement_checker
implicit none
private
public :: check_statements
contains
subroutine check_statements(truth, is_valid, failed_count, failed_indices)
logical, intent(in) :: truth(12)
logical, intent(out) :: is_valid
integer, intent(out) :: failed_count
integer, intent(out) :: failed_indices(12)
logical :: conditions(12)
integer :: i, true_count, last_six_count, even_count, odd_count, first_six_count
integer :: true_789_count
! Initialize
failed_count = 0
failed_indices = 0
conditions = .false.
! Statement 1: This is a numbered list of twelve statements (always true)
conditions(1) = .true.
! Statement 2: Exactly 3 of the last 6 statements (7 to 12) are true
last_six_count = count(truth(7:12))
conditions(2) = (last_six_count == 3)
! Statement 3: Exactly 2 of the even-numbered statements (2,4,6,8,10,12) are true
even_count = count(truth([2, 4, 6, 8, 10, 12]))
conditions(3) = (even_count == 2)
! Statement 4: If statement 5 is true, then statements 6 and 7 are both true
conditions(4) = (.not.truth(5)) .or. (truth(6) .and. truth(7))
! Statement 5: The 3 preceding statements (2,3,4) are all false
conditions(5) = (.not.truth(2)) .and. (.not.truth(3)) .and. (.not.truth(4))
! Statement 6: Exactly 4 of the odd-numbered statements (1,3,5,7,9,11) are true
odd_count = count(truth([1, 3, 5, 7, 9, 11]))
conditions(6) = (odd_count == 4)
! Statement 7: Either statement 2 or 3 is true, but not both
conditions(7) = (truth(2) .neqv. truth(3))
! Statement 8: If statement 7 is true, then 5 and 6 are both true
conditions(8) = (.not.truth(7)) .or. (truth(5) .and. truth(6))
! Statement 9: Exactly 3 of the first 6 statements are true
first_six_count = count(truth(1:6))
conditions(9) = (first_six_count == 3)
! Statement 10: The next two statements (11,12) are both true
conditions(10) = truth(11) .and. truth(12)
! Statement 11: Exactly 1 of statements 7, 8, and 9 are true
true_789_count = count(truth(7:9))
conditions(11) = (true_789_count == 1)
! Statement 12: Exactly 4 of the preceding statements (1 to 11) are true
true_count = count(truth(1:11))
conditions(12) = (true_count == 4)
! Check if solution is valid (all conditions match truth values)
is_valid = all(truth .eqv. conditions)
! Count failed statements and record their indices
do i = 1, 12
if (truth(i) .neqv. conditions(i)) then
failed_count = failed_count + 1
failed_indices(failed_count) = i
end if
end do
end subroutine check_statements
end module statement_checker
program solve_statements
use statement_checker
implicit none
logical :: truth(12)
integer :: i, j, combo, failed_count, failed_indices(12), k
logical :: is_valid
integer :: valid_solutions(0:4095, 12)
integer :: near_misses(0:4095, 13)
integer :: valid_count, near_miss_count
character(len=100) :: truth_str
character(len=2) :: holder
valid_count = 0
near_miss_count = 0
valid_solutions = 0
near_misses = 0
! Iterate through all 2^12 combinations
do combo = 0, 2**12 - 1
! Convert combo to binary truth array
do i = 1, 12
truth(i) = btest(combo, i - 1)
end do
! Check statements
call check_statements(truth, is_valid, failed_count, failed_indices)
! Store valid solutions
if (is_valid) then
valid_solutions(valid_count, 1:12) = merge(1, 0, truth)
valid_count = valid_count + 1
end if
! Store near-misses (exactly one statement false)
if (failed_count == 1) then
near_misses(near_miss_count, 1:12) = merge(1, 0, truth)
near_misses(near_miss_count, 13) = failed_indices(1)
near_miss_count = near_miss_count + 1
end if
end do
! Print valid solutions
write(*, '(A)') 'Exact hits:'
if (valid_count == 0) then
write(*, '(A)') ' None'
else
do i = 0, valid_count - 1
truth_str = ''
do j = 1, 12
if (valid_solutions(i, j) == 1) then
write(holder, '(i0)') j
holder = adjustl(holder)
truth_str = trim(truth_str) // ' ' // holder
end if
end do
write(*, '(A,A)') ' ', trim(truth_str)
end do
end if
! Print near-misses
write(*, '(/A)') 'Near misses:'
if (near_miss_count == 0) then
write(*, '(A)') ' None'
else
do i = 0, near_miss_count - 1
truth_str = ''
do j = 1, 12
if (near_misses(i, j) == 1) then
! WRITE(truth_str, '(A,I0,A)') TRIM(truth_str), j, '~'
write(holder, '(i0)') j
holder = adjustl(holder)
truth_str = trim(truth_str) // ' ' // holder
end if
end do
if (truth_str == '') then
truth_str = 'None'
end if
! call foobar(truth_str,len_trim(truth_str))
write(*, '(A,I0,A,T28, A)') ' (Fails at statement ', near_misses(i, 13), ') ', trim(truth_str)
end do
end if
contains
subroutine foobar(thing, n)
implicit none
integer, intent(in) :: n
character(len=1), dimension(n) :: thing
where (thing == '~') thing = ' '
return
end subroutine foobar
end program solve_statements

View file

@ -1,32 +1,30 @@
(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">s1</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">12</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">s2</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_eq</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">7</span><span style="color: #0000FF;">..</span><span style="color: #000000;">12</span><span style="color: #0000FF;">],</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">3</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">s3</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_eq</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)),</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">2</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">s4</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">or</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">6</span><span style="color: #0000FF;">..</span><span style="color: #000000;">7</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">"11"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">s5</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">"000"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">s6</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_eq</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)),</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">4</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">s7</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">s8</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">7</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">or</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">..</span><span style="color: #000000;">6</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">"11"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">s9</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_eq</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">6</span><span style="color: #0000FF;">],</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">3</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">s10</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">11</span><span style="color: #0000FF;">..</span><span style="color: #000000;">12</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">"11"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">s11</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_eq</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">7</span><span style="color: #0000FF;">..</span><span style="color: #000000;">9</span><span style="color: #0000FF;">],</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">s12</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_eq</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">11</span><span style="color: #0000FF;">],</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">4</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function s1(string s) return length(s)=12 end function
function s2(string s) return sum(sq_eq(s[7..12],'1'))=3 end function
function s3(string s) return sum(sq_eq(extract(s,tagset(12,2,2)),'1'))=2 end function
function s4(string s) return s[5]='0' or s[6..7]="11" end function
function s5(string s) return s[2..4]="000" end function
function s6(string s) return sum(sq_eq(extract(s,tagset(12,1,2)),'1'))=4 end function
function s7(string s) return s[2]!=s[3] end function
function s8(string s) return s[7]='0' or s[5..6]="11" end function
function s9(string s) return sum(sq_eq(s[1..6],'1'))=3 end function
function s10(string s) return s[11..12]="11" end function
function s11(string s) return sum(sq_eq(s[7..9],'1'))=1 end function
function s12(string s) return sum(sq_eq(s[1..11],'1'))=4 end function
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rtn</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s11</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s12</span><span style="color: #0000FF;">}</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">misses</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"\n"</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%012b"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find_all</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pass</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">fail</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">pass</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">call_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rtn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">b</span><span style="color: #0000FF;">],{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})=(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">b</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">pass</span> <span style="color: #008080;">then</span> <span style="color: #000000;">fail</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">pass</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">12</span> <span style="color: #008080;">and</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">=</span><span style="color: #000000;">12</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Solution: %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">=</span><span style="color: #000000;">11</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">misses</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Near miss: %v, fail on %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fail</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">misses</span><span style="color: #0000FF;">)</span>
<!--
sequence rtn = {s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12}
string misses = "\n"
for i=0 to power(2,12)-1 do
string s = sprintf("%012b",i)
sequence res = find_all('1',s)
integer t = 0, pass, fail
for b=1 to 12 do
pass = call_func(rtn[b],{s})=(s[b]='1')
if not pass then fail = b end if
t += pass
if b=12 and t=12 then printf(1,"Solution: %v\n",{res}) end if
end for
if t=11 then
misses &= sprintf("Near miss: %v, fail on %d\n",{res,fail})
end if
end for
puts(1,misses)

View file

@ -0,0 +1,49 @@
require "table2"
local fmt = require "fmt"
local btoi = |b| -> b ? 1 : 0
local predicates = {
|s| -> #s == 12,
|s| -> range(7, 12):count(|i| -> s[i] == "1") == 3,
|s| -> {2, 4, 6, 8, 10, 12}:count(|i| -> s[i] == "1") == 2,
|s| -> s[5] == "0" or (s[6] == "1" and s[7] == "1"),
|s| -> s[2] == "0" and s[3] == "0" and s[4] == "0",
|s| -> {1, 3, 5, 7, 9, 11}:count(|i| -> s[i] == "1") == 4,
|s| -> fmt.itob(btoi(s[2] == "1") ~ btoi(s[3] == "1")),
|s| -> s[7] == "0" or (s[5] == "1" and s[6] == "1"),
|s| -> range(1, 6):count(|i| -> s[i] == "1") == 3,
|s| -> s[11] == "1" and s[12] == "1",
|s| -> range(7, 9):count(|i| -> s[i] == "1") == 1,
|s| -> range(1, 11):count(|i| -> s[i] == "1") == 4
}
local function show(s, indent)
if indent then io.write(" ") end
for i = 1, #s do
if s[i] == "1" then io.write($"{i} ") end
end
print()
end
print("Exact hits:")
for i = 0, 4095 do
local s = fmt.lpad(fmt.bin(i), 12, "0")
local j = 1
if predicates:checkall(|pred| -> pred(s) == (s[j++] == "1")) then show(s, true) end
end
print("\nNear misses:")
for i = 0, 4095 do
local s = fmt.lpad(fmt.bin(i), 12, "0")
local j = 1
if predicates:count(|pred| -> pred(s) == (s[j++] == "1")) == 11 then
local k = 1
for predicates as pred do
if pred(s) != (s[k] == "1") then break end
k += 1
end
fmt.write(" (Fails at statement %2d) ", k)
show(s, false)
end
end

View file

@ -0,0 +1,26 @@
#Express the 12 statements programmatically
test_12 <- function(v){
c(length(v)==12,
sum(v[7:12])==3,
sum(v[2*(1:6)])==2,
ifelse(v[5], v[6]&v[7], TRUE),
!any(v[2:4]),
sum(v[2*(1:6)-1])==4,
xor(v[2], v[3]),
ifelse(v[7], v[5]&v[6], TRUE),
sum(v[1:6])==3,
all(v[11:12]),
sum(v[7:9])==1,
sum(v)==4)
}
#Find solution and near misses
for(i in 0:4095){
v <- as.logical(intToBits(i))[1:12]
if(all(v==test_12(v))){
cat(which(v), "(all correct)\n")
}
if(sum(v!=test_12(v))==1){
cat(which(v), "(statement", which(v!=test_12(v)), "wrong)\n")
}
}

View file

@ -0,0 +1,49 @@
nStatements = 12
T = list(nStatements)
Pass = list(nStatements)
for tryVal = 0 to (2**nStatements) - 1
# 1. Postulate answer: Extract bits into T array
for stmt = 1 to 12
if (tryVal & (2**(stmt-1))) != 0
T[stmt] = 1
else
T[stmt] = 0
ok
next
# 2. Test consistency (Ring: True = 1, False = 0)
Pass[1] = (T[1] = (nStatements = 12))
Pass[2] = (T[2] = (sum_range(T, 7, 12) = 3))
Pass[3] = (T[3] = ((T[2]+T[4]+T[6]+T[8]+T[10]+T[12]) = 2))
Pass[4] = (T[4] = ((not T[5]) or (T[6] and T[7])))
Pass[5] = (T[5] = ((not T[2]) and (not T[3]) and (not T[4])))
Pass[6] = (T[6] = ((T[1]+T[3]+T[5]+T[7]+T[9]+T[11]) = 4))
Pass[7] = (T[7] = (T[2] != T[3])) # XOR replacement
Pass[8] = (T[8] = ((not T[7]) or (T[5] and T[6])))
Pass[9] = (T[9] = (sum_range(T, 1, 6) = 3))
Pass[10] = (T[10] = (T[11] and T[12]))
Pass[11] = (T[11] = (sum_range(T, 7, 9) = 1))
Pass[12] = (T[12] = (sum_range(T, 1, 11) = 4))
# 3. Check if all statements pass
totalPass = 0
for p in Pass totalPass += p next
if totalPass = 12
see "Solution! True statements: "
for i = 1 to 12
if T[i] = 1 see "" + i + " " ok
next
see nl
ok
next
# Helper function to sum elements in a specific range
func sum_range lst, start, stop
val = 0
for i = start to stop
val += lst[i]
next
return val

View file

@ -0,0 +1,83 @@
\( Solve the "Twelve Statements" puzzle
See https://rosettacode.org/wiki/Twelve_statements
Emphasis is on unterstandability.
\)
main (p):+
\ uses presentation syntax
for r = give bit permutations 12
if is solution r
print 'Found,', join r
tell result r as sentences
\( Function to evaluate the setting in the row (s)
True if the new booleans in (n) are the same as in (s)
There are more tricky variants, but this is easy to debug
\)
is solution (s):
n =: new row size s.Count
n[1] =: s.Count = 12
n[2] =: in s exactly 3 of 12, 11, 10, 9, 8, 7
n[3] =: in s exactly 2 of 2, 4, 6, 8, 10, 12
n[4] =: in s if 5 then 6 and 7
n[5] =: in s exactly 0 of 4, 3, 2
n[6] =: in s exactly 4 of 1, 3, 5, 7, 9, 11
n[7] =: in s exactly 1 of 2, 3
n[8] =: in s if 7 then 5 and 6
n[9] =: in s exactly 3 of 1, 2, 3, 4, 5, 6
n[10] =: in s exactly 2 of 11, 12
n[11] =: in s exactly 1 of 7, 8, 9
n[12] =: in s exactly 4 of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
return (row n as tuple) = (row s as tuple)
\ In row (s), exactly (n) of the elements given by (list) are true
in (s) exactly (n) of (list):
?# i =: tuple list give values
? s[i] \ if true
n =- 1
:> n = 0
\ logical implication
in (s) if (a) then (b) and (c):
? s[a]
:> s[b] & s[c]
:> ?+
\( Scan function to supoly a row of (n) boolean values
Relays to a scan to give all bit patterns as integers
and converts to a row of booleans.
\)
give bit permutations (n):
v =: from 0 upto 2^n - 1 \ call scan function
? $ = () \ done ?
:> \ yes, return void
\ creation of a new row is cheap, no need to reuse one
s =: new row size n
?# i =: from 1 upto n \ check each bit
s[i] =: integer v bit is set i-1 \ boolean
:> s
\( Tell true and false sentenes
A string literal is used for better readablilty
\)
tell result (res) as sentences:
\ create a tuple of text lines by successive accumulation
ps =: " 1. This is a numbered list of twelve statements."
ps =, " 2. Exactly 3 of the last 6 statements are true."
ps =, " 3. Exactly 2 of the even-numbered statements are true."
ps =, " 4. If statement 5 is true, then statements 6 and 7 are both true."
ps =, " 5. The 3 preceding statements are all false."
ps =, " 6. Exactly 4 of the odd-numbered statements are true."
ps =, " 7. Either statement 2 or 3 is true, but not both."
ps =, " 8. If statement 7 is true, then 5 and 6 are both true."
ps =, " 9. Exactly 3 of the first 6 statements are true."
ps =, "10. The next two statements are both true."
ps =, "11. Exactly 1 of statements 7, 8 and 9 are true."
ps =, "12. Exactly 4 of the preceding statements are true."
?# i =: res::give keys
? res[i]
v =: ' true:'
|
v =: 'false;'
print v, ps[i]