RosettaCodeData/Task/Twelve-statements/XPL0/twelve-statements.xpl0
2023-07-01 13:44:08 -04:00

92 lines
4.5 KiB
Text

\We have 12 statements to determine the truth/falsehood of (see task).
integer Stmt( 1+12 ), Expected( 1+12 );
\Logical-to-integer utility procedure
function ToInteger; int V ; return if V # 0 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 ) # 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; \FindExpectedValues
\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 ( AllowedContradictions, Heading ) ;
integer AllowedContradictions, Heading;
integer Wrong( 1+12 );
integer Solution, N, Incorrect, DPos, S;
begin
Text(0, Heading ); CrLf(0);
Text(0, " 1 2 3 4 5 6 7 8 9 10 11 12^m^j" );
Text(0, " ====================================^m^j" );
\There are 12 statements, so we have 2^12 possible combinations
for Solution := 1 to 4096 do begin
\Convert the number to the set of true/false values
N := Solution;
for DPos := 1 to 12 do begin
Stmt( DPos ) := (N & 1) # 0; \very odd
N := N / 2;
end; \for_DPos
\Get the expected values of the statements based on suggested values
FindExpectedValues;
\Count contradictions. If the required number, print solution
Incorrect := 0;
for DPos := 1 to 12 do begin
Wrong( DPos ) := Expected( DPos ) # Stmt( DPos );
Incorrect := Incorrect + ToInteger( Wrong( DPos ) );
end; \for_DPos
if Incorrect = AllowedContradictions then begin
\Have a solution
Text(0, " " );
for S := 1 to 12 do begin
Text(0, " ");
Text(0, if Stmt( S ) then "T" else "-");
Text(0, if Wrong( S ) then "*" else " ");
end;
CrLf(0);
end;
end; \for_solution
end; \PrintSolutions
begin
\Find complete solutions
PrintSolutions( 0, "Solutions" );
\Find near solutions
PrintSolutions( 1, "Near solutions (incorrect values marked ^"*^")" );
end