Created
February 29, 2012 18:18
-
-
Save semmons99/1943272 to your computer and use it in GitHub Desktop.
minecraft crafting data modeling exercise part 2
This file contains hidden or 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
require "matrix" | |
require "forwardable" | |
class RecipeMatrix | |
extend Forwardable | |
def_delegators :@matrix, :to_a | |
def initialize(recipe) | |
@matrix = Matrix[*recipe] | |
end | |
def first_col_empty? | |
@matrix.column_vectors.first.all?{|c| c == :empty} | |
end | |
def first_row_empty? | |
@matrix.row_vectors.first.all?{|c| c == :empty} | |
end | |
def col_rotate! | |
@matrix = Matrix.columns( | |
@matrix | |
.column_vectors | |
.to_a | |
.rotate | |
) | |
end | |
def row_rotate! | |
@matrix = Matrix.rows( | |
@matrix | |
.row_vectors | |
.to_a | |
.rotate | |
) | |
end | |
end | |
class Recipe | |
attr_reader :recipe | |
def initialize(recipe) | |
@recipe = RecipeMatrix.new(recipe) | |
@recipe.col_rotate! while @recipe.first_col_empty? | |
@recipe.row_rotate! while @recipe.first_row_empty? | |
end | |
end | |
standardized_input = [ | |
[:coal, :empty, :empty, ], | |
[:stick, :empty, :empty, ], | |
[:empty, :empty, :empty, ], | |
] | |
inputs = [ | |
[ | |
[:coal, :empty, :empty, ], | |
[:stick, :empty, :empty, ], | |
[:empty, :empty, :empty, ], | |
], | |
[ | |
[:empty, :empty, :empty, ], | |
[:coal, :empty, :empty, ], | |
[:stick, :empty, :empty, ], | |
], | |
[ | |
[:empty, :coal, :empty, ], | |
[:empty, :stick, :empty, ], | |
[:empty, :empty, :empty, ], | |
], | |
[ | |
[:empty, :empty, :empty, ], | |
[:empty, :coal, :empty, ], | |
[:empty, :stick, :empty, ], | |
], | |
[ | |
[:empty, :empty, :coal, ], | |
[:empty, :empty, :stick, ], | |
[:empty, :empty, :empty, ], | |
], | |
[ | |
[:empty, :empty, :empty, ], | |
[:empty, :empty, :coal, ], | |
[:empty, :empty, :stick, ], | |
], | |
] | |
inputs.each do |input| | |
recipe = Recipe.new(input) | |
puts recipe.recipe.to_a == standardized_input | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment