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
# With newlines, for readability | |
def q(a); | |
a.permutation; | |
end; | |
def n(a,b,c,d); | |
i(a,b,c,d,1)||i(c,d,a,b,1); | |
end; | |
def i(a,b,c,d,o=0); | |
(0..4).any?{|i|a[i]==b&&c[i+o]==d}; | |
end; |
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
def q(a) | |
a.permutation | |
end | |
q([1,2,3]).to_a # => [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] |
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
# Implies | |
def i(a, b, c, d) | |
(0..4).any? do |i| | |
a[i] == b && | |
c[i] == d | |
end | |
end | |
# Left of | |
def l(a, b, c, d) |
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
colors = [:white, :yellow, :blue, :red, :green] | |
cigars = [:blends, :pall_mall, :prince, :bluemasters, :dunhill] | |
nationalities = [:german, :swedish, :british, :norwegian, :danish] | |
drinks = [:beer, :water, :tea, :milk, :coffee] | |
pets = [:birds, :cats, :horses, :fish, :dogs] | |
# Becomes: | |
c = [0,1,2,3,4] | |
s = [5,6,7,8,9] # Cigars becomes 's' for 'smokes' |
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
def implies?(set_a, val_a, set_b, val_b) | |
(0..4).any? do |i| | |
set_a[i] == val_a && | |
set_b[i] == val_b | |
end | |
end | |
# Becomes: | |
def i(a, b, c, d) |
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
class Symbol | |
def humanize | |
result = self.to_s | |
result.gsub!('_', ' ') | |
result.split(' ').collect{|part| part.capitalize }.join(' ') | |
end | |
end | |
solution, attempts = solve |
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
@colors = [:white, :yellow, :blue, :red, :green].shuffle.permutation | |
@cigars = [:blends, :pall_mall, :prince, :bluemasters, :dunhill].shuffle.permutation | |
@nationalities = [:german, :swedish, :british, :norwegian, :danish,].shuffle.permutation | |
@drinks = [:beer, :water, :tea, :milk, :coffee].shuffle.permutation | |
@pets = [:birds, :cats, :horses, :fish, :dogs].shuffle.permutation | |
def solve | |
i = 0 | |
@colors.each do |colors| | |
i += 1 |
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
i = 0 | |
[:white, :yellow, :blue, :red, :green].permutation.each do |colors| | |
[:german, :swedish, :british, :norwegian, :danish,].permutation.each do |nationalities| | |
i += 1 | |
end | |
end | |
puts i | |
# => 14400 |
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
[:white, :yellow, :blue, :red, :green].permutation.each do |colors| | |
[:german, :swedish, :british, :norwegian, :danish,].permutation.each do |nationalities| | |
[:birds, :cats, :horses, :fish, :dogs].permutation.each do |pets| | |
[:beer, :water, :tea, :milk, :coffee].permutation.each do |drinks| | |
[:blends, :pall_mall, :prince, :bluemasters, :dunhill].permutation.each do |cigars| | |
next unless nationalities[0] == :norwegian | |
next unless drinks[2] == :milk | |
next unless implies?(nationalities, :british, colors, :red) | |
next unless implies?(nationalities, :swedish, pets, :dogs) |
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
nationalities = [:german, :swedish, :british, :norwegian, :danish] | |
nationalities[0] == :norwegian | |
# => false | |
drinks = [:beer, :water, :milk, :coffee, :tea] | |
drinks[2] == :milk | |
# => true |