fork download
  1.  
  2. DRUNKEN_BISHOP=
  3. ->key{
  4. get_char_at_pos=->x{
  5. middle = 4 * 17 + 8
  6.  
  7. return 'S' if x == middle
  8.  
  9. current_location = middle
  10.  
  11. step_count_at_location_x = 0
  12.  
  13. key.split(?:).flat_map{|b|(0..7).map{|i|b.to_i(16)[i]}}.each_slice(2) do |horz, vert|
  14. if vert == 0 && current_location > 17
  15. current_location -= 17
  16. end
  17. if vert == 1 && current_location < 17 * 9 - 17
  18. current_location += 17
  19. end
  20.  
  21. if horz == 0 && current_location % 17 > 0
  22. current_location -= 1
  23. end
  24. if horz == 1 && current_location % 17 < 16
  25. current_location += 1
  26. end
  27.  
  28. step_count_at_location_x += 1 if current_location == x
  29. end
  30.  
  31. if current_location == x
  32. return 'E'
  33. else
  34. return ' .o+=*BOX@%&#/^'[step_count_at_location_x]
  35. end
  36. }
  37.  
  38. r=[z=?++?-*17+?+]
  39.  
  40. (0...17*9).each_slice(17).map do |row|
  41. r << ?|+row.map(&get_char_at_pos).join+?|
  42. end
  43. (r+[z]).join ?\n
  44. }
  45.  
  46.  
  47. require 'minitest/autorun'
  48.  
  49. describe DRUNKEN_BISHOP do
  50. def test_case_1
  51. assert_equal <<-EOS.chomp, DRUNKEN_BISHOP['37:e4:6a:2d:48:38:1a:0a:f3:72:6d:d9:17:6b:bd:5e']
  52. +-----------------+
  53. | |
  54. | |
  55. | . |
  56. | . o |
  57. |o . o . S + |
  58. |.+ + = . B . |
  59. |o + + o B o E |
  60. | o . + . o |
  61. | .o |
  62. +-----------------+
  63. EOS
  64. end
  65.  
  66. def test_case_2
  67. assert_equal <<-EOS.chomp, DRUNKEN_BISHOP['16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48']
  68. +-----------------+
  69. | . |
  70. | + . |
  71. | . B . |
  72. | o * + |
  73. | X * S |
  74. | + O o . . |
  75. | . E . o |
  76. | . . o |
  77. | . . |
  78. +-----------------+
  79. EOS
  80. end
  81.  
  82. def test_case_3
  83. assert_equal <<-EOS.chomp, DRUNKEN_BISHOP['b6:dd:b7:1f:bc:25:31:d3:12:f4:92:1c:0b:93:5f:4b']
  84. +-----------------+
  85. | o.o |
  86. | .= E.|
  87. | .B.o|
  88. | .= |
  89. | S = .|
  90. | . o . .= |
  91. | . . . oo.|
  92. | . o+|
  93. | .o.|
  94. +-----------------+
  95. EOS
  96. end
  97.  
  98. def test_case_4
  99. assert_equal <<-EOS.chomp, DRUNKEN_BISHOP['05:1e:1e:c1:ac:b9:d1:1c:6a:60:ce:0f:77:6c:78:47']
  100. +-----------------+
  101. | o=. |
  102. | o o++E |
  103. | + . Ooo. |
  104. | + O B.. |
  105. | = *S. |
  106. | o |
  107. | |
  108. | |
  109. | |
  110. +-----------------+
  111. EOS
  112. end
  113. end
  114.  
  115.  
Success #stdin #stdout 0.22s 10744KB
stdin
Standard input is empty
stdout
Run options: --seed 34983

# Running tests:

....

Finished tests in 0.104810s, 38.1643 tests/s, 38.1643 assertions/s.

4 tests, 4 assertions, 0 failures, 0 errors, 0 skips