Twelve statements: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring the hard way) |
|||
Line 2,474: | Line 2,474: | ||
{0,0,0,0,1,0,0,1,0,1,1,1} |
{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> |
{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. |
|||
<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</lang> |
|||
{{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}}== |
=={{header|Pascal}}== |