Twelve statements: Difference between revisions

m
→‎{{header|Uiua}}: final fix :-)
m (→‎{{header|Uiua}}: final fix :-))
 
(25 intermediate revisions by 14 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 770 ⟶ 969:
=={{header|C sharp}}==
{{works with|C sharp|6}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 832 ⟶ 1,031:
}
 
}</langsyntaxhighlight>
{{out}}
<pre>
Line 857 ⟶ 1,056:
=={{header|C++}}==
{{works with|gcc 5.1.0 c++11}}
<langsyntaxhighlight lang="c++">#include <iostream>
#include <vector>
#include <string>
Line 973 ⟶ 1,172:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,013 ⟶ 1,212:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(use '[clojure.math.combinatorics]
 
(defn xor? [& args]
Line 1,032 ⟶ 1,231:
: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]))</langsyntaxhighlight>
 
{{out}}
Line 1,041 ⟶ 1,240:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
(defparameter *state* (make-list 12))
 
Line 1,085 ⟶ 1,284:
 
(defun result (?)
(format t "~:[Missed by one~;Solution:~] ~%~{~:[F~;T~] ~}~%" ? *state*))</langsyntaxhighlight>
<pre>
Missed by one
Line 1,124 ⟶ 1,323:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.functional;
 
immutable texts = [
Line 1,171 ⟶ 1,370:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Missed by statement: 8
Line 1,207 ⟶ 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,389 ⟶ 1,666:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,397 ⟶ 1,674:
 
=={{header|Elena}}==
ELENA 56.0x :
<langsyntaxhighlight lang="elena">import system'routines;
import extensions;
import extensions'text;
Line 1,406 ⟶ 1,683:
printSolution(bits)
= self.zipBy(bits,
(s,b => s.iif("T","F") + (s.xor:(b)).iif("* "," "))).summarize(new StringWriter());
 
toBit()
Line 1,416 ⟶ 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,426 ⟶ 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,432 ⟶ 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,438 ⟶ 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,445 ⟶ 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)) };
};
console.readChar()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,484 ⟶ 1,761:
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM TWELVE_STMS
 
Line 1,543 ⟶ 1,820:
 
END FOR ! TRY%
END PROGRAM</langsyntaxhighlight>
{{out}}<pre>
Near miss with statements 1 4 true (failed 8 ).
Line 1,566 ⟶ 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,608 ⟶ 1,885:
;
 
12statements</langsyntaxhighlight>
{{out}}
<pre>
Line 1,630 ⟶ 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,758 ⟶ 2,085:
solution <- tz
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,782 ⟶ 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,836 ⟶ 2,163:
}
 
Rule.evaluate()</langsyntaxhighlight>
 
{{out}}
Line 1,860 ⟶ 2,187:
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 :: [[Int] -> Bool] -> [[Int]]
Line 1,901 ⟶ 2,228:
mapM_ print $ t 0
putStrLn "Near misses"
mapM_ print $ t 1</langsyntaxhighlight>
{{out}}
<pre>
Line 1,927 ⟶ 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,949 ⟶ 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,996 ⟶ 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,020 ⟶ 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,040 ⟶ 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,068 ⟶ 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,074 ⟶ 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,188 ⟶ 2,515:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,212 ⟶ 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,262 ⟶ 2,589:
# number of exact and off-by-one solutions:
 
task | length</langsyntaxhighlight>
{{out}}
$ jq -M -n -f Twelve_statements.jq
Line 2,269 ⟶ 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.
<langsyntaxhighlight Julialang="julia">using Printf
 
function showflaggedbits{T<:BitArray{1}}(a::T, f::T)
Line 2,325 ⟶ 2,652:
println(@sprintf " %2d => %4d" i-1 mhist[i])
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,370 ⟶ 2,697:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.3
 
typealias Predicate = (String) -> Boolean
Line 2,414 ⟶ 2,741:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,440 ⟶ 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,450 ⟶ 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,474 ⟶ 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,480 ⟶ 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,611 ⟶ 3,006:
writeln('Done. Press ENTER.');
readln
END.</langsyntaxhighlight>
 
{{Out}}
Line 2,618 ⟶ 3,013:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use List::Util 'sum';
 
my @condition = (
Line 2,646 ⟶ 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,668 ⟶ 3,063:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>string s -- (eg "101101100010")
<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>
integer t -- scratch
<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>
function s1() return length(s)=12 end function
<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>
function s2() t=0 for i=7 to 12 do t+=s[i]='1' end for return t=3 end function
<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>
function s3() t=0 for i=2 to 12 by 2 do t+=s[i]='1' end for return t=2 end function
<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>
function s4() return s[5]='0' or (s[6]='1' and s[7]='1') end function
<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>
function s5() return s[2]='0' and s[3]='0' and s[4]='0' end function
<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>
function s6() t=0 for i=1 to 12 by 2 do t+=s[i]='1' end for return t=4 end function
<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>
function s7() return s[2]!=s[3] end function
<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>
function s8() return s[7]='0' or (s[5]='1' and s[6]='1') end function
<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>
function s9() t=0 for i=1 to 6 do t+=s[i]='1' end for return t=3 end function
<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>
function s10() return s[11]='1' and s[12]='1' end function
function s11() t=0 for i=7 to 9 do t+=s[i]='1' end for return t=1 end function
<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>
function s12() t=0 for i=1 to 11 do t+=s[i]='1' end for return t=4 end function
<span style="color: #004080;">string</span> <span style="color: #000000;">misses</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"\n"</span>
 
<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>
sequence r = repeat(0,12)
<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>
for b=1 to 12 do
<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>
r[b] = routine_id(sprintf("s%d",b))
<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>
end for
<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>
for i=0 to power(2,12)-1 do
<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>
s = sprintf("%012b",i)
<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>
for b=1 to 12 do
<span style="color: #000000;">t</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">pass</span>
if call_func(r[b],{})!=(s[b]='1') then exit end if
<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>
if b=12 then ?s end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<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>
end for</lang>
<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}
"101101100010"
 
Near miss: {5,8,11}, fail on 1
Near miss: {5,8,10,11,12}, fail on 1
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}}==
{{trans|Prolog}}
 
<syntaxhighlight lang="picat">% {{trans|Prolog}}
go ?=>
puzzle,
fail, % check for more answers
nl.
go => true.
 
puzzle =>
% 1. This is a numbered list of twelve statements.
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.
A2 #<=> sum(L[7..12]) #= 3,
 
% 3. Exactly 2 of the even-numbered statements are true.
A3 #<=> sum([L[I]:I in 1..12,I mod 2 == 0]) #= 2,
 
% 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.
A6 #<=> sum([L[I]:I in 1..12,I mod 2 == 1]) #= 4,
 
% 7. Either statement 2 or 3 is true, but not both.
A7 #<=> (A2 + A3 #= 1),
 
% 8. If statement 7 is true, then 5 and 6 are both true.
A8 #<=> (A7 #=> A5 #/\ A6),
 
% 9. Exactly 3 of the first 6 statements are true.
A9 #<=> sum(L[1..6]) #= 3,
 
% 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>
 
{{out}}
<pre>L=[1,0,1,1,0,1,1,0,0,0,1,0]
Statements 1 3 4 6 7 11 are true.</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,753 ⟶ 3,230:
 
my_write(_N, 0).
</syntaxhighlight>
</lang>
{{out}}
<pre> ?- puzzle.
Line 2,766 ⟶ 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,806 ⟶ 3,283:
 
for stm in full + partial:
printer(*stm)</langsyntaxhighlight>
 
{{out}}
Line 2,848 ⟶ 3,325:
This question really begs to be done with <tt>amb</tt>
 
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
 
Line 2,916 ⟶ 3,393:
;; -> '(1 3 4 6 7 11)
 
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2016.07}}
<syntaxhighlight lang="raku" perl6line>sub infix:<→> ($protasis, $apodosis) { !$protasis or $apodosis }
my @tests =
Line 2,955 ⟶ 3,432:
say "";
say "Near misses:";
.say for @misses;</langsyntaxhighlight>
 
{{out}}
Line 2,980 ⟶ 3,457:
=={{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 3,010 ⟶ 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,022 ⟶ 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,050 ⟶ 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,081 ⟶ 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,114 ⟶ 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,159 ⟶ 3,636:
===Imperative Programming (Ugly)===
{{trans|Java}}
<langsyntaxhighlight Scalalang="scala">class LogicPuzzle {
val s = new Array[Boolean](13)
var count = 0
Line 3,239 ⟶ 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,245 ⟶ 3,722:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">var conditions = [
{ false },
{|a| a.len == 13 },
Line 3,270 ⟶ 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,405 ⟶ 3,882:
=={{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,477 ⟶ 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,501 ⟶ 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,536 ⟶ 4,013:
 
(each ((r results))
(put-line `@r`))</langsyntaxhighlight>
 
{{out}}
Line 3,551 ⟶ 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,617 ⟶ 4,095:
Loop
 
Return</langsyntaxhighlight>
Output:
<pre>Near miss with statements 1 4 true (failed 8).
Line 3,636 ⟶ 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,724 ⟶ 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}}
<langsyntaxhighlight Yabasiclang="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
Line 3,753 ⟶ 4,488:
if b=12 print s$
next
next</langsyntaxhighlight>
 
=={{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,785 ⟶ 4,520:
(12).pump(List,'wrap(n){ statements[n] and n or Void.Skip })
.concat(",").println(" : ",vm.pasteArgs());
}</langsyntaxhighlight>
{{out}}
<pre>
64

edits