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

# Running tests:

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

Finished tests in 0.025244s, 1386.4893 tests/s, 1386.4893 assertions/s.

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