Task: Twelve statements





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.");
    }
}







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;