Twelve statements: Difference between revisions

m
→‎{{header|Uiua}}: final fix :-)
m (syntax highlighting fixup automation)
m (→‎{{header|Uiua}}: final fix :-))
 
(13 intermediate revisions by 6 users not shown)
Line 392:
T - - - T - - T - T T T*
</pre>
 
=={{header|AppleScript}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="applescript">on twelveStatements()
set statements to " 1. This is a numbered list of twelve statements.
2. Exactly 3 of the last 6 statements are true.
3. Exactly 2 of the even-numbered statements are true.
4. If statement 5 is true, then statements 6 and 7 are both true.
5. The 3 preceding statements are all false.
6. Exactly 4 of the odd-numbered statements are true.
7. Either statement 2 or 3 is true, but not both.
8. If statement 7 is true, then 5 and 6 are both true.
9. Exactly 3 of the first 6 statements are true.
10. The next two statements are both true.
11. Exactly 1 of statements 7, 8 and 9 are true.
12. Exactly 4 of the preceding statements are true."
script o
property posits : {}
property upshots : missing value
on countTrues(indexList)
set sum to 0
repeat with i in indexList
if (my posits's item i) then set sum to sum + 1
end repeat
return sum
end countTrues
end script
-- While setting up, test statement 1, whose truth isn't about the others' truths.
set statements to statements's paragraphs
set nStatements to (count statements)
set statement1Truth to (nStatements = 12)
repeat with stmt from 1 to nStatements
set end of o's o's posits to false
tell (statements's item stmt's words) to set statement1Truth to ¬
((statement1Truth) and ((count) > 1) and (beginning = stmt as text))
end repeat
set output to {}
set firstIteration to true
repeat (2 ^ nStatements) times
-- Postulate answer:
if (firstIteration) then
set firstIteration to false
else -- "Increment" the 'posits' boolean array binarily.
repeat with stmt from 1 to nStatements
set o's posits's item stmt to (not (o's posits's item stmt))
if (result) then exit repeat -- No carry.
end repeat
end if
-- Test consistency:
tell o's posits to set o's upshots to {statement1Truth, ¬
(o's countTrues({7, 8, 9, 10, 11, 12}) = 3), ¬
(o's countTrues({2, 4, 6, 8, 10, 12}) = 2), ¬
((not (item 5)) or ((item 6) and (item 7))), ¬
(not ((item 2) or (item 3) or (item 4))), ¬
(o's countTrues({1, 3, 5, 7, 9, 11}) = 4), ¬
((item 2) ≠ (item 3)), ¬
((not (item 7)) or ((item 5) and (item 6))), ¬
(o's countTrues({1, 2, 3, 4, 5, 6}) = 3), ¬
((item 11) and (item 12)), ¬
(o's countTrues({7, 8, 9}) = 1), ¬
(o's countTrues({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}) = 4)}
set nMatches to 0
repeat with stmt from 1 to nStatements
if ((o's posits's item stmt) = (o's upshots's item stmt)) then set nMatches to nMatches + 1
end repeat
if (nMatches > nStatements - 2) then
set statementsPositedTrue to {}
repeat with stmt from 1 to nStatements
set thisPosit to o's posits's item stmt
if (thisPosit) then set end of statementsPositedTrue to stmt
if ((thisPosit) ≠ (o's upshots's item stmt)) then set failure to stmt
end repeat
set statementsPositedTrue's last item to "and " & statementsPositedTrue's end
if ((count statementsPositedTrue) > 2) then
set statementsPositedTrue to join(statementsPositedTrue, ", ")
else
set statementsPositedTrue to join(statementsPositedTrue, space)
end if
if (nMatches = nStatements) then
set output's end to "SOLUTION: statements " & statementsPositedTrue & " are true."
else
set output's end to "Near miss when statements " & statementsPositedTrue & ¬
" are posited true: posit for statement " & failure & " fails."
end if
end if
end repeat
return join(output, linefeed)
end twelveStatements
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
twelveStatements()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"Near miss when statements 1 and 4 are posited true: posit for statement 8 fails.
Near miss when statements 1 and 5 are posited true: posit for statement 8 fails.
Near miss when statements 1, 5, and 8 are posited true: posit for statement 11 fails.
Near miss when statements 1, 3, 4, 6, 7, and 9 are posited true: posit for statement 9 fails.
Near miss when statements 1, 3, 4, 8, and 9 are posited true: posit for statement 7 fails.
Near miss when statements 1, 4, 6, 8, and 9 are posited true: posit for statement 6 fails.
Near miss when statements 1, 2, 4, 7, 8, and 9 are posited true: posit for statement 8 fails.
Near miss when statements 1, 2, 4, 7, 9, and 10 are posited true: posit for statement 10 fails.
SOLUTION: statements 1, 3, 4, 6, 7, and 11 are true.
Near miss when statements 5, 8, and 11 are posited true: posit for statement 1 fails.
Near miss when statements 1, 5, 8, and 11 are posited true: posit for statement 12 fails.
Near miss when statements 1, 5, 6, 9, and 11 are posited true: posit for statement 8 fails.
Near miss when statements 1, 2, 4, 7, 9, and 12 are posited true: posit for statement 12 fails.
Near miss when statements 4, 8, 10, 11, and 12 are posited true: posit for statement 1 fails.
Near miss when statements 1, 4, 8, 10, 11, and 12 are posited true: posit for statement 12 fails.
Near miss when statements 5, 8, 10, 11, and 12 are posited true: posit for statement 1 fails.
Near miss when statements 1, 5, 8, 10, 11, and 12 are posited true: posit for statement 12 fails."</syntaxhighlight>
 
=={{header|AutoHotkey}}==
Line 1,283 ⟶ 1,406:
Missed by statement: 12
T F F F T F F T F T T T</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
len t[] 12
func f n .
if n = 1
return if 12 = 12
elif n = 2
for i = 7 to 12
s += t[i]
.
return if s = 3
elif n = 3
for i = 2 step 2 to 12
s += t[i]
.
return if s = 2
elif n = 4
if t[5] = 1
return if t[6] + t[7] = 2
.
return 1
elif n = 5
for i = 2 to 4
s += t[i]
.
return if s = 0
elif n = 6
for i = 1 step 2 to 11
s += t[i]
.
return if s = 4
elif n = 7
return if t[2] + t[3] = 1
elif n = 8
if t[7] = 1
return if t[5] + t[6] = 2
.
return 1
elif n = 9
for i = 1 to 6
s += t[i]
.
return if s = 3
elif n = 10
return if t[11] + t[12] = 2
elif n = 11
for i = 7 to 9
s += t[i]
.
return if s = 1
elif n = 12
for i = 1 to 11
s += t[i]
.
return if s = 4
.
.
for tst = 0 to 4095
h = tst
for i to 12
t[i] = h mod 2
h = h div 2
.
s = 0
for i to 12
s += if f i = t[i]
.
if s = 12
print t[]
.
.
</syntaxhighlight>
{{out}}
<pre>
[ 1 0 1 1 0 1 1 0 0 0 1 0 ]
</pre>
 
 
=={{header|Eiffel}}==
Line 1,473 ⟶ 1,674:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 1,482 ⟶ 1,683:
printSolution(bits)
= self.zipBy(bits,
(s,b => s.iif("T","F") + (s.xor:(b)).iif("* "," "))).summarize(new StringWriter());
 
toBit()
Line 1,492 ⟶ 1,693:
(bits => bits.Length == 12),
(bits => bits.last(6).selectBy::(x => x.toBit()).summarize() == 3 ),
(bits => bits.zipBy(new Range(1, 12),
(x,i => (i.toInt().isEven()).and:(x).toBit())).summarize() == 2 ),
(bits => bits[4].iif(bits[5] && bits[6],true) ),
Line 1,502 ⟶ 1,703:
(bits => bits.zipBy(new Range(1, 12),
(x,i => (i.toInt().isOdd()).and:(x).toBit() )).summarize() == 4 ),
(bits => bits[1].xor(bits[2]) ),
Line 1,508 ⟶ 1,709:
(bits => bits[6].iif(bits[5] && bits[4],true) ),
(bits => bits.top(6).selectBy::(x => x.toBit() ).summarize() == 3 ),
(bits => bits[10] && bits[11] ),
Line 1,514 ⟶ 1,715:
(bits => (bits[6].toBit() + bits[7].toBit() + bits[8].toBit())==1 ),
(bits => bits.top(11).selectBy::(x => x.toBit()).summarize() == 4 )
};
 
Line 1,521 ⟶ 1,722:
console.writeLine();
for(int n := 0,; n < 2.power(12),; n += 1)
{
var bits := BitArray32.load(n).top(12).toArray();
var results := puzzle.selectBy::(r => r(bits)).toArray();
var counts := bits.zipBy(results, (b,r => b.xor:(r).toBit() )).summarize();
 
counts =>
0 { console.printLine("Total hit :",results.printSolution:(bits)) }
1 { console.printLine("Near miss :",results.printSolution:(bits)) }
12 { console.printLine("Total miss:",results.printSolution:(bits)) };
};
Line 3,827 ⟶ 4,028:
close 1 2 4 7 9 10 (wrong 10)
close 1 2 4 7 8 9 (wrong 8)</pre>
 
 
=={{header|uBasic/4tH}}==
Line 3,912 ⟶ 4,114:
Near miss with statements 5 8 10 11 12 true (failed 1).
Near miss with statements 1 5 8 10 11 12 true (failed 12).</pre>
 
=={{header|Uiua}}==
{{works with|Uiua|0.10.0-dev.1}}
 
Defined the rules as functions to avoid the main loop devolving into line noise :-)
 
<syntaxhighlight lang="Uiua">
Even ← =1◿2⇡⧻ # nb indexes are zero-based
SA ← =12⧻ # Total is always twelve (don't test)
SB ← =3⧻⊚↘6 # Three of last six are true
SC ← =2⧻⊚⊏⊚Even. # Exactly two even rules are true
SD ← ⟨1◌|/×⟩⊢.↙3↘4 # If 5 is true so are 6 and 7
SE ← /׬⊏[1 2 3] # 2, 3, 4 are all false
SF ← =4⧻⊚⊏⊚¬Even. # Four odd rules are true
SG ← =1/+↙2↘1 # 2 xor 3
SH ← ⟨1◌|/×⟩⊢.⇌↙3↘4 # If 7 is true so are 6 and 5
SI ← =3⧻⊚↙6 # Three of first six are true
SJ ← /×⊏[10 11] # 11 and 12 are both true
SK ← =1/+⊏[6 7 8] # Exactly one of 7, 8, 9 is true
SL ← =4/+↘¯1 # Exactly four of above are true
 
⋯+1×2⇡2048 # Brute force sensible combinations
≡(
# Test each rule against the data and concatenate
:[⊃⊃⊃⊃⊃⊃⊃⊃⊃⊃⊃SA SB SC SD SE SF SG SH SI SJ SK SL].
⊟∩□:⊙(⊚⌵-). # Append hit-count
)
 
# Partition by fit, keep only hits and near-misses
°⊟↙2⊕□≡(◇⧻⊢).
 
# Print results
&p"\nNear Misses"&s+1◇⊚⊢↘1◇⊢&p"Hits"
⊏⍏≡⊢.°□ # Sort misses
≡(⊃(&p+1⊚°□⊢↘1)(&pf"\t"&pf+1◇⊢⊢&pf"Fails at "))
</syntaxhighlight>
{{out}}
<pre>
Hits
[1 3 4 6 7 11]
 
Near Misses
Fails at 6 [1 4 6 8 9]
Fails at 7 [1 3 4 8 9]
Fails at 8 [1 4]
Fails at 8 [1 5]
Fails at 8 [1 2 4 7 8 9]
Fails at 8 [1 5 6 9 11]
Fails at 9 [1 3 4 6 7 9]
Fails at 10 [1 2 4 7 9 10]
Fails at 11 [1 5 8]
Fails at 12 [1 5 8 11]
Fails at 12 [1 2 4 7 9 12]
Fails at 12 [1 4 8 10 11 12]
Fails at 12 [1 5 8 10 11 12]</pre>
 
=={{header|VBA}}==
Line 4,006 ⟶ 4,263:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv, Fmt
 
var predicates = [
Line 4,082 ⟶ 4,339:
(Fails at statement 10) 1 2 4 7 9 10
(Fails at statement 8) 1 2 4 7 8 9
</pre>
 
=={{header|XPL0}}==
{{trans|ALGOL W}}
<syntaxhighlight lang "XPL0"> \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</syntaxhighlight>
{{out}}
<pre>
Solutions
1 2 3 4 5 6 7 8 9 10 11 12
====================================
T - T T - T T - - - T -
Near solutions (incorrect values marked "*")
1 2 3 4 5 6 7 8 9 10 11 12
====================================
T - - T - - - -* - - - -
T - - - T - - -* - - - -
T - - - T - - T - - -* -
T - T T - T T - T* - - -
T - T T - - -* T T - - -
T - - T - T* - T T - - -
T T - T - - T T* T - - -
T T - T - - T - T T* - -
-* - - - T - - T - - T -
T - - - T - - T - - T -*
T - - - T T - -* T - T -
T T - T - - T - T - - T*
-* - - T - - - T - T T T
T - - T - - - T - T T T*
-* - - - T - - T - T T T
T - - - T - - T - T T T*
</pre>
 
130

edits