fork download
  1. # -*- coding: utf-8 -*-
  2. require 'minitest/autorun'
  3.  
  4. BuildTrack =->{
  5.  
  6. # Program begins here ----------
  7.  
  8. _={│:[1,4],─:[2,8],┌:[4,8],┐:[4,2],└:[1,8],┘:[1,2],┼:[1,4,2,8]}
  9. s=->a,l,b{l==[]&&a==[]?b:(l.product(l).any?{|q,r|q,r=q[0],r[0];(q[0]-r[0])**2+(q[1]-r[1])**2>a.size**2}?!0:(w,f=l.pop
  10. w&&v=!a.size.times{|i|y=_[x=a[i]]
  11. f&&y&[f]==[]||(k=l.select{|p,d|w!=p||y&[d]==[]}
  12. (y-[f]).map{|d|z=[w[0]+(d<2?-1:(d&4)/4),w[1]+(d==2?-1:d>7?1:0)]
  13. g=d<3?d*4:d/4
  14. b[z]?_[b[z]]&[g]!=[]||v=0:k<<[z,g]}
  15. v||r=s[a[0...i]+a[i+1..-1],k,b.merge({w=>x})]
  16. return r if r)}))}
  17. c=eval"[#{gets}]"
  18. r=s[6.downto(0).map{|i|[_.keys[i]]*c[i]}.flatten,[[[0,0],nil]],{}]
  19. h=j=k=l=0
  20. r.map{|w,_|y,x=w
  21. h>x&&h=x
  22. j>y&&j=y
  23. k<x&&k=x
  24. l<y&&l=y}
  25. s=(j..l).map{|_|' '*(k-h+1)}
  26. r.map{|w,p|y,x=w
  27. s[y-j][x-h]=p.to_s}
  28. puts s
  29.  
  30. # Program ends here ----------
  31.  
  32. }
  33.  
  34. describe BuildTrack do
  35. # takes ~0.1s
  36. def test_very_simple_square
  37. assert_equal <<-EOS, execute("2,2,1,1,1,1,0\n")
  38. ┌─┐
  39. │ │
  40. └─┘
  41. EOS
  42. end
  43.  
  44. # takes ~0.6s
  45. def test_added_more_vertical_pieces
  46. assert_equal <<-EOS, execute("6,2,1,1,1,1,0\n")
  47. ┌─┐
  48. │ │
  49. │ │
  50. │ │
  51. └─┘
  52. EOS
  53. end
  54.  
  55. # takes ~0.0s
  56. def test_throwing_cross_pieces_into_the_mix
  57. assert_equal <<-EOS, execute("0,0,2,1,1,2,1\n")
  58. ┌┐
  59. ┌┼┘
  60. └┘
  61. EOS
  62. end
  63.  
  64. # takes ~0.4s
  65. def test_longer_track_containing_a_cross_piece
  66. assert_equal <<-EOS, execute("4,4,2,1,1,2,1\n")
  67. ┌┐
  68. ┌──┼┘
  69. │ │
  70. │ │
  71. └──┘
  72. EOS
  73. end
  74.  
  75. # remove the leading "x_"s to run these long lasting tests
  76.  
  77. # takes ~34s
  78. def x_test_OP_testcase_1
  79. assert_equal <<-EOS, execute("3,5,2,2,2,2,1\n")
  80. ┌──┐┌┐
  81. │ └┼┘
  82. │ │
  83. └───┘
  84. EOS
  85. end
  86.  
  87. # takes ~2s
  88. def x_test_OP_testcase_2
  89. assert_equal <<-EOS, execute("0,0,1,4,4,1,3\n")
  90. ┌┐
  91. └┼┐
  92. └┼┐
  93. └┼┐
  94. └┘
  95. EOS
  96. end
  97.  
  98.  
  99. def execute(input)
  100. capture_stdout(input){ BuildTrack[] }
  101. end
  102. end
  103.  
  104.  
  105. # capture STDIN/STDOUT for testing purposes
  106. require 'stringio'
  107. module Kernel
  108. def capture_stdout(console_input = '')
  109. $stdin = StringIO.new(console_input)
  110. out = StringIO.new
  111. out.set_encoding 'utf-8'
  112. $stdout = out
  113. yield
  114. return out.string
  115. ensure
  116. $stdout = STDOUT
  117. $stdin = STDIN
  118. end
  119. end
  120.  
Success #stdin #stdout 2s 8736KB
stdin
Standard input is empty
stdout
Run options: --seed 35090

# Running tests:

....

Finished tests in 1.942423s, 2.0593 tests/s, 2.0593 assertions/s.

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