Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- class Array
- def point(x, y)
- self[y][x]
- end
- def set_point(x, y, char)
- self[y][x] = char
- self
- end
- def find_zero
- each_with_index do |ary, y|
- x = ary.find_index '0'
- if x
- return [x, y]
- end
- end
- end
- def find_char(char)
- each_with_index do |ary, y|
- x = ary.find_index char
- if x
- return [x, y]
- end
- end
- nil
- end
- def up
- zero_x, zero_y = find_zero
- return false if zero_y == 0
- char = point(zero_x, zero_y - 1)
- return false if char == '='
- ary = Marshal.load(Marshal.dump(self))
- ary.set_point(zero_x, zero_y, char)
- ary.set_point(zero_x, zero_y - 1, '0')
- ary
- end
- def down
- zero_x, zero_y = find_zero
- return false if zero_y >= self.size - 1
- char = point(zero_x, zero_y + 1)
- return false if char == '='
- ary = Marshal.load(Marshal.dump(self))
- ary.set_point(zero_x, zero_y, char)
- ary.set_point(zero_x, zero_y + 1, '0')
- ary
- end
- def right
- zero_x, zero_y = find_zero
- return false if zero_x >= self.first.size - 1
- char = point(zero_x + 1, zero_y)
- return false if char == '='
- ary = Marshal.load(Marshal.dump(self))
- ary.set_point(zero_x, zero_y, char)
- ary.set_point(zero_x + 1, zero_y, '0')
- ary
- end
- def left
- zero_x, zero_y = find_zero
- return false if zero_x == 0
- char = point(zero_x - 1, zero_y)
- return false if char == '='
- ary = Marshal.load(Marshal.dump(self))
- ary.set_point(zero_x, zero_y, char)
- ary.set_point(zero_x - 1, zero_y, '0')
- ary
- end
- def check
- valid_row = '123456789' + ('A'..'Z').to_a.join
- str = self.flatten.join
- if str[-1] == '0'
- str.chop!
- else
- return false
- end
- (0...str.length).all? do |i|
- str[i] == '=' || str[i] == valid_row[i]
- end
- end
- def check_top
- valid_row = '123456789' + ('A'..'Z').to_a.join
- str = self[0].join
- (0...str.length).all? do |i|
- str[i] == '=' || str[i] == valid_row[i]
- end
- end
- def check_left
- valid_row = '123456789' + ('A'..'Z').to_a.join
- w = self[0].length
- str = map {|ary| ary[0] }.join
- (0...str.length).all? do |i|
- str[i] == '=' || str[i] == valid_row[i*w]
- end
- end
- def calc
- counter = 0
- valid_row = ('1'..'9').to_a + ('A'..'Z').to_a
- w = self[0].length
- h = self.length
- (0...h).each do |y|
- (0...w).each do |x|
- if po = find_char(valid_row.shift)
- sx = po[0]
- sy = po[1]
- counter += ((x-sx).abs + (y-sy).abs)
- end
- end
- end
- counter
- end
- def cleanup
- c = self.map {|q| q[1].calc }
- border = (c.max + c.min) / 2
- self.delete_if {|q| q[1].calc > border }
- self
- end
- end
Add Comment
Please, Sign In to add comment