Twelve statements: Difference between revisions

m
→‎{{header|Uiua}}: final fix :-)
m (→‎{{header|Uiua}}: final fix :-))
 
(44 intermediate revisions by 19 users not shown)
Line 1:
{{task|Puzzles}}
[[Category:Constraint Handling RulesCHR]]
{{omit from|Brlcad}}
{{omit from|GUISS}}
Line 34:
Print out a table of near misses, that is, solutions that are contradicted by only a single statement.
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
{{trans|Python}}
 
<syntaxhighlight lang="11l">[([Int] -> Bool)] predicates
predicates [+]= st -> st.len == 12
predicates [+]= st -> sum(st[(len)-6 ..]) == 3
predicates [+]= st -> sum(st[(1..).step(2)]) == 2
predicates [+]= st -> I st[4] {(st[5] [&] st[6])} E 1
predicates [+]= st -> sum(st[1.<4]) == 0
predicates [+]= st -> sum(st[(0..).step(2)]) == 4
predicates [+]= st -> sum(st[1.<3]) == 1
predicates [+]= st -> I st[6] {(st[4] [&] st[5])} E 1
predicates [+]= st -> sum(st[0.<6]) == 3
predicates [+]= st -> (st[10] [&] st[11])
predicates [+]= st -> sum(st[6.<9]) == 1
predicates [+]= st -> sum(st[0.<11]) == 4
 
F to_str(b)
R (0.<12).filter(i -> @b[i]).map(i -> i + 1).join(‘ ’)
 
print(‘Exact hits:’)
L(n) 0 .< (1 << 12)
V bools = [0] * 12
L(i) 12
I n [&] (1 << (11 - i)) != 0
bools[i] = 1
 
L(predicate) predicates
I Int(predicate(bools)) != bools[L.index]
L.break
L.was_no_break
print(‘ ’to_str(bools))
 
print("\nNear misses:")
L(n) 0 .< (1 << 12)
V bools = [0] * 12
L(i) 12
I n [&] (1 << (11 - i)) != 0
bools[i] = 1
 
V count = 0
L(predicate) predicates
I Int(predicate(bools)) == bools[L.index]
count++
I count == 11
L(predicate) predicates
V i = L.index
I Int(predicate(bools)) != bools[i]
print(f:‘ (Fails at statement {i + 1:2}) {to_str(bools)}’)
L.break</syntaxhighlight>
 
{{out}}
<pre>
Exact hits:
1 3 4 6 7 11
 
Near misses:
(Fails at statement 1) 5 8 11
(Fails at statement 1) 5 8 10 11 12
(Fails at statement 1) 4 8 10 11 12
(Fails at statement 8) 1 5
(Fails at statement 11) 1 5 8
(Fails at statement 12) 1 5 8 11
(Fails at statement 12) 1 5 8 10 11 12
(Fails at statement 8) 1 5 6 9 11
(Fails at statement 8) 1 4
(Fails at statement 12) 1 4 8 10 11 12
(Fails at statement 6) 1 4 6 8 9
(Fails at statement 7) 1 3 4 8 9
(Fails at statement 9) 1 3 4 6 7 9
(Fails at statement 12) 1 2 4 7 9 12
(Fails at statement 10) 1 2 4 7 9 10
(Fails at statement 8) 1 2 4 7 8 9
</pre>
 
=={{header|Ada}}==
Line 42 ⟶ 118:
are very handy for this task.
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Logic;
 
procedure Twelve_Statements is
Line 120 ⟶ 196:
Ada.Text_IO.Put_Line("Near Misses:");
Try(T => (1..12 => False), Fail => 1);
end Twelve_Statements;</langsyntaxhighlight>
 
{{out}}
Line 147 ⟶ 223:
Here is the definition the package Logic:
 
<langsyntaxhighlight Adalang="ada">generic
Number_Of_Statements: Positive;
package Logic is
Line 162 ⟶ 238:
function Half(T: Table; Which: Even_Odd) return Table;
end Logic;</langsyntaxhighlight>
 
And here is the implementation of the "convenience functions" in Logic:
 
<langsyntaxhighlight Adalang="ada">package body Logic is
function Sum(T: Table) return Natural is
Line 192 ⟶ 268:
end Half;
 
end Logic;</langsyntaxhighlight>
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin
% we have 12 statements to determine the truth/falsehood of (see task) %
 
Line 290 ⟶ 366:
printSolutions( 1, "Near solutions (incorrect values marked ""*"")" );
 
end.</langsyntaxhighlight>
{{out}}
<pre>Solutions
Line 316 ⟶ 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 323 ⟶ 522:
The code shows all cases where we have at least S-1 matches (where S = 12 statements).
 
<langsyntaxhighlight AutoHotkeylang="autohotkey">; Note: the original puzzle provides 12 statements and starts with
; "Given the following twelve statements...", so the first statement
; should ignore the F1 flag and always be true (see "( N == 1 )").
Line 398 ⟶ 597:
Return ( C == 4 )
}
}</langsyntaxhighlight>
{{out}}
<pre>11 -> 1 0 0 1 0 0 0 1 0 0 0 0
Line 422 ⟶ 621:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> nStatements% = 12
DIM Pass%(nStatements%), T%(nStatements%)
Line 464 ⟶ 663:
NEXT try%
END</langsyntaxhighlight>
{{out}}
<pre>
Line 487 ⟶ 686:
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">(
( number
= n done ntest oldFT
Line 751 ⟶ 950:
)
)
);</langsyntaxhighlight>
{{out}}
<pre> Solution:
Line 767 ⟶ 966:
(12.false)
That's it. I made 220 true/false guesses in all. (A brute force method needs 2^12=4096 guesses.</pre>
=={{header|Clojure}}==
<lang clojure>(use '[clojure.math.combinatorics]
 
(defn xor? [& args]
(odd? (count (filter identity args))))
 
(defn twelve-statements []
(for [[a b c d e f g h i j k l] (selections [true false] 12)
:when (true? a)
:when (if (= 3 (count (filter true? [g h i j k l]))) (true? b) (false? b))
:when (if (= 2 (count (filter true? [b d f h j l]))) (true? c) (false? c))
:when (if (or (false? e) (every? true? [e f g])) (true? d) (false? d))
:when (if (every? false? [b c d]) (true? e) (false? e))
:when (if (= 4 (count (filter true? [a c e g i k]))) (true? f) (false? f))
:when (if (xor? (true? b) (true? c)) (true? g) (false? g))
:when (if (or (false? g) (every? true? [e f g])) (true? h) (false? h))
:when (if (= 3 (count (filter true? [a b c d e f]))) (true? i) (false? i))
:when (if (every? true? [k l]) (true? j) (false? j))
:when (if (= 1 (count (filter true? [g h i]))) (true? k) (false? k))
:when (if (= 4 (count (filter true? [a b c d e f g h i j k]))) (true? l) (false? l))]
[a b c d e f g h i j k l]))</lang>
 
{{out}}
<pre>
=> (twelve-statements)
([true false true true false true true false false false true false])
</pre>
 
=={{header|Common Lisp}}==
<lang lisp>
(defparameter *state* (make-list 12))
 
(defparameter *statements* '(t ; 1
(= (count-true '(7 8 9 10 11 12)) 3) ; 2
(= (count-true '(2 4 6 8 10 12)) 2) ; 3
(or (not (p 5)) (and (p 6) (p 7))) ; 4
(and (not (p 2)) (not (p 3)) (not (p 4))) ; 5
(= (count-true '(1 3 5 7 9 11)) 4) ; 6
(or (and (p 2) (not (p 3))) (and (not (p 2)) (p 3))) ; 7
(or (not (p 7)) (and (p 5) (p 6))) ; 8
(= (count-true '(1 2 3 4 5 6)) 3) ; 9
(and (p 11) (p 12)) ;10
(= (count-true '(7 8 9)) 1) ;11
(= (count-true '(1 2 3 4 5 6 7 8 9 10 11)) 4))) ;12
 
(defun start ()
(loop while (not (equal *state* '(t t t t t t t t t t t t)))
do (progn (let ((true-stats (check)))
(cond ((= true-stats 11) (result nil))
((= true-stats 12) (result t))))
(new-state))))
 
(defun check ()
(loop for el in *state*
for stat in *statements*
counting (eq el (eval stat)) into true-stats
finally (return true-stats)))
 
(defun count-true (lst)
(loop for i in lst
counting (nth (- i 1) *state*) into total
finally (return total)))
 
(defun p (n)
(nth (- n 1) *state*))
 
(defun new-state ()
(let ((contr t))
(loop for i from 0 to 11
do (progn (setf (nth i *state*) (not (eq (nth i *state*) contr)))
(setq contr (and contr (not (nth i *state*))))))))
 
(defun result (?)
(format t "~:[Missed by one~;Solution:~] ~%~{~:[F~;T~] ~}~%" ? *state*))</lang>
<pre>
Missed by one
T F F T F F F F F F F F
Missed by one
T F F F T F F F F F F F
Missed by one
T F F F T F F T F F F F
Missed by one
T F T T F T T F T F F F
Missed by one
T F T T F F F T T F F F
Missed by one
T F F T F T F T T F F F
Missed by one
T T F T F F T T T F F F
Missed by one
T T F T F F T F T T F F
Solution:
T F T T F T T F F F T F
Missed by one
F F F F T F F T F F T F
Missed by one
T F F F T F F T F F T F
Missed by one
T F F F T T F F T F T F
Missed by one
T T F T F F T F T F F T
Missed by one
F F F T F F F T F T T T
Missed by one
T F F T F F F T F T T T
Missed by one
F F F F T F F T F T T T
Missed by one
T F F F T F F T F T T T
NIL</pre>
 
=={{header|C sharp}}==
{{works with|C sharp|6}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 942 ⟶ 1,031:
}
 
}</langsyntaxhighlight>
{{out}}
<pre>
Line 967 ⟶ 1,056:
=={{header|C++}}==
{{works with|gcc 5.1.0 c++11}}
<langsyntaxhighlight lang="c++">#include <iostream>
#include <vector>
#include <string>
Line 1,083 ⟶ 1,172:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,121 ⟶ 1,210:
1:T 2:F 3:F 4:F 5:T 6:F 7:F 8:T 9:F 10:T 11:T 12:T
</pre>
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(use '[clojure.math.combinatorics]
 
(defn xor? [& args]
(odd? (count (filter identity args))))
 
(defn twelve-statements []
(for [[a b c d e f g h i j k l] (selections [true false] 12)
:when (true? a)
:when (if (= 3 (count (filter true? [g h i j k l]))) (true? b) (false? b))
:when (if (= 2 (count (filter true? [b d f h j l]))) (true? c) (false? c))
:when (if (or (false? e) (every? true? [e f g])) (true? d) (false? d))
:when (if (every? false? [b c d]) (true? e) (false? e))
:when (if (= 4 (count (filter true? [a c e g i k]))) (true? f) (false? f))
:when (if (xor? (true? b) (true? c)) (true? g) (false? g))
:when (if (or (false? g) (every? true? [e f g])) (true? h) (false? h))
:when (if (= 3 (count (filter true? [a b c d e f]))) (true? i) (false? i))
:when (if (every? true? [k l]) (true? j) (false? j))
:when (if (= 1 (count (filter true? [g h i]))) (true? k) (false? k))
:when (if (= 4 (count (filter true? [a b c d e f g h i j k]))) (true? l) (false? l))]
[a b c d e f g h i j k l]))</syntaxhighlight>
 
{{out}}
<pre>
=> (twelve-statements)
([true false true true false true true false false false true false])
</pre>
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
(defparameter *state* (make-list 12))
 
(defparameter *statements* '(t ; 1
(= (count-true '(7 8 9 10 11 12)) 3) ; 2
(= (count-true '(2 4 6 8 10 12)) 2) ; 3
(or (not (p 5)) (and (p 6) (p 7))) ; 4
(and (not (p 2)) (not (p 3)) (not (p 4))) ; 5
(= (count-true '(1 3 5 7 9 11)) 4) ; 6
(or (and (p 2) (not (p 3))) (and (not (p 2)) (p 3))) ; 7
(or (not (p 7)) (and (p 5) (p 6))) ; 8
(= (count-true '(1 2 3 4 5 6)) 3) ; 9
(and (p 11) (p 12)) ;10
(= (count-true '(7 8 9)) 1) ;11
(= (count-true '(1 2 3 4 5 6 7 8 9 10 11)) 4))) ;12
 
(defun start ()
(loop while (not (equal *state* '(t t t t t t t t t t t t)))
do (progn (let ((true-stats (check)))
(cond ((= true-stats 11) (result nil))
((= true-stats 12) (result t))))
(new-state))))
 
(defun check ()
(loop for el in *state*
for stat in *statements*
counting (eq el (eval stat)) into true-stats
finally (return true-stats)))
 
(defun count-true (lst)
(loop for i in lst
counting (nth (- i 1) *state*) into total
finally (return total)))
 
(defun p (n)
(nth (- n 1) *state*))
 
(defun new-state ()
(let ((contr t))
(loop for i from 0 to 11
do (progn (setf (nth i *state*) (not (eq (nth i *state*) contr)))
(setq contr (and contr (not (nth i *state*))))))))
 
(defun result (?)
(format t "~:[Missed by one~;Solution:~] ~%~{~:[F~;T~] ~}~%" ? *state*))</syntaxhighlight>
<pre>
Missed by one
T F F T F F F F F F F F
Missed by one
T F F F T F F F F F F F
Missed by one
T F F F T F F T F F F F
Missed by one
T F T T F T T F T F F F
Missed by one
T F T T F F F T T F F F
Missed by one
T F F T F T F T T F F F
Missed by one
T T F T F F T T T F F F
Missed by one
T T F T F F T F T T F F
Solution:
T F T T F T T F F F T F
Missed by one
F F F F T F F T F F T F
Missed by one
T F F F T F F T F F T F
Missed by one
T F F F T T F F T F T F
Missed by one
T T F T F F T F T F F T
Missed by one
F F F T F F F T F T T T
Missed by one
T F F T F F F T F T T T
Missed by one
F F F F T F F T F T T T
Missed by one
T F F F T F F T F T T T
NIL</pre>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.functional;
 
immutable texts = [
Line 1,170 ⟶ 1,370:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Missed by statement: 8
Line 1,206 ⟶ 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}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 1,388 ⟶ 1,666:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,394 ⟶ 1,672:
1 solution found.
</pre>
 
=={{header|Elena}}==
ELENA 36.4x :
<langsyntaxhighlight lang="elena">import system'routines.;
import extensions.;
import extensions'text.;
 
extension op
{
printSolution : (bits)
= self zip:.zipBy(bits by,
(:s:,b)( s=> s.iif("T","F") + (s .xor:(b) ).iif("* "," ") ); ).summarize(String new StringWriter().);
 
toBit()
= self .iif(1,0).;
}
 
puzzle = new Func1[]
{
(
(:bits)( => bits length.Length == 12 ),
(:bits)( => bits .last:(6; ).selectBy::(:x)( => x .toBit (); ).summarize() == 3 ),
(:bits)( => bits.zipBy(new zipRange(RangeEnumerator from:1, to:12),
by(:x:,i)( (T<int=> (i.toInt() .isEven()).and:(x; ).toBit (); )).summarize() == 2 ),
(:bits)( => bits[4] .iif(bits[5] && bits[6],true) ),
(:bits)( => ((bits[1] || bits[2]) || bits[3]) inverted.Inverted ),
(:bits)( => bits.zipBy(new zipRange(RangeEnumerator from:1, to:12),
by(:x:,i)( (T<int=> (i.toInt() .isOdd()).and:(x; ).toBit (); )).summarize() == 4 ),
(:bits)( => bits[1] .xor(bits[2]) ),
(:bits)( => bits[6] .iif(bits[5] && bits[4],true) ),
(:bits)( => bits .top:(6; ).selectBy::(:x)( => x .toBit() ); .summarize() == 3 ),
(:bits)( => bits[10] && bits[11] ),
(:bits)( => (bits[6] .toBit() + bits[7] .toBit() + bits[8] .toBit())==1 ),
(:bits)( => bits .top:(11; ).selectBy::(:x)( => x .toBit (); ).summarize() == 4 )
};
).
 
public program()
{
[
console writeLine:"".writeLine();
for(int n := 0; till(2n < 2.power(12)); do(:n += 1)
[{
var bits := BitArray32 new:.load(n; ).top:(12; toArray).toArray();
var results := puzzle .selectBy(:r):(r => r(bits) ); toArray.toArray();
var counts := bits zip:.zipBy(results, by(:b:,r)( b=> b.xor:(r; ).toBit (); summarize)).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)) ].};
].};
console .readChar()
}</syntaxhighlight>
]</lang>
{{out}}
<pre>
Line 1,482 ⟶ 1,761:
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM TWELVE_STMS
 
Line 1,541 ⟶ 1,820:
 
END FOR ! TRY%
END PROGRAM</langsyntaxhighlight>
{{out}}<pre>
Near miss with statements 1 4 true (failed 8 ).
Line 1,564 ⟶ 1,843:
=={{header|Forth}}==
Forth is excellently suited to solve this, because it has excellent support for manipulating bitpatterns.
<langsyntaxhighlight lang="forth">: lastbit ( n1 -- n2)
dup if 1 swap begin dup 1 <> while swap 1+ swap 1 rshift repeat drop then
;
Line 1,606 ⟶ 1,885:
;
 
12statements</langsyntaxhighlight>
{{out}}
<pre>
Line 1,628 ⟶ 1,907:
ok
</pre>
 
 
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="freebasic">Dim As Integer nEnunciados = 12, intento, enun, errado
Dim As Integer Afirm(nEnunciados), T(nEnunciados)
 
For intento = 0 To 2^nEnunciados-1
REM Postular respuesta:
For enun = 1 To 12
T(enun) = (intento And 2^(enun-1)) <> 0
Next enum
REM Prueba de consistencia:
Afirm(1) = T(1) = (nEnunciados = 12)
Afirm(2) = T(2) = ((T(7)+T(8)+T(9)+T(10)+T(11)+T(12)) = -3)
Afirm(3) = T(3) = ((T(2)+T(4)+T(6)+T(8)+T(10)+T(12)) = -2)
Afirm(4) = T(4) = ((Not T(5) Or (T(6) And T(7))))
Afirm(5) = T(5) = (Not T(2) And Not T(3) And Not T(4))
Afirm(6) = T(6) = ((T(1)+T(3)+T(5)+T(7)+T(9)+T(11)) = -4)
Afirm(7) = T(7) = ((T(2) Or T(3)))
Afirm(8) = T(8) = ((Not T(7) Or (T(5) And T(6))))
Afirm(9) = T(9) = ((T(1)+T(2)+T(3)+T(4)+T(5)+T(6)) = -3)
Afirm(10) = T(10) = (T(11) And T(12))
Afirm(11) = T(11) = ((T(7)+T(8)+T(9)) = -1)
Afirm(12) = T(12) = ((T(1)+T(2)+T(3)+T(4)+T(5)+T(6) + T(7)+T(8)+T(9)+T(10)+T(11)) = -4)
Dim As Integer suma = 0
For cont As Integer = 1 To 12
suma += Afirm(cont)
Next cont
Select Case suma
Case -11
Color 7: Print "Casi resuelto, con los enunciados ";
For enun = 1 To 12
If T(enun) Then Print ; enun; " ";
If Not Afirm(enun) Then errado = enun
Next enun
Print "verdaderos (falsos"; errado; ")."
Case -12
Color 10: Print "­Resuelto! con los enunciados ";
For enun = 1 To 12
If T(enun) Then Print ; enun; " ";
Next enun
Print "verdaderos."
End Select
Next intento
Sleep</syntaxhighlight>
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,755 ⟶ 2,085:
solution <- tz
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,779 ⟶ 2,109:
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">enum Rule {
r01( 1, { r()*.num == (1..12) }),
r02( 2, { r(7..12).count { it.truth } == 3 }),
Line 1,833 ⟶ 2,163:
}
 
Rule.evaluate()</langsyntaxhighlight>
 
{{out}}
Line 1,853 ⟶ 2,183:
Near Miss: [5, 8, 10, 11, 12] (failed 1)
Near Miss: [5, 8, 11] (failed 1)</pre>
 
 
=={{header|Haskell}}==
Shows answers with 1 for true, followed by list of indices of contradicting elements in each set of 1/0s (index is 0-based).
 
<langsyntaxhighlight lang="haskell">import Data.List (findIndices)
 
tf =:: mapM[[Int] (\_-> Bool] -> [1,0[Int]])
tf = traverse (const [1, 0])
 
wrongness b = findIndices id . zipWith (/=) b . map (fromEnum . ($ b))
wrongness :: [Int] -> [[Int] -> Bool] -> [Int]
wrongness ns ps = findIndices id (zipWith (/=) ns (map (fromEnum . ($ ns)) ps))
statements = [ (==12) . length,
 
3 ⊂ [length statements-6..],
statements :: [[Int] -> Bool]
2 ⊂ [1,3..],
statements =
4 → [4..6],
[ (== 12) . length
0 ⊂ [1..3],
4 , 3 ⊂ [0,2length statements - 6 ..],
1 , 2 ⊂ [1,23 ..],
6 , 4 → [4 .. 6],
3 , 0 ⊂ [01 ..5 3],
2 , 4 ⊂ [100,112 ..],
, 1 ⊂ [61,7,8 2],
4 , 6 → [04 ..10 6]
, 3 ⊂ [0 .. 5]
] where
, 2 ⊂ [10, 11]
(s ⊂ x) b = s == (sum . map (b!!) . takeWhile (< length b)) x
, 1 ⊂ [6, 7, 8]
(a → x) b = (b!!a == 0) || all ((==1).(b!!)) x
, 4 ⊂ [0 .. 10]
]
testall s n = [(b, w) | b <- tf s, w <- [wrongness b s], length w == n]
where
(⊂), (→) :: Int -> [Int] -> [Int] -> Bool
(s ⊂ x) b = s == (sum . map (b !!) . takeWhile (< length b)) x
(a → x) b = (b !! a == 0) || all ((== 1) . (b !!)) x
 
testall :: [[Int] -> Bool] -> Int -> [([Int], [Int])]
testall s n =
[ (b, w)
| b <- tf s
, w <- [wrongness b s]
, length w == n ]
 
main :: IO ()
main = let t = testall statements in do
main =
putStrLn "Answer"
let t = testall statements
mapM_ print $ t 0
in do putStrLn "Near missesAnswer"
mapM_ print $ t 1</lang>0
putStrLn "Near misses"
mapM_ print $ t 1</syntaxhighlight>
{{out}}
<pre>
Line 1,912 ⟶ 2,254:
=={{header|J}}==
In the following 'apply' is the foreign conjunction:
<langsyntaxhighlight lang="j"> apply
128!:2
 
NB. example
'*:' apply 1 2 3
1 4 9</langsyntaxhighlight>
This enables us to apply strings (left argument) being verbs to the right argument, mostly a noun.
<langsyntaxhighlight lang="j">S=: <;._2 (0 :0)
12&=@# NB. 1. This is a numbered list of twelve statements.
3=+/@:{.~&_6 NB. 2. Exactly 3 of the last 6 statements are true.
Line 1,934 ⟶ 2,276:
)
 
testall=: (];"1 0<@I.@:(]~:(apply&><))"1) #:@i.@(2&^)@#</langsyntaxhighlight>
 
The output follows the Haskell convention: true/false bitstring followed by the index of a contradiction
 
'''All true'''
<langsyntaxhighlight lang="j"> (#~0=#@{::~&_1"1) testall S
┌───────────────────────┬┐
│1 0 1 1 0 1 1 0 0 0 1 0││
└───────────────────────┴┘</langsyntaxhighlight>
Or, numerically:
<langsyntaxhighlight lang="j"> 1+I.;(#~0=#@{::~&_1"1) testall S
1 3 4 6 7 11</langsyntaxhighlight>
 
'''Near misses'''
<langsyntaxhighlight lang="j"> (#~1=#@{::~&_1"1) testall S
┌───────────────────────┬──┐
│0 0 0 0 1 0 0 1 0 0 1 0│0 │
Line 1,981 ⟶ 2,323:
├───────────────────────┼──┤
│1 1 0 1 0 0 1 1 1 0 0 0│7 │
└───────────────────────┴──┘</langsyntaxhighlight>
 
'''Iterative for all true'''
<br>A repeat while true approach: x f^:(p)^:_ y
<langsyntaxhighlight lang="j"> (-N)&{. #: S <:@]^:((]-.@-:(apply&><)"1) (-N)&{.@#:@])^:(_) 2^N=.#S
1 0 1 1 0 1 1 0 0 0 1 0</langsyntaxhighlight>
 
Here is an alternative representation of the statements which might be slightly easier to read. (The behavior is identical):
 
<langsyntaxhighlight lang="j">true=:1 :'(m-1)&{'
 
S=: <;._2 (0 :0)
Line 2,005 ⟶ 2,347:
1 (= +/) 7 8 9 true NB. 11. Exactly 1 of statements 7, 8 and 9 are true.
4 (= +/) }: NB. 12. Exactly 4 of the preceding statements are true.
)</langsyntaxhighlight>
 
And here is an approach which does not use the verb apply, but instead mostly relies on simple arithmetic.
 
<langsyntaxhighlight lang="j">'sum not mask'=: |:".;._2(0 :0)
0; 0; 0 0 0 0 0 0 0 0 0 0 0 0 NB. 1. This is a numbered list of twelve statements.
3; 0; 0 0 0 0 0 0 1 1 1 1 1 1 NB. 2. Exactly 3 of the last 6 statements are true.
Line 2,025 ⟶ 2,367:
propositions=: |:#:i.2^#sum
 
errors=: propositions~:(1 - not { 1,propositions) >. sum = mask +/ .*propositions</langsyntaxhighlight>
 
Now, as before, we can find the consistent set of true and false values:
 
<langsyntaxhighlight Jlang="j"> #:I.0=+/errors
1 0 1 1 0 1 1 0 0 0 1 0
1+I.#:I.0=+/errors NB. true propositions for the consistent case
1 3 4 6 7 11</langsyntaxhighlight>
 
And, we can find the set which is inconsistent for only one proposition:
 
<langsyntaxhighlight Jlang="j"> offby1=: 1=+/errors
'Statement ',"1 (":1+I.|: offby1 #"1 errors),"1 ' is inconsistent with exactly ',"1 ((1":@:+I.)"1 #:I.offby1),"1 ' being true'
Statement 1 is inconsistent with exactly 5 8 11 being true
Line 2,053 ⟶ 2,395:
Statement 12 is inconsistent with exactly 1 2 4 7 9 12 being true
Statement 10 is inconsistent with exactly 1 2 4 7 9 10 being true
Statement 8 is inconsistent with exactly 1 2 4 7 8 9 being true</langsyntaxhighlight>
 
=={{header|Java}}==
Line 2,059 ⟶ 2,401:
The following Java code uses brute force. It tries to translate the logical statements as naturally as possible. The run time is almost zero.
 
<syntaxhighlight lang="java">
<lang Java>
public class LogicPuzzle
{
Line 2,173 ⟶ 2,515:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,197 ⟶ 2,539:
for a specific "select every nth item" filter, but also for a
generic "with_index" annotator.
<langsyntaxhighlight lang="jq">def indexed(filter):
. as $in
| reduce range(0;length) as $i ([]; if ($i | filter) then . + [$in[$i]] else . end);
Line 2,247 ⟶ 2,589:
# number of exact and off-by-one solutions:
 
task | length</langsyntaxhighlight>
{{out}}
$ jq -M -n -f Twelve_statements.jq
Line 2,254 ⟶ 2,596:
=={{header|Julia}}==
This task involves only 12 statements, so an exhaustive search of the 2^12 possible statement value combinations is quite feasible. The program shows "total misses" and the distribution of numbers of hits in addition to solutions and near misses.
<syntaxhighlight lang="julia">using Printf
<lang Julia>
 
function showflaggedbits{T<:BitArray{1}}(a::T, f::T)
tf = map(x->x ? "T" : "F", a)
Line 2,309 ⟶ 2,652:
println(@sprintf " %2d => %4d" i-1 mhist[i])
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,354 ⟶ 2,697:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.3
 
typealias Predicate = (String) -> Boolean
Line 2,398 ⟶ 2,741:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,424 ⟶ 2,767:
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">Print["Answer:\n", Column@Cases[#, {s_, 0} :> s], "\nNear misses:\n",
Column@Cases[#, {s_, 1} :> s]] &[{#,
Count[Boole /@ {Length@# == 12, Total@#[[7 ;;]] == 3,
Line 2,434 ⟶ 2,777:
Total@#[[;; 6]] == 3, #[[11]] + #[[12]] == 2,
Total@#[[7 ;; 9]] == 1, Total@#[[;; 11]] == 4} - #,
Except[0]]} & /@ Tuples[{1, 0}, 12]]</langsyntaxhighlight>
 
{{Out}}
<pre>Answer:
{1,0,1,1,0,1,1,0,0,0,1,0}
 
 
Near misses:
Line 2,458 ⟶ 2,799:
{0,0,0,0,1,0,0,1,0,1,1,1}
{0,0,0,0,1,0,0,1,0,0,1,0}</pre>
 
=={{header|Nim}}==
{{trans|Kotlin}}
Not quite a translation as we use an array of booleans instead of a string. There are also other differences but the final result is the same.
 
<syntaxhighlight lang="nim">import bitops, sequtils, strformat, strutils, sugar
 
type Bools = array[1..12, bool]
 
const Predicates = [1: (b: Bools) => b.len == 12,
2: (b: Bools) => b[7..12].count(true) == 3,
3: (b: Bools) => toSeq(countup(2, 12, 2)).mapIt(b[it]).count(true) == 2,
4: (b: Bools) => not b[5] or b[6] and b[7],
5: (b: Bools) => not b[2] and not b[3] and not b[4],
6: (b: Bools) => toSeq(countup(1, 12, 2)).mapIt(b[it]).count(true) == 4,
7: (b: Bools) => b[2] xor b[3],
8: (b: Bools) => not b[7] or b[5] and b[6],
9: (b: Bools) => b[1..6].count(true) == 3,
10: (b: Bools) => b[11] and b[12],
11: (b: Bools) => b[7..9].count(true) == 1,
12: (b: Bools) => b[1..11].count(true) == 4]
 
 
proc `$`(b: Bools): string =
toSeq(1..12).filterIt(b[it]).join(" ")
 
 
echo "Exacts hits:"
var bools: Bools
for n in 0..4095:
block check:
for i in 1..12: bools[i] = n.testBit(12 - i)
for i, predicate in Predicates:
if predicate(bools) != bools[i]:
break check
echo " ", bools
 
echo "\nNear misses:"
for n in 0..4095:
for i in 1..12: bools[i] = n.testBit(12 - i)
var count = 0
for i, predicate in Predicates:
if predicate(bools) == bools[i]: inc count
if count == 11:
for i, predicate in Predicates:
if predicate(bools) != bools[i]:
echo &" (Fails at statement {i:2}) {bools}"
break</syntaxhighlight>
 
{{out}}
<pre>Exacts hits:
1 3 4 6 7 11
 
Near misses:
(Fails at statement 1) 5 8 11
(Fails at statement 1) 5 8 10 11 12
(Fails at statement 1) 4 8 10 11 12
(Fails at statement 8) 1 5
(Fails at statement 11) 1 5 8
(Fails at statement 12) 1 5 8 11
(Fails at statement 12) 1 5 8 10 11 12
(Fails at statement 8) 1 5 6 9 11
(Fails at statement 8) 1 4
(Fails at statement 12) 1 4 8 10 11 12
(Fails at statement 6) 1 4 6 8 9
(Fails at statement 7) 1 3 4 8 9
(Fails at statement 9) 1 3 4 6 7 9
(Fails at statement 12) 1 2 4 7 9 12
(Fails at statement 10) 1 2 4 7 9 10
(Fails at statement 8) 1 2 4 7 8 9</pre>
 
=={{header|Pascal}}==
Line 2,464 ⟶ 2,875:
Inspired by the C++ implementation, this version makes extensive use of Pascal's built-in set handling capabilities.
 
<langsyntaxhighlight lang="pascal">PROGRAM TwelveStatements;
 
{
Line 2,595 ⟶ 3,006:
writeln('Done. Press ENTER.');
readln
END.</langsyntaxhighlight>
 
{{Out}}
Line 2,602 ⟶ 3,013:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use List::Util 'sum';
 
my @condition = (
Line 2,630 ⟶ 3,041:
print "1 miss (",$no[0],"): true statements are ", join( " ", grep { $truth[$_] } 1..12), "\n" if 1 == @no;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>1 miss (1): true statements are 5 8 11
Line 2,651 ⟶ 3,062:
</pre>
 
=={{header|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
{{Works with|rakudo|2016.07}}
<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>
<lang perl6>sub infix:<→> ($protasis, $apodosis) { !$protasis or $apodosis }
<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>
my @tests =
<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>
{ .end == 12 and all(.[1..12]) === any(True, False) },
<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>
{ 3 == [+] .[7..12] },
<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>
{ 2 == [+] .[2,4...12] },
<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>
{ .[5] → all .[6,7] },
<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>
{ none .[2,3,4] },
<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>
{ 4 == [+] .[1,3...11] },
<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>
{ one .[2,3] },
<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>
{ .[7] → all .[5,6] },
<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>
{ 3 == [+] .[1..6] },
{ all .[11,12] },
<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>
{ one .[7,8,9] },
<span style="color: #004080;">string</span> <span style="color: #000000;">misses</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"\n"</span>
{ 4 == [+] .[1..11] },
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Solution: {1,3,4,6,7,11}
 
Near miss: {5,8,11}, fail on 1
my @solutions;
Near miss: {5,8,10,11,12}, fail on 1
my @misses;
Near miss: {4,8,10,11,12}, fail on 1
Near miss: {1,5}, fail on 8
Near miss: {1,5,8}, fail on 11
Near miss: {1,5,8,11}, fail on 12
Near miss: {1,5,8,10,11,12}, fail on 12
Near miss: {1,5,6,9,11}, fail on 8
Near miss: {1,4}, fail on 8
Near miss: {1,4,8,10,11,12}, fail on 12
Near miss: {1,4,6,8,9}, fail on 6
Near miss: {1,3,4,8,9}, fail on 7
Near miss: {1,3,4,6,7,9}, fail on 9
Near miss: {1,2,4,7,9,12}, fail on 12
Near miss: {1,2,4,7,9,10}, fail on 10
Near miss: {1,2,4,7,8,9}, fail on 8
</pre>
 
=={{header|Picat}}==
for [X] (True, False) xx 12 {
{{trans|Prolog}}
my @assert = Nil, |$_;
my @result = Nil, |@tests.map({ ?.(@assert) });
my @true = @assert.grep(?*, :k);
my @cons = (@assert Z=== @result).grep(!*, :k);
given @cons {
when 0 { push @solutions, "<{@true}> is consistent."; }
when 1 { push @misses, "<{@true}> implies { "¬" if !@result[~$_] }$_." }
}
}
 
<syntaxhighlight lang="picat">% {{trans|Prolog}}
.say for @solutions;
go ?=>
say "";
puzzle,
say "Near misses:";
fail, % check for more answers
.say for @misses;</lang>
nl.
go => true.
 
puzzle =>
{{out}}
% 1. This is a numbered list of twelve statements.
<pre><1 3 4 6 7 11> is consistent.
L = [A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12],
L :: 0..1,
element(1, L, 1),
 
% 2. Exactly 3 of the last 6 statements are true.
Near misses:
A2 #<=> sum(L[7..12]) #= 3,
<1 2 4 7 8 9> implies ¬8.
<1 2 4 7 9 10> implies ¬10.
<1 2 4 7 9 12> implies ¬12.
<1 3 4 6 7 9> implies ¬9.
<1 3 4 8 9> implies 7.
<1 4 6 8 9> implies ¬6.
<1 4 8 10 11 12> implies ¬12.
<1 4> implies 8.
<1 5 6 9 11> implies 8.
<1 5 8 10 11 12> implies ¬12.
<1 5 8 11> implies 12.
<1 5 8> implies 11.
<1 5> implies 8.
<4 8 10 11 12> implies 1.
<5 8 10 11 12> implies 1.
<5 8 11> implies 1.</pre>
 
% 3. Exactly 2 of the even-numbered statements are true.
=={{header|Phix}}==
A3 #<=> sum([L[I]:I in 1..12,I mod 2 == 0]) #= 2,
<lang Phix>string s -- (eg "101101100010")
 
integer t -- scratch
% 4. If statement 5 is true, then statements 6 and 7 are both true.
A4 #<=> (A5 #=> (A6 #/\ A7)),
 
% 5. The 3 preceding statements are all false.
A5 #<=> sum(L[2..4]) #= 0,
 
% 6. Exactly 4 of the odd-numbered statements are true.
function s1() return length(s)=12 end function
A6 #<=> sum([L[I]:I in 1..12,I mod 2 == 1]) #= 4,
function s2() t=0 for i=7 to 12 do t+=s[i]='1' end for return t=3 end function
 
function s3() t=0 for i=2 to 12 by 2 do t+=s[i]='1' end for return t=2 end function
% 7. Either statement 2 or 3 is true, but not both.
function s4() return s[5]='0' or (s[6]='1' and s[7]='1') end function
A7 #<=> (A2 + A3 #= 1),
function s5() return s[2]='0' and s[3]='0' and s[4]='0' end function
 
function s6() t=0 for i=1 to 12 by 2 do t+=s[i]='1' end for return t=4 end function
% 8. If statement 7 is true, then 5 and 6 are both true.
function s7() return s[2]!=s[3] end function
A8 #<=> (A7 #=> A5 #/\ A6),
function s8() return s[7]='0' or (s[5]='1' and s[6]='1') end function
 
function s9() t=0 for i=1 to 6 do t+=s[i]='1' end for return t=3 end function
% 9. Exactly 3 of the first 6 statements are true.
function s10() return s[11]='1' and s[12]='1' end function
A9 #<=> sum(L[1..6]) #= 3,
function s11() t=0 for i=7 to 9 do t+=s[i]='1' end for return t=1 end function
 
function s12() t=0 for i=1 to 11 do t+=s[i]='1' end for return t=4 end function
% 10. The next two statements are both true.
A10 #<=> (A11 #/\ A12),
 
% 11. Exactly 1 of statements 7, 8 and 9 are true.
A11 #<=> (A7 + A8 + A9 #= 1),
 
% 12. Exactly 4 of the preceding statements are true.
A12 #<=> sum(L[1..11]) #= 4,
 
solve(L),
 
println('L'=L),
printf("Statements %w are true.\n", [I.to_string : I in 1..12, L[I] == 1].join(" ")),
nl.</syntaxhighlight>
 
sequence r = repeat(0,12)
for b=1 to 12 do
r[b] = routine_id(sprintf("s%d",b))
end for
for i=0 to power(2,12)-1 do
s = sprintf("%012b",i)
for b=1 to 12 do
if call_func(r[b],{})!=(s[b]='1') then exit end if
if b=12 then ?s end if
end for
end for</lang>
{{out}}
<pre>L=[1,0,1,1,0,1,1,0,0,0,1,0]
<pre>
Statements 1 3 4 6 7 11 are true.</pre>
"101101100010"
 
</pre>
 
=={{header|Prolog}}==
Works with '''SWI-Prolog''' and '''library(clpfd)'''.
<langsyntaxhighlight Prologlang="prolog">puzzle :-
% 1. This is a numbered list of twelve statements.
L = [A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12],
Line 2,796 ⟶ 3,230:
 
my_write(_N, 0).
</syntaxhighlight>
</lang>
{{out}}
<pre> ?- puzzle.
Line 2,809 ⟶ 3,243:
Python's boolean type <tt>bool</tt>is a subclass of <tt>int</tt>, so boolean values True, False can be used as integers (1, 0, respectively) in numerical contexts.
This fact is used in the lambda expressions that use function sum.
<langsyntaxhighlight lang="python">
from itertools import product
#from pprint import pprint as pp
Line 2,849 ⟶ 3,283:
 
for stm in full + partial:
printer(*stm)</langsyntaxhighlight>
 
{{out}}
Line 2,891 ⟶ 3,325:
This question really begs to be done with <tt>amb</tt>
 
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
 
Line 2,959 ⟶ 3,393:
;; -> '(1 3 4 6 7 11)
 
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2016.07}}
<syntaxhighlight lang="raku" line>sub infix:<→> ($protasis, $apodosis) { !$protasis or $apodosis }
my @tests =
{ .end == 12 and all(.[1..12]) === any(True, False) },
{ 3 == [+] .[7..12] },
{ 2 == [+] .[2,4...12] },
{ .[5] → all .[6,7] },
{ none .[2,3,4] },
{ 4 == [+] .[1,3...11] },
{ one .[2,3] },
{ .[7] → all .[5,6] },
{ 3 == [+] .[1..6] },
{ all .[11,12] },
{ one .[7,8,9] },
{ 4 == [+] .[1..11] },
;
 
my @solutions;
my @misses;
 
for [X] (True, False) xx 12 {
my @assert = Nil, |$_;
my @result = Nil, |@tests.map({ ?.(@assert) });
my @true = @assert.grep(?*, :k);
my @cons = (@assert Z=== @result).grep(!*, :k);
given @cons {
when 0 { push @solutions, "<{@true}> is consistent."; }
when 1 { push @misses, "<{@true}> implies { "¬" if !@result[~$_] }$_." }
}
}
 
.say for @solutions;
say "";
say "Near misses:";
.say for @misses;</syntaxhighlight>
 
{{out}}
<pre><1 3 4 6 7 11> is consistent.
 
Near misses:
<1 2 4 7 8 9> implies ¬8.
<1 2 4 7 9 10> implies ¬10.
<1 2 4 7 9 12> implies ¬12.
<1 3 4 6 7 9> implies ¬9.
<1 3 4 8 9> implies 7.
<1 4 6 8 9> implies ¬6.
<1 4 8 10 11 12> implies ¬12.
<1 4> implies 8.
<1 5 6 9 11> implies 8.
<1 5 8 10 11 12> implies ¬12.
<1 5 8 11> implies 12.
<1 5 8> implies 11.
<1 5> implies 8.
<4 8 10 11 12> implies 1.
<5 8 10 11 12> implies 1.
<5 8 11> implies 1.</pre>
 
=={{header|REXX}}==
===generalized logic===
<langsyntaxhighlight lang="rexx">/*REXX program solves the "Twelve Statement Puzzle". */
q=12; @stmt=right('statement',20) /*number of statements in the puzzle. */
m=0 /*[↓] statement one is TRUE by fiat.*/
Line 2,993 ⟶ 3,487:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
yeses: parse arg L,H,B; #=0; do i=L to H by word(B 1, 1); #=#+@.i; end; return #</langsyntaxhighlight>
'''output'''
<pre>
Line 3,005 ⟶ 3,499:
 
===discrete logic===
<langsyntaxhighlight lang="rexx">/*REXX program solves the "Twelve Statement Puzzle". */
q=12; @stmt=right('statement',20) /*number of statements in the puzzle. */
m=0 /*[↓] statement one is TRUE by fiat.*/
Line 3,033 ⟶ 3,527:
end /*tell*/
end /*e*/
end /*pass*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
'''output''' &nbsp; is the same as the 1<sup>st</sup> version.
<br><br>
 
===optimized===
<langsyntaxhighlight lang="rexx">/*REXX program solves the "Twelve Statement Puzzle". */
q=12; @stmt=right('statement',20) /*number of statements in the puzzle. */
m=0 /*[↓] statement one is TRUE by fiat.*/
Line 3,064 ⟶ 3,558:
end /*j*/
end /*e*/
end /*pass*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
'''output''' &nbsp; is the same as the 1<sup>st</sup> version.
<br><br>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">constraints = [
->(st) { st.size == 12 },
->(st) { st.last(6).count(true) == 3 },
Line 3,097 ⟶ 3,591:
near_misses.each do |r|
puts "missed by statement #{r.consistency.index(false) + 1}", r.truths.to_s
end</langsyntaxhighlight>
 
{{out}}
Line 3,142 ⟶ 3,636:
===Imperative Programming (Ugly)===
{{trans|Java}}
<langsyntaxhighlight Scalalang="scala">class LogicPuzzle {
val s = new Array[Boolean](13)
var count = 0
Line 3,222 ⟶ 3,716:
println(s"${p.count} Solutions found.")
 
}</langsyntaxhighlight>
{{Out}}See it in running in your browser by [https://scastie.scala-lang.org/XaRsz8gOQxavQ5rwMxi9ZA Scastie (JVM)] or
by [https://scalafiddle.io/sf/x73tnT6/1 ScalaFiddle (JavaScript)].
Line 3,228 ⟶ 3,722:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">var conditions = [
{ false },
{|a| a.len == 13 },
Line 3,253 ⟶ 3,747:
no.len == 0 && say "Solution: true statements are #{1..12->grep{t[_]}.join(' ')}"
no.len == 1 && say "1 miss (#{no[0]}): true statements are #{1..12->grep{t[_]}.join(' ')}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,275 ⟶ 3,769:
</pre>
 
=={{header|Swift Playground}}==
{{works with|Swift|5.2}}
==={{trans|Java}}===
<lang Swift>var statements = Array(count: 13, repeatedValue: false)
statements[1] = true
var count = 0
 
func check2() -> Bool {
var count = 0
for (var k = 7; k <= 12; k++) {
if (statements[k]) {
count++
}
}
return statements[2] == (count == 3)
}
 
func check3() -> Bool {
var count = 0
for (var k = 2; k <= 12; k += 2) {
if (statements[k]) {
count++
}
}
return statements[3] == (count == 2)
}
 
func check4() -> Bool {
return statements[4] == (!statements[5] || statements[6] && statements[7])
}
 
func check5() -> Bool {
return statements[5] == (!statements[2] && !statements[3] && !statements[4])
}
 
func check6() -> Bool {
var count = 0
for (var k = 1; k <= 11; k += 2) {
if (statements[k]) {
count++
}
}
return statements[6] == (count == 4)
}
 
func check7() -> Bool {
return statements[7] == ((statements[2] || statements[3]) && !(statements[2] && statements[3]))
}
 
func check8() -> Bool {
return statements[8] == ( !statements[7] || statements[5] && statements[6])
}
 
func check9() -> Bool {
var count = 0
for (var k = 1; k <= 6; k++) {
if (statements[k]) {
count++
}
}
return statements[9] == (count == 3)
}
 
func check10() -> Bool {
return statements[10] == (statements[11] && statements[12])
}
 
func check11() -> Bool {
var count = 0
for (var k = 7; k <= 9; k++) {
if (statements[k]) {
count++
}
}
return statements[11] == (count == 1)
}
 
func check12() -> Bool {
var count = 0
for (var k = 1; k <= 11; k++) {
if (statements[k]) {
count++
}
}
return statements[12] == (count == 4)
}
 
func check() {
if (check2() && check3() && check4() && check5() && check6()
&& check7() && check8() && check9() && check10() && check11()
&& check12()) {
for (var k = 1; k <= 12; k++) {
if (statements[k]) {
print("\(k) ")
}
}
println()
count++
}
}
 
func checkAll(k:Int) {
if (k == 13) {
check()
} else {
statements[k] = false
checkAll(k + 1)
statements[k] = true
checkAll(k + 1)
}
}
 
checkAll(2)
println()
println("\(count) solutions found")</lang>
{{out}}
<pre>
1 3 4 6 7 11
 
1 solutions found
Program ended with exit code: 0
</pre>
 
import UIKit
===Semi-functional solution===
<lang Swift>import Foundation
 
internal enum PaddingOption {
Line 3,405 ⟶ 3,782:
case Right
}
 
extension Array {
func pad(element: Element, times: Int, toThe: PaddingOption) -> Array<Element> {
let padded = [Element](count: times, repeatedValue: element)
let padded = [Element](repeating: element, count: times)
switch(toThe) {
case .Left:
Line 3,416 ⟶ 3,794:
}
}
func take(n: Int) -> Array<Element> {
if n <= 0 {
return []
}
return Array(self[0..<Swift.min(n, self.count)])
}
func drop(n: Int) -> Array<Element> {
if n <= 0 {
Line 3,431 ⟶ 3,808:
return []
}
return Array(self[n..<self.count])
}
func stride(n: Int) -> Array<Element> {
var result:[Element] = []
Line 3,442 ⟶ 3,818:
return result
}
func zipWithIndex() -> Array<(Element, Int)> {
returnlet result = [(Element, Int)](zip(self, indices(self).indices))
return result
}
}
 
extension Int {
func binaryRepresentationOfLength(length: Int) -> [Int] {
Line 3,456 ⟶ 3,833:
value /= 2
}
returnlet result = binaryRepresentation.pad(element: 0, times: length-binaryRepresentation.count, toThe: .Right).reverse()
return result
}
}
 
let problem = [
"1. This is a numbered list of twelve statements.",
Line 3,473 ⟶ 3,851:
"11. Exactly 1 of statements 7, 8 and 9 are true.",
"12. Exactly 4 of the preceding statements are true."]
 
let statements:[(([Bool]) -> Bool)] = [
{ s in s.count == 12 },
{ s in s.drop(n: 6).filter({ $0 }).count == 3 },
{ s in s.drop(n: 1).stride(n: 2).filter({ $0 }).count == 2 },
{ s in s[4] ? (s[5] && s[6]) : true },
{ s in s.drop(n: 1).take(n: 3).filter({ $0 }).count == 0 },
{ s in s.stride(n: 2).filter({ $0 }).count == 4 },
{ s in [s[1], s[2]].filter({ $0 }).count == 1 },
{ s in s[6] ? (s[4] && s[5]) : true },
{ s in s.take(n: 6).filter({ $0 }).count == 3 },
{ s in [s[10], s[11]].filter({ $0 }).count == 2 },
{ s in [s[6], s[7], s[8]].filter({ $0 }).count == 1 },
{ s in s.take(n: 11).filter({ $0 }).count == 4 }
]
 
for variant in 0..<(1<<statements.count) {
let attempt = variant.binaryRepresentationOfLength(length: statements.count).map { $0 == 1 }
if statements.map({ $0(attempt) }) == attempt {
let trueAre = attempt.zipWithIndex().filter { $0.0 }.map { $0.1 + 1 }
printlnprint("Solution found! True are: \(trueAre)")
}
}
}</lang>
 
{{out}}
<pre>
Solution found! True are: [1, 3, 4, 6, 7, 11]
</pre>
----
Solution found! True are: [1, 3, 4, 6, 7, 11]
 
=={{header|Tcl}}==
{{works with|Tcl|8.6}} <!-- but not 8.6b3; the lmap command post-dates that release -->
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
# Function to evaluate the truth of a statement
Line 3,576 ⟶ 3,954:
foreach {state j} $differ {
puts "almost found\t[renderstate $state] \u21d2 [expr {[lindex $state $j-1]?"\u00ac":{}}]S($j)"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,600 ⟶ 3,978:
=={{header|TXR}}==
 
<langsyntaxhighlight lang="txrlisp">(defmacro defconstraints (name size-name (var) . forms)
^(progn (defvar ,size-name ,(length forms))
(defun ,name (,var)
Line 3,635 ⟶ 4,013:
 
(each ((r results))
(put-line `@r`))</langsyntaxhighlight>
 
{{out}}
Line 3,650 ⟶ 4,028:
close 1 2 4 7 9 10 (wrong 10)
close 1 2 4 7 8 9 (wrong 8)</pre>
 
 
=={{header|uBasic/4tH}}==
{{trans|BBC Basic}}
<syntaxhighlight lang="text">S = 12
For T = 0 To (2^S)-1
 
Line 3,716 ⟶ 4,095:
Loop
 
Return</langsyntaxhighlight>
Output:
<pre>Near miss with statements 1 4 true (failed 8).
Line 3,735 ⟶ 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}}==
{{trans|Phix}}<langsyntaxhighlight lang="vb">Public s As String '-- (eg "101101100010")
Public t As Integer '-- scratch
Line 3,823 ⟶ 4,257:
Next
Next
End Sub</langsyntaxhighlight>{{out}}
<pre>Found solution:101101100010</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Conv, Fmt
 
var predicates = [
Fn.new { |s| s.count == 13 }, // indexing starts at 0 but first bit ignored
Fn.new { |s| (7..12).count { |i| s[i] == "1" } == 3 },
Fn.new { |s| [2, 4, 6, 8, 10, 12].count { |i| s[i] == "1" } == 2 },
Fn.new { |s| s[5] == "0" || (s[6] == "1" && s[7] == "1") },
Fn.new { |s| s[2] == "0" && s[3] == "0" && s[4] == "0" },
Fn.new { |s| [1, 3, 5, 7, 9, 11].count { |i| s[i] == "1" } == 4 },
Fn.new { |s| Conv.itob(Conv.btoi(s[2] == "1") ^ Conv.btoi(s[3] == "1")) },
Fn.new { |s| s[7] == "0" || (s[5] == "1" && s[6] == "1") },
Fn.new { |s| (1..6).count { |i| s[i] == "1" } == 3 },
Fn.new { |s| s[11] == "1" && s[12] == "1" },
Fn.new { |s| (7..9).count { |i| s[i] == "1" } == 1 },
Fn.new { |s| (1..11).count { |i| s[i] == "1" } == 4 }
]
 
var show = Fn.new { |s, indent|
if (indent) System.write(" ")
for (i in 0...s.count) if (s[i] == "1") System.write("%(i) ")
System.print()
}
 
System.print("Exact hits:")
for (i in 0..4095) {
var s = Fmt.swrite("$013b", i)
var j = 1
if (predicates.all { |pred|
var res = pred.call(s) == (s[j] == "1")
j = j + 1
return res
}) show.call(s, true)
}
 
System.print("\nNear misses:")
for (i in 0..4095) {
var s = Fmt.swrite("$013b", i)
var j = 1
var c = predicates.count { |pred|
var res = pred.call(s) == (s[j] == "1")
j = j + 1
return res
}
if (c == 11) {
var k = 1
for (pred in predicates) {
if (pred.call(s) != (s[k] == "1") ) break
k = k + 1
}
Fmt.write(" (Fails at statement $2d) ", k)
show.call(s, false)
}
}</syntaxhighlight>
 
{{out}}
<pre>
Exact hits:
1 3 4 6 7 11
 
Near misses:
(Fails at statement 1) 5 8 11
(Fails at statement 1) 5 8 10 11 12
(Fails at statement 1) 4 8 10 11 12
(Fails at statement 8) 1 5
(Fails at statement 11) 1 5 8
(Fails at statement 12) 1 5 8 11
(Fails at statement 12) 1 5 8 10 11 12
(Fails at statement 8) 1 5 6 9 11
(Fails at statement 8) 1 4
(Fails at statement 12) 1 4 8 10 11 12
(Fails at statement 6) 1 4 6 8 9
(Fails at statement 7) 1 3 4 8 9
(Fails at statement 9) 1 3 4 6 7 9
(Fails at statement 12) 1 2 4 7 9 12
(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>
 
=={{header|Yabasic}}==
{{trans|Phix}}
<syntaxhighlight lang="yabasic">sub s1() return len(s$)=12 end sub
sub s2() local t, i : t=0 : for i=7 to 12 : t = t + (mid$(s$, i, 1) <> "0") : next : return t=3 end sub
sub s3() local t, i : t=0 : for i=2 to 12 step 2 : t = t + (mid$(s$, i, 1) <> "0") : next : return t=2 end sub
sub s4() return mid$(s$, 5, 1) = "0" or (mid$(s$, 6, 1) <> "0" and mid$(s$, 7, 1) <> "0") end sub
sub s5() return mid$(s$, 2, 1) = "0" and mid$(s$, 3, 1) = "0" and mid$(s$, 4, 1) = "0" end sub
sub s6() local t, i : t=0 : for i=1 to 12 step 2 : t = t + mid$(s$, i, 1) <> "0" : next : return t=4 end sub
sub s7() return mid$(s$, 2, 1) <> mid$(s$, 3, 1) end sub
sub s8() return mid$(s$, 7, 1) = "0" or (mid$(s$, 5, 1) <> "0" and mid$(s$, 6, 1) <> "0") end sub
sub s9() local t, i : t=0 : for i=1 to 6 : t = t + mid$(s$, i, 1) <> "0" : next : return t=3 end sub
sub s10() return mid$(s$, 11, 1) <> "0" and mid$(s$, 12, 1) <> "0" end sub
sub s11() local t, i : t=0 : for i=7 to 9 : t = t + mid$(s$, i, 1) <> "0" : next : return t=1 end sub
sub s12() local t, i : t=0 : for i=1 to 11 : t = t + mid$(s$, i, 1) <> "0" : next : return t=4 end sub
dim r$(12)
 
for b=1 to 12
r$(b) = "s"+str$(b)
next
for i=0 to 2^12-1
s$ = right$("000000000000" + bin$(i), 12)
for b=1 to 12
if execute(r$(b)) <> (mid$(s$, b, 1) <> "0") break
if b=12 print s$
next
next</syntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">var statements; // list of 13 Bools, statements[0] is garbage to make 1 based
fcn s0 { False } // dummy for padding
fcn s1 { True }
Line 3,855 ⟶ 4,520:
(12).pump(List,'wrap(n){ statements[n] and n or Void.Skip })
.concat(",").println(" : ",vm.pasteArgs());
}</langsyntaxhighlight>
{{out}}
<pre>
62

edits