June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,20 @@
(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]))

View file

@ -47,7 +47,7 @@ program =
0 till(2 power int:12) do(:n)
[
var bits := BitArray32 new:n; top:12; toArray.
var results := puzzle selectBy(:r)( r eval:bits ); toArray.
var results := puzzle selectBy(:r)( r(bits) ); toArray.
var counts := bits zip:results by(:b:r)( b xor:r; toBit ); summarize.

View file

@ -0,0 +1,132 @@
PROGRAM TwelveStatements;
{
This program searches through the 4095 possible sets
of 12 statements for any which may be self-consistent.
}
CONST
max12b = 4095; { Largest 12 byte number. }
TYPE
statnum = 1..12; { statement numbers }
statset = set of statnum; { sets of statements }
VAR { global variables for use in main algorithm }
trialNumber: integer;
trialSet, testResults: statset;
function Convert(n: integer): statset;
{
Converts an integer into a set of statements.
For each "1" in the last 12 bits of
the integer's binary representation,
a statement number is put into the set.
}
var
i: statnum;
s: statset;
begin
s := []; { Empty set. }
for i := 12 downto 1 do begin
if (n mod 2) = 1 then s := s + [i];
n := n div 2
end;
Convert := s
end;
procedure Express(truths: statset);
{
Writes the statement number of each "truth",
with at least one space in front,
all on one line.
}
var n: statnum;
begin
for n := 1 to 12 do
if n in truths then write(n:3);
writeln
end;
function Count(truths: statset): integer;
{ Counts the statement numbers in the set. }
var
s: statnum;
i: integer;
begin
i := 0;
for s := 1 to 12 do if s in truths then i := i + 1;
Count := i
end;
function Test(truths: statset): statset;
{
Starts with a set of supposedly true statements
and checks which of the 12 statements can actually
be confirmed about the set itself.
}
var
evens, odds, confirmations: statset;
begin
evens := [2, 4, 6, 8, 10, 12];
odds := [1, 3, 5, 7, 9, 11];
{ Statement 1 is necessarily true. }
confirmations := [1];
{ Statement 2 }
if Count(truths * [7..12]) = 3
then confirmations := confirmations + [2];
{ Statement 3 }
if Count(truths * evens) = 2
then confirmations := confirmations + [3];
{ Statement 4 is true if 6 and 7 are true, or if 5 is false. }
if ([6, 7] <= truths) or not (5 in truths)
then confirmations := confirmations + [4];
{ Statement 5 }
if [2, 3, 4] <= truths
then confirmations := confirmations + [5];
{ Statement 6 }
if Count(truths * odds) = 4
then confirmations := confirmations + [6];
{ Statement 7 }
if (2 in truths) xor (3 in truths)
then confirmations := confirmations + [7];
{ Statement 8 is true if 5 and 6 are true, or if 7 is false. }
if ([5, 6] <= truths) or not (7 in truths)
then confirmations := confirmations + [8];
{ Statement 9 }
if Count(truths * [1..6]) = 3
then confirmations := confirmations + [9];
{ Statement 10 }
if [11, 12] <= truths
then confirmations := confirmations + [10];
{ Statement 11 }
if Count(truths * [7, 8, 9]) = 1
then confirmations := confirmations + [11];
{ Statement 12 }
if Count(truths - [12]) = 4
then confirmations := confirmations + [12];
Test := confirmations
end;
BEGIN { Main algorithm. }
for trialNumber := 1 to max12b do begin
trialSet := Convert(trialNumber);
testResults := Test(trialSet);
if testResults = trialSet then Express(trialSet)
end;
writeln('Done. Press ENTER.');
readln
END.

View file

@ -0,0 +1,81 @@
class LogicPuzzle {
val s = new Array[Boolean](13)
var count = 0
def check2: Boolean = {
var count = 0
for (k <- 7 to 12) if (s(k)) count += 1
s(2) == (count == 3)
}
def check3: Boolean = {
var count = 0
for (k <- 2 to 12 by 2) if (s(k)) count += 1
s(3) == (count == 2)
}
def check4: Boolean = s(4) == (!s(5) || s(6) && s(7))
def check5: Boolean = s(5) == (!s(2) && !s(3) && !s(4))
def check6: Boolean = {
var count = 0
for (k <- 1 to 11 by 2) if (s(k)) count += 1
s(6) == (count == 4)
}
def check7: Boolean = s(7) == ((s(2) || s(3)) && !(s(2) && s(3)))
def check8: Boolean = s(8) == (!s(7) || s(5) && s(6))
def check9: Boolean = {
var count = 0
for (k <- 1 to 6) if (s(k)) count += 1
s(9) == (count == 3)
}
def check10: Boolean = s(10) == (s(11) && s(12))
def check11: Boolean = {
var count = 0
for (k <- 7 to 9) if (s(k)) count += 1
s(11) == (count == 1)
}
def check12: Boolean = {
var count = 0
for (k <- 1 to 11) if (s(k)) count += 1
s(12) == (count == 4)
}
def check(): Unit = {
if (check2 && check3 && check4 && check5 && check6 && check7 && check8 && check9 && check10 && check11 && check12) {
for (k <- 1 to 12) if (s(k)) print(k + " ")
println()
count += 1
}
}
def recurseAll(k: Int): Unit = {
if (k == 13) check()
else {
s(k) = false
recurseAll(k + 1)
s(k) = true
recurseAll(k + 1)
}
}
}
object LogicPuzzle extends App {
val p = new LogicPuzzle
p.s(1) = true
p.recurseAll(2)
println()
println(s"${p.count} Solutions found.")
}

View file

@ -1,17 +1,17 @@
var conditions = [
{ false },
{|a| a.len == 13 },
{|a| Math.sum(a[7..12]) == 3 },
{|a| Math.sum(a[2..12 `by` 2]) == 2 },
{|a| [a[7..12]].count(true) == 3 },
{|a| [a[2..12 `by` 2]].count(true) == 2 },
{|a| a[5] ? (a[6] && a[7]) : true },
{|a| !a[2] && !a[3] && !a[4] },
{|a| Math.sum(a[1..11 `by` 2]) == 4 },
{|a| [a[1..11 `by` 2]].count(true) == 4 },
{|a| a[2] == true^a[3] },
{|a| a[7] ? (a[5] && a[6]) : true },
{|a| Math.sum(a[1..6]) == 3 },
{|a| Math.sum(a[11,12]) == 2 },
{|a| Math.sum(a[7..9]) == 1 },
{|a| Math.sum(a[1..11]) == 4 },
{|a| [a[1..6]].count(true) == 3 },
{|a| [a[11,12]].count(true) == 2 },
{|a| [a[7..9]].count(true) == 1 },
{|a| [a[1..11]].count(true) == 4 },
]
func miss(args) {
@ -19,7 +19,7 @@ func miss(args) {
}
for k in (^(1<<12)) {
var t = ("0%012b" % k -> chars.map {|bit| bit == '1' ? true : false })
var t = ("0%012b" % k -> chars.map {|bit| bit == '1' })
var no = miss(t)
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(' ')}"