Guest User

Untitled

a guest
Jan 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. class Array
  3.  
  4. def point(x, y)
  5. self[y][x]
  6. end
  7.  
  8. def set_point(x, y, char)
  9. self[y][x] = char
  10. self
  11. end
  12.  
  13. def find_zero
  14. each_with_index do |ary, y|
  15. x = ary.find_index '0'
  16. if x
  17. return [x, y]
  18. end
  19. end
  20. end
  21.  
  22. def find_char(char)
  23. each_with_index do |ary, y|
  24. x = ary.find_index char
  25. if x
  26. return [x, y]
  27. end
  28. end
  29. nil
  30. end
  31.  
  32. def up
  33. zero_x, zero_y = find_zero
  34. return false if zero_y == 0
  35. char = point(zero_x, zero_y - 1)
  36. return false if char == '='
  37. ary = Marshal.load(Marshal.dump(self))
  38. ary.set_point(zero_x, zero_y, char)
  39. ary.set_point(zero_x, zero_y - 1, '0')
  40. ary
  41. end
  42.  
  43. def down
  44. zero_x, zero_y = find_zero
  45. return false if zero_y >= self.size - 1
  46. char = point(zero_x, zero_y + 1)
  47. return false if char == '='
  48. ary = Marshal.load(Marshal.dump(self))
  49. ary.set_point(zero_x, zero_y, char)
  50. ary.set_point(zero_x, zero_y + 1, '0')
  51. ary
  52. end
  53.  
  54. def right
  55. zero_x, zero_y = find_zero
  56. return false if zero_x >= self.first.size - 1
  57. char = point(zero_x + 1, zero_y)
  58. return false if char == '='
  59. ary = Marshal.load(Marshal.dump(self))
  60. ary.set_point(zero_x, zero_y, char)
  61. ary.set_point(zero_x + 1, zero_y, '0')
  62. ary
  63. end
  64.  
  65. def left
  66. zero_x, zero_y = find_zero
  67. return false if zero_x == 0
  68. char = point(zero_x - 1, zero_y)
  69. return false if char == '='
  70. ary = Marshal.load(Marshal.dump(self))
  71. ary.set_point(zero_x, zero_y, char)
  72. ary.set_point(zero_x - 1, zero_y, '0')
  73. ary
  74. end
  75.  
  76. def check
  77. valid_row = '123456789' + ('A'..'Z').to_a.join
  78. str = self.flatten.join
  79. if str[-1] == '0'
  80. str.chop!
  81. else
  82. return false
  83. end
  84.  
  85. (0...str.length).all? do |i|
  86. str[i] == '=' || str[i] == valid_row[i]
  87. end
  88. end
  89.  
  90. def check_top
  91. valid_row = '123456789' + ('A'..'Z').to_a.join
  92. str = self[0].join
  93. (0...str.length).all? do |i|
  94. str[i] == '=' || str[i] == valid_row[i]
  95. end
  96. end
  97.  
  98. def check_left
  99. valid_row = '123456789' + ('A'..'Z').to_a.join
  100. w = self[0].length
  101. str = map {|ary| ary[0] }.join
  102. (0...str.length).all? do |i|
  103. str[i] == '=' || str[i] == valid_row[i*w]
  104. end
  105. end
  106.  
  107. def calc
  108. counter = 0
  109. valid_row = ('1'..'9').to_a + ('A'..'Z').to_a
  110. w = self[0].length
  111. h = self.length
  112. (0...h).each do |y|
  113. (0...w).each do |x|
  114. if po = find_char(valid_row.shift)
  115. sx = po[0]
  116. sy = po[1]
  117. counter += ((x-sx).abs + (y-sy).abs)
  118. end
  119. end
  120. end
  121. counter
  122. end
  123.  
  124. def cleanup
  125. c = self.map {|q| q[1].calc }
  126. border = (c.max + c.min) / 2
  127. self.delete_if {|q| q[1].calc > border }
  128. self
  129. end
  130.  
  131. end
Add Comment
Please, Sign In to add comment