Twelve statements: Difference between revisions

m
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 3,415:
</pre>
 
import UIKit
===Semi-functional solution===
<lang Swift>import Foundation
 
 
<lang Swift>import Foundation
internal enum PaddingOption {
case Left
case Right
}
 
extension Array {
func pad(element: Element, times: Int, toThe: PaddingOption) -> Array<Element> {
let padded = [Element](countrepeating: timeselement, repeatedValuecount: elementtimes)
switch(toThe) {
case .Left:
Line 3,433 ⟶ 3,436:
}
}
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,448 ⟶ 3,451:
return []
}
return Array(self[n..<self.count])
}
func stride(n: Int) -> Array<Element> {
var result:[Element] = []
Line 3,459 ⟶ 3,462:
return result
}
func zipWithIndex() -> Array<(Element, Int)> {
// returnlet result = [(Element, Int)](zip(self, indices(self)))
let result = [(Element, Int)](zip(self, self.indices))
// let result = [(Element, Int)](zip(indices,self.indices))
 
return result
}
}
 
extension Int {
func binaryRepresentationOfLength(length: Int) -> [Int] {
Line 3,473 ⟶ 3,481:
value /= 2
}
return//let result = binaryRepresentation.pad(element: 0, times: length-binaryRepresentation.count, toThe: .Right).reverse()
let result = binaryRepresentation.pad(element: 0, times: length-binaryRepresentation.count, toThe: .Right)
return result
}
}
 
let problem = [
"1. This is a numbered list of twelve statements.",
Line 3,490 ⟶ 3,502:
"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>
 
=={{header|Tcl}}==