Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
59
Task/Twelve-statements/ERRE/twelve-statements.erre
Normal file
59
Task/Twelve-statements/ERRE/twelve-statements.erre
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
PROGRAM TWELVE_STMS
|
||||
|
||||
!$DYNAMIC
|
||||
DIM PASS%[0],T%[0]
|
||||
|
||||
FUNCTION EOR(X,Y)
|
||||
EOR=(X AND NOT(Y)) OR (NOT(X) AND Y)
|
||||
END FUNCTION
|
||||
|
||||
BEGIN
|
||||
NSTATEMENTS%=12
|
||||
!$DIM PASS%[NSTATEMENTS%],T%[NSTATEMENTS%]
|
||||
|
||||
FOR TRY%=0 TO 2^NSTATEMENTS%-1 DO
|
||||
|
||||
! Postulate answer:
|
||||
FOR STMT%=1 TO 12 DO
|
||||
T%[STMT%]=(TRY% AND 2^(STMT%-1))<>0
|
||||
END FOR
|
||||
|
||||
! Test consistency:
|
||||
PASS%[1]=T%[1]=(NSTATEMENTS%=12)
|
||||
PASS%[2]=T%[2]=((T%[7]+T%[8]+T%[9]+T%[10]+T%[11]+T%[12])=-3)
|
||||
PASS%[3]=T%[3]=((T%[2]+T%[4]+T%[6]+T%[8]+T%[10]+T%[12])=-2)
|
||||
PASS%[4]=T%[4]=((NOT T%[5] OR (T%[6] AND T%[7])))
|
||||
PASS%[5]=T%[5]=(NOT T%[2] AND NOT T%[3] AND NOT T%[4])
|
||||
PASS%[6]=T%[6]=((T%[1]+T%[3]+T%[5]+T%[7]+T%[9]+T%[11])=-4)
|
||||
PASS%[7]=T%[7]=(EOR(T%[2],T%[3]))
|
||||
PASS%[8]=T%[8]=((NOT T%[7] OR (T%[5] AND T%[6])))
|
||||
PASS%[9]=T%[9]=((T%[1]+T%[2]+T%[3]+T%[4]+T%[5]+T%[6])=-3)
|
||||
PASS%[10]=T%[10]=(T%[11] AND T%[12])
|
||||
PASS%[11]=T%[11]=((T%[7]+T%[8]+T%[9])=-1)
|
||||
PASS%[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)
|
||||
|
||||
SUM=0
|
||||
FOR I%=1 TO 12 DO
|
||||
SUM=SUM+PASS%[I%]
|
||||
END FOR
|
||||
|
||||
CASE SUM OF
|
||||
-11->
|
||||
PRINT("Near miss with statements ";)
|
||||
FOR STMT%=1 TO 12 DO
|
||||
IF T%[STMT%] THEN PRINT(STMT%;) END IF
|
||||
IF NOT PASS%[STMT%] THEN MISS%=STMT% END IF
|
||||
END FOR
|
||||
PRINT("true (failed ";MISS%;").")
|
||||
END ->
|
||||
-12->
|
||||
PRINT("Solution! with statements ";)
|
||||
FOR STMT%=1 TO 12 DO
|
||||
IF T%[STMT%] THEN PRINT(STMT%;) END IF
|
||||
END FOR
|
||||
PRINT("true.")
|
||||
END ->
|
||||
END CASE
|
||||
|
||||
END FOR ! TRY%
|
||||
END PROGRAM
|
||||
27
Task/Twelve-statements/Phix/twelve-statements.phix
Normal file
27
Task/Twelve-statements/Phix/twelve-statements.phix
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
string s -- (eg "101101100010")
|
||||
integer t -- scratch
|
||||
|
||||
function s1() return length(s)=12 end function
|
||||
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
|
||||
function s4() return s[5]='0' or (s[6]='1' and s[7]='1') end function
|
||||
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
|
||||
function s7() return s[2]!=s[3] end function
|
||||
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
|
||||
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
|
||||
function s12() t=0 for i=1 to 11 do t+=s[i]='1' end for return t=4 end function
|
||||
|
||||
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
|
||||
113
Task/Twelve-statements/Swift/twelve-statements-1.swift
Normal file
113
Task/Twelve-statements/Swift/twelve-statements-1.swift
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
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")
|
||||
98
Task/Twelve-statements/Swift/twelve-statements-2.swift
Normal file
98
Task/Twelve-statements/Swift/twelve-statements-2.swift
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
import Foundation
|
||||
|
||||
internal enum PaddingOption {
|
||||
case Left
|
||||
case Right
|
||||
}
|
||||
|
||||
extension Array {
|
||||
func pad(element: Element, times: Int, toThe: PaddingOption) -> Array<Element> {
|
||||
let padded = [Element](count: times, repeatedValue: element)
|
||||
switch(toThe) {
|
||||
case .Left:
|
||||
return padded + self
|
||||
case .Right:
|
||||
return self + padded
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
return self
|
||||
} else if n >= self.count {
|
||||
return []
|
||||
}
|
||||
|
||||
return Array(self[n..<self.count])
|
||||
}
|
||||
|
||||
func stride(n: Int) -> Array<Element> {
|
||||
var result:[Element] = []
|
||||
for i in Swift.stride(from: 0, to: self.count, by: n) {
|
||||
result.append(self[i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func zipWithIndex() -> Array<(Element, Int)> {
|
||||
return [(Element, Int)](zip(self, indices(self)))
|
||||
}
|
||||
}
|
||||
|
||||
extension Int {
|
||||
func binaryRepresentationOfLength(length: Int) -> [Int] {
|
||||
var binaryRepresentation:[Int] = []
|
||||
var value = self
|
||||
while (value != 0) {
|
||||
binaryRepresentation.append(value & 1)
|
||||
value /= 2
|
||||
}
|
||||
return binaryRepresentation.pad(0, times: length-binaryRepresentation.count, toThe: .Right).reverse()
|
||||
}
|
||||
}
|
||||
|
||||
let problem = [
|
||||
"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."]
|
||||
|
||||
let statements:[([Bool] -> Bool)] = [
|
||||
{ s in s.count == 12 },
|
||||
{ s in s.drop(6).filter({ $0 }).count == 3 },
|
||||
{ s in s.drop(1).stride(2).filter({ $0 }).count == 2 },
|
||||
{ s in s[4] ? (s[5] && s[6]) : true },
|
||||
{ s in s.drop(1).take(3).filter({ $0 }).count == 0 },
|
||||
{ s in s.stride(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(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(11).filter({ $0 }).count == 4 }
|
||||
]
|
||||
|
||||
for variant in 0..<(1<<statements.count) {
|
||||
let attempt = variant.binaryRepresentationOfLength(statements.count).map { $0 == 1 }
|
||||
|
||||
if statements.map({ $0(attempt) }) == attempt {
|
||||
let trueAre = attempt.zipWithIndex().filter { $0.0 }.map { $0.1 + 1 }
|
||||
println("Solution found! True are: \(trueAre)")
|
||||
}
|
||||
}
|
||||
51
Task/Twelve-statements/jq/twelve-statements.jq
Normal file
51
Task/Twelve-statements/jq/twelve-statements.jq
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
def indexed(filter):
|
||||
. as $in
|
||||
| reduce range(0;length) as $i ([]; if ($i | filter) then . + [$in[$i]] else . end);
|
||||
|
||||
def count(value): map(select(. == value)) | length;
|
||||
|
||||
# The truth or falsity of the 12 statements can be captured in an array of size 12:
|
||||
def generate(k):
|
||||
if k == 1 then [true], [false]
|
||||
else generate(1) + generate(k-1)
|
||||
end;
|
||||
|
||||
# Input: a boolean array
|
||||
def evaluate:
|
||||
[ (length == 12), #1
|
||||
((.[6:] | count(true)) == 3), #2
|
||||
((indexed(. % 2 == 1) | count(true)) == 2), #3
|
||||
(if .[4] then .[5] and .[6] else true end), #4
|
||||
((.[1:4] | count(false)) == 3), #5
|
||||
((indexed(. % 2 == 0) | count(true)) == 4), #6
|
||||
(([.[1], .[2]] | count(true)) == 1), #7
|
||||
(if .[6] then .[4] and .[5] else true end), #8
|
||||
((.[0:6] | count(true)) == 3), #9
|
||||
(.[10] and .[11]), #10
|
||||
((.[6:9] | count(true)) == 1), #11
|
||||
((.[0:11] | count(true)) == 4) #12
|
||||
];
|
||||
|
||||
# The following query generates the solution to the problem:
|
||||
# generate(12) | . as $vector | if evaluate == $vector then $vector else empty end
|
||||
|
||||
# Running "task" as defined next would generate
|
||||
# both the general solution as well as the off-by-one solutions:
|
||||
|
||||
def task:
|
||||
|
||||
# count agreements
|
||||
def agreed(x;y): reduce range(0;x|length) as $i (0; if x[$i] == y[$i] then .+1 else . end);
|
||||
|
||||
reduce generate(12) as $vector
|
||||
([]; ($vector | evaluate) as $e
|
||||
| agreed($vector; $e) as $agreed
|
||||
| if $agreed == 12 then [[12,$vector]] + .
|
||||
elif $agreed == 11 then . + [[11, $vector]]
|
||||
else .
|
||||
end);
|
||||
|
||||
# Since the solutions have been given elsewhere, we simply count the
|
||||
# number of exact and off-by-one solutions:
|
||||
|
||||
task | length
|
||||
Loading…
Add table
Add a link
Reference in a new issue