Twelve statements: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 3,415: | Line 3,415: | ||
</pre> |
</pre> |
||
import UIKit |
|||
===Semi-functional solution=== |
|||
⚫ | |||
⚫ | |||
internal enum PaddingOption { |
internal enum PaddingOption { |
||
case Left |
case Left |
||
case Right |
case Right |
||
} |
} |
||
extension Array { |
extension Array { |
||
func pad(element: Element, times: Int, toThe: PaddingOption) -> Array<Element> { |
func pad(element: Element, times: Int, toThe: PaddingOption) -> Array<Element> { |
||
⚫ | |||
let padded = [Element]( |
let padded = [Element](repeating: element, count: times) |
||
switch(toThe) { |
switch(toThe) { |
||
case .Left: |
case .Left: |
||
Line 3,433: | Line 3,436: | ||
} |
} |
||
} |
} |
||
⚫ | |||
func take(n: Int) -> Array<Element> { |
func take(n: Int) -> Array<Element> { |
||
if n <= 0 { |
if n <= 0 { |
||
return [] |
return [] |
||
} |
} |
||
⚫ | |||
return Array(self[0..<Swift.min(n, self.count)]) |
return Array(self[0..<Swift.min(n, self.count)]) |
||
} |
} |
||
⚫ | |||
func drop(n: Int) -> Array<Element> { |
func drop(n: Int) -> Array<Element> { |
||
if n <= 0 { |
if n <= 0 { |
||
Line 3,448: | Line 3,451: | ||
return [] |
return [] |
||
} |
} |
||
return Array(self[n..<self.count]) |
return Array(self[n..<self.count]) |
||
} |
} |
||
func stride(n: Int) -> Array<Element> { |
func stride(n: Int) -> Array<Element> { |
||
var result:[Element] = [] |
var result:[Element] = [] |
||
Line 3,459: | Line 3,462: | ||
return result |
return result |
||
} |
} |
||
func zipWithIndex() -> Array<(Element, Int)> { |
func zipWithIndex() -> Array<(Element, Int)> { |
||
// let 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 { |
extension Int { |
||
func binaryRepresentationOfLength(length: Int) -> [Int] { |
func binaryRepresentationOfLength(length: Int) -> [Int] { |
||
Line 3,473: | Line 3,481: | ||
value /= 2 |
value /= 2 |
||
} |
} |
||
//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 = [ |
let problem = [ |
||
"1. This is a numbered list of twelve statements.", |
"1. This is a numbered list of twelve statements.", |
||
Line 3,490: | Line 3,502: | ||
"11. Exactly 1 of statements 7, 8 and 9 are true.", |
"11. Exactly 1 of statements 7, 8 and 9 are true.", |
||
"12. Exactly 4 of the preceding statements are true."] |
"12. Exactly 4 of the preceding statements are true."] |
||
let statements:[([Bool] -> Bool)] = [ |
let statements:[(([Bool]) -> Bool)] = [ |
||
{ s in s.count == 12 }, |
{ s in s.count == 12 }, |
||
{ s in s.drop(6).filter({ $0 }).count == 3 }, |
{ s in s.drop(n: 6).filter({ $0 }).count == 3 }, |
||
{ s in s.drop(1).stride(2).filter({ $0 }).count == 2 }, |
{ 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[4] ? (s[5] && s[6]) : true }, |
||
{ s in s.drop(1).take(3).filter({ $0 }).count == 0 }, |
{ s in s.drop(n: 1).take(n: 3).filter({ $0 }).count == 0 }, |
||
{ s in s.stride(2).filter({ $0 }).count == 4 }, |
{ s in s.stride(n: 2).filter({ $0 }).count == 4 }, |
||
{ s in [s[1], s[2]].filter({ $0 }).count == 1 }, |
{ s in [s[1], s[2]].filter({ $0 }).count == 1 }, |
||
{ s in s[6] ? (s[4] && s[5]) : true }, |
{ s in s[6] ? (s[4] && s[5]) : true }, |
||
{ s in s.take(6).filter({ $0 }).count == 3 }, |
{ s in s.take(n: 6).filter({ $0 }).count == 3 }, |
||
{ s in [s[10], s[11]].filter({ $0 }).count == 2 }, |
{ 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[6], s[7], s[8]].filter({ $0 }).count == 1 }, |
||
{ s in s.take(11).filter({ $0 }).count == 4 } |
{ s in s.take(n: 11).filter({ $0 }).count == 4 } |
||
] |
] |
||
for variant in 0..<(1<<statements.count) { |
for variant in 0..<(1<<statements.count) { |
||
let attempt = variant.binaryRepresentationOfLength(statements.count).map { $0 == 1 } |
let attempt = variant.binaryRepresentationOfLength(length: statements.count).map { $0 == 1 } |
||
if statements.map({ $0(attempt) }) == attempt { |
if statements.map({ $0(attempt) }) == attempt { |
||
let trueAre = attempt.zipWithIndex().filter { $0.0 }.map { $0.1 + 1 } |
let trueAre = attempt.zipWithIndex().filter { $0.0 }.map { $0.1 + 1 } |
||
print("Solution found! True are: \(trueAre)") |
|||
} |
} |
||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Solution found! True are: [1, 3, 4, 6, 7, 11] |
|||
</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |