Created
October 23, 2013 00:37
-
-
Save geronimod/7110599 to your computer and use it in GitHub Desktop.
Waragon Contest
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 Ingredient | |
include Comparable | |
def ==(other) | |
self.class == other.class | |
end | |
def to_s | |
self.class.name.downcase | |
end | |
end | |
class Candy < Ingredient; end | |
class Fruit < Ingredient; end | |
class Sweet < Ingredient; end | |
class Cookie < Ingredient | |
def ==(other) | |
true | |
end | |
end | |
class Bowl | |
def initialize(ingredients=[Array.new(3)] * 3) | |
@ingredients = ingredients | |
end | |
def has_ingredients_aligned? | |
vertical_aligned? || horizontal_aligned? | |
end | |
def vertical_aligned? | |
@ingredients[0][0] == @ingredients[1][0] && @ingredients[1][0] == @ingredients[2][0] || | |
@ingredients[0][1] == @ingredients[1][1] && @ingredients[1][1] == @ingredients[2][1] || | |
@ingredients[0][2] == @ingredients[1][2] && @ingredients[1][2] == @ingredients[2][2] | |
end | |
def horizontal_aligned? | |
@ingredients[0][0] == @ingredients[0][1] && @ingredients[0][1] == @ingredients[0][2] || | |
@ingredients[1][0] == @ingredients[1][1] && @ingredients[1][1] == @ingredients[1][2] || | |
@ingredients[2][0] == @ingredients[2][1] && @ingredients[2][1] == @ingredients[2][2] | |
end | |
def to_s | |
@ingredients.map { |i| "#{i}\n" }.join("") | |
end | |
end | |
class Cake | |
def initialize(ingredients) | |
@bowl = Bowl.new ingredients | |
end | |
def tasty? | |
@bowl.has_ingredients_aligned? | |
end | |
def to_s | |
@bowl.to_s | |
end | |
end | |
class Kitchen | |
def self.cook_tasty_cakes | |
ingredients = [Candy.new] * 3 | |
ingredients += [Fruit.new] * 3 | |
ingredients += [Sweet.new] * 3 | |
ingredients << Cookie.new | |
posibilities = ingredients.permutation(9).to_a | |
tasty_cakes = [] | |
posibilities.each do |ingredients| | |
cake = Cake.new ingredients.each_slice(3).to_a | |
tasty_cakes << cake if cake.tasty? | |
end | |
tasty_cakes.each { |c| p c } | |
puts "Tasty cakes: #{tasty_cakes.size}" | |
# Tasty cakes: 979776 | |
end | |
end | |
Kitchen.cook_tasty_cakes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment