Created
January 15, 2013 22:23
-
-
Save jtprince/4542708 to your computer and use it in GitHub Desktop.
My solution to Twelve Statements:
(http://rosettacode.org/wiki/Twelve_statements)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://rosettacode.org/wiki/Twelve_statements | |
#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. | |
constraints = [ | |
->(st) { st.size == 12 }, | |
->(st) { st[-6,6].count(true) == 3 }, | |
->(st) { st.each_slice(2).map(&:last).count(true) == 2 }, | |
->(st) { st[4] ? (st[5] & st[6]) : true }, | |
->(st) { st[1..3].map(&:!).all? }, | |
->(st) { st.each_slice(2).map(&:first).count(true) == 4 }, | |
->(st) { st[1] ^ st[2] }, | |
->(st) { st[6] ? (st[4] & st[5]) : true }, | |
->(st) { st[0,6].count(true) == 3 }, | |
->(st) { st[10] & st[11] }, | |
->(st) { st[6..8].count(true) == 1 }, | |
->(st) { st[0,11].count(true) == 4 }, | |
] | |
Result = Struct.new(:truths, :consistency) | |
results = [true, false].repeated_permutation(12).map do |truths| | |
Result.new(truths, constraints.zip(truths).map {|cn,truth| cn[truths] == truth }) | |
end | |
puts "solution:", | |
results.find {|r| r.consistency.all? }.truths.inspect | |
puts "near misses: " | |
near_misses = results.select {|r| r.consistency.count(false) == 1 } | |
near_misses.each do |r| | |
puts "missed by statement #{r.consistency.index(false) + 1}" | |
puts r.truths.inspect | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment