Created
March 22, 2012 20:30
-
-
Save takehiko/2163691 to your computer and use it in GitHub Desktop.
Four fives: Method 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
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
class Rot90d | |
def initialize | |
@field_s = (<<EOS).gsub(/\n/, "") | |
** | |
** | |
****** | |
****** | |
** | |
** | |
EOS | |
reset_field | |
@width = @height = 6 | |
@field_size = @width * @height | |
@pos_1 = @width * (@height / 2 - 1) + @width / 2 - 1 | |
end | |
def start | |
print_field | |
@qstar = count_star / 4 | |
@stack = [] # array of position | |
@num_answer = 0 | |
search | |
end | |
def search(pos_start = 0) | |
pos_start.upto(@field_size - 1) do |i| | |
if @field[i, 1] == "*" | |
@stack << i | |
if @stack.length == @qstar | |
if @stack.index(@pos_1) && examine_field | |
@num_answer += 1 | |
puts "Answer No.#{@num_answer}" | |
print_field | |
end | |
reset_field | |
else | |
search(i + 1) | |
end | |
@stack.pop | |
end | |
end | |
end | |
def examine_field | |
reset_field | |
@stack.each do |pos| | |
@field[pos, 1] = "1" | |
end | |
rotate_superpose("1", "2") | |
rotate_superpose("2", "3") | |
rotate_superpose("3", "4") | |
# If there is no "*" in the field, then that is a solution! | |
count_star == 0 | |
end | |
def reset_field | |
@field = @field_s.dup | |
end | |
def count_star | |
@field.scan(/\*/).length | |
end | |
def print_field | |
@height.times do |i| | |
puts @field[@height * i, @width] | |
end | |
end | |
def xy_rot90(x_from, y_from) | |
[y_from, @height - 1 - x_from] | |
end | |
def rotate_superpose(c_from, c_to) | |
@field_size.times do |pos| | |
if @field[pos, 1] == c_from | |
y, x = pos.divmod(@width) | |
x_to, y_to = xy_rot90(x, y) | |
@field[@width * y_to + x_to] = c_to | |
end | |
end | |
end | |
end | |
if __FILE__ == $0 | |
Rot90d.new.start | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment