Task: Twelve statements







Python: from itertools import product


constraintinfo = (  
  (lambda st: len(st) == 12                 ,(1, 'This is a numbered list of twelve statements')),
  (lambda st: sum(st[-6:]) == 3             ,(2, 'Exactly 3 of the last 6 statements are true')),
  (lambda st: sum(st[1::2]) == 2            ,(3, 'Exactly 2 of the even-numbered statements are true')),
  (lambda st: (st[5]&st[6]) if st[4] else 1 ,(4, 'If statement 5 is true, then statements 6 and 7 are both true')),
  (lambda st: sum(st[1:4]) == 0             ,(5, 'The 3 preceding statements are all false')),
  (lambda st: sum(st[0::2]) == 4            ,(6, 'Exactly 4 of the odd-numbered statements are true')),
  (lambda st: sum(st[1:3]) == 1             ,(7, 'Either statement 2 or 3 is true, but not both')),
  (lambda st: (st[4]&st[5]) if st[6] else 1 ,(8, 'If statement 7 is true, then 5 and 6 are both true')),
  (lambda st: sum(st[:6]) == 3              ,(9, 'Exactly 3 of the first 6 statements are true')),
  (lambda st: (st[10]&st[11])               ,(10, 'The next two statements are both true')),
  (lambda st: sum(st[6:9]) == 1             ,(11, 'Exactly 1 of statements 7, 8 and 9 are true')),
  (lambda st: sum(st[0:11]) == 4            ,(12, 'Exactly 4 of the preceding statements are true')),
)  

def printer(st, matches):
    if False in matches:
        print('Missed by one statement: %i, %s' % docs[matches.index(False)])
    else:
        print('Full match:')
    print('  ' + ', '.join('%i:%s' % (i, 'T' if t else 'F') for i, t in enumerate(st, 1)))

funcs, docs = zip(*constraintinfo)

full, partial = [], []

for st in product( *([(False, True)] * 12) ):
    truths = [bool(func(st)) for func in funcs]
    matches = [s == t for s,t in zip(st, truths)]
    mcount = sum(matches)
    if mcount == 12:
        full.append((st, matches))
    elif mcount == 11:
        partial.append((st, matches))

for stm in full + partial:
    printer(*stm)





Ada: with Ada.Text_IO, Logic; 

procedure Twelve_Statements is
   
   package L is new Logic(Number_Of_Statements => 12); use L;
   
   
   function P01(T: Table) return Boolean is (T'Length = 12);              
   function P02(T: Table) return Boolean is (Sum(T(7 .. 12)) = 3);        
   function P03(T: Table) return Boolean is (Sum(Half(T, Even)) = 2);     
   function P04(T: Table) return Boolean is (if T(5) then T(6) and T(7)); 
   function P05(T: Table) return Boolean is 
      ( (not T(2)) and (not T(3)) and (not T(4)) );                       
   function P06(T: Table) return Boolean is (Sum(Half(T, Odd)) = 4);      
   function P07(T: Table) return Boolean is (T(2) xor T(3));              
   function P08(T: Table) return Boolean is (if T(7) then T(5) and T(6)); 
   function P09(T: Table) return Boolean is (Sum(T(1 .. 6)) = 3);         
   function P10(T: Table) return Boolean is (T(11) and T(12));            
   function P11(T: Table) return Boolean is (Sum(T(7..9)) = 1);           
   function P12(T: Table) return Boolean is (Sum(T(1 .. 11)) = 4);        
   
   
   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);
   
   
   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 
	 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;