fork(1) download
  1. require 'pp'
  2. require 'minitest/autorun'
  3.  
  4. F=
  5. # 200
  6. ->g,w,h{m=->y,x,d,v=[]{q=y,x
  7. r=->s{([""]*h+g)[y+h,h].map{|l|(?x*w+l)[x+w,w]}.join.count s}
  8. j=r[?>]-r[?<]
  9. k=r[?v]-r[?^]
  10. q[d=[d,1,0][j*j<=>k*k]]+=[k,j][d]<=>0
  11. v&[q<<d]!=[]?q!=v[-1]:m[*q,v<<q]}
  12. m[0,0,1]}
  13.  
  14. # Debug version
  15. # ->g,w,h{m=->y,x,d,v=[]{r=->s{g[[0,y].max...y+h].map{|l|l[[0,x].max...x+w]}.join.count s}
  16. # j=r[?>]-r[?<]
  17. # k=r[?v]-r[?^]
  18. # q=y,x
  19. # z=g.map{|l|l.dup}
  20. # z[[0,y].max...y+h].map!{|l|
  21. # r=[0,x].max...x+w
  22. # n = l
  23. # n[r]=?#*r.size
  24. # n
  25. # }
  26. # puts z
  27. # p [*q,d]
  28. # q[d=[d,1,0][j*j<=>k*k]]+=[k,j][d]<=>0
  29. # q<<d
  30. # p q
  31. # puts
  32. # v&[q]!=[]?q!=v[-1]:m[q[0],q[1],d,v<<q]}
  33. # m[0,0,1]}
  34.  
  35. describe :X do
  36. describe 'Grid 1 (2x2)' do
  37. before do
  38. @g = [
  39. '>v',
  40. '^<' ]
  41. end
  42.  
  43. it 'detects loop for 1x1' do assert F[@g, 1, 1] end
  44. it 'detects loop for 1x2' do assert F[@g, 1, 2] end
  45. it 'detects loop for 2x1' do assert F[@g, 2, 1] end
  46. it 'detects rest for 2x2' do refute F[@g, 2, 2] end
  47. end
  48.  
  49. describe 'Grid 2 (3x3)' do
  50. before do
  51. @g = [
  52. '> v',
  53. ' ',
  54. '^ <' ]
  55. end
  56.  
  57. it 'detects rest for 1x1' do refute F[@g, 1, 1] end
  58. it 'detects rest for 1x2' do refute F[@g, 1, 2] end
  59. it 'detects rest for 1x3' do refute F[@g, 1, 3] end
  60. it 'detects rest for 2x1' do refute F[@g, 2, 1] end
  61. it 'detects loop for 2x2' do assert F[@g, 2, 2] end
  62. it 'detects loop for 2x3' do assert F[@g, 2, 3] end
  63. it 'detects rest for 3x1' do refute F[@g, 3, 1] end
  64. it 'detects loop for 3x2' do assert F[@g, 3, 2] end
  65. it 'detects rest for 3x3' do refute F[@g, 3, 3] end
  66. end
  67.  
  68. describe 'Grid 3 (4x3)' do
  69. before do
  70. @g = [
  71. '>^>v',
  72. 'v^v ',
  73. '^ <<',
  74. ]
  75. end
  76.  
  77. it 'detects rest for 2x2' do refute F[@g, 2, 2] end
  78. end
  79.  
  80. describe 'Grid 4 (6x5)' do
  81. before do
  82. @g = [
  83. '>v>v>v',
  84. '^v^v^v',
  85. '^v^v^v',
  86. '^>^>^v',
  87. '^<<<<<']
  88. end
  89.  
  90. it 'detects loop for 1x1' do assert F[@g, 1, 1] end
  91. it 'detects rest for 1x2' do refute F[@g, 1, 2] end
  92. it 'detects loop for 2x1' do assert F[@g, 2, 1] end
  93. it 'detects loop for 2x2' do assert F[@g, 2, 2] end
  94. it 'detects loop for 2x4' do assert F[@g, 2, 4] end
  95. it 'detects rest for 2x5' do refute F[@g, 2, 5] end
  96. it 'detects rest for 3x1' do refute F[@g, 3, 1] end
  97. it 'detects loop for 3x2' do assert F[@g, 3, 2] end
  98. it 'detects loop for 3x3' do assert F[@g, 3, 3] end
  99. it 'detects loop for 3x5' do assert F[@g, 3, 5] end
  100. it 'detects rest for 6x2' do refute F[@g, 6, 2] end
  101. it 'detects loop for 6x3' do assert F[@g, 6, 3] end
  102. it 'detects rest for 6x5' do refute F[@g, 6, 5] end
  103. end
  104.  
  105. describe 'Grid 5 (10x6)' do
  106. before do
  107. @g = [
  108. '> <vv <',
  109. ' v ^ >v v ',
  110. ' >v^^>vv^',
  111. ' ^>^ v ',
  112. '> v<v >>',
  113. ' >v v<^ ']
  114. end
  115.  
  116. it 'detects rest for 1x1' do refute F[@g, 1, 1] end
  117. it 'detects rest for 2x3' do refute F[@g, 2, 3] end
  118. it 'detects rest for 2x6' do refute F[@g, 2, 6] end
  119. it 'detects loop for 3x2' do assert F[@g, 3, 2] end
  120. it 'detects rest for 5x4' do refute F[@g, 5, 4] end
  121. it 'detects loop for 6x1' do assert F[@g, 6, 1] end
  122. it 'detects rest for 10x6' do refute F[@g, 10, 6] end
  123. end
  124.  
  125. describe 'Incorrect cycle detection' do
  126. before do
  127. @g = [
  128. '>>v ',
  129. ' >v ',
  130. '>> >',
  131. '^ v',
  132. '^ v ',
  133. ' <<'
  134. ]
  135. end
  136.  
  137. it 'detects rest for 2x2' do refute F[@g, 2, 2] end
  138. end
  139. end
  140.  
Success #stdin #stdout 0.14s 10864KB
stdin
Standard input is empty
stdout
Run options: --seed 20883

# Running tests:

...................................

Finished tests in 0.026640s, 1313.7927 tests/s, 1313.7927 assertions/s.

35 tests, 35 assertions, 0 failures, 0 errors, 0 skips