fork download
  1. # http://c...content-available-to-author-only...e.com/questions/47229/water-held-in-a-hexagonal-r-scuplture
  2. require 'minitest/autorun'
  3.  
  4. # The program is wrapped in a function, in order for it to be testable.
  5. # `ii` is used instead of `$<` to pass the input
  6. # (because `$<` cannot be mocked in tests and `ii` is also 2 chars long)
  7. f=->ii{
  8.  
  9. # -- program begin --
  10. s={}
  11. l=ii.lines
  12. y=0
  13. l.map{|r|x=0
  14. r.scan(/../){s[[x,y]]=[v=$&.to_i,v<1?0:99];x+=1}
  15. y+=1}
  16. loop{break if s.map{|c,r|x,y=c
  17. m = [[-1,-1],[1,-1],[-2,0],[2,0],[1,-1],[1,1]].map{|w,z|s[[x+w,y+z]]}.map{|n|n ?n[0]+n[1]:0}.min
  18. r[1]=[0,m-r[0]].max if r[0]+r[1]>m&&r[1]>0}.none?}
  19. puts s.map{|c,r|r[1]}.reduce :+
  20. # -- program end --
  21.  
  22. # return the result, for testing
  23. s.map{|c,r|r[1]}.reduce :+
  24. }
  25.  
  26.  
  27. describe '#f' do
  28.  
  29. it 'passes test case #1' do
  30. i = <<-EOS
  31. 04 04
  32. 04 01 03
  33. 04 04
  34. EOS
  35.  
  36. assert_equal 2, f[i]
  37. end
  38.  
  39. it 'passes test case #2' do
  40. i = <<-EOS
  41. 55 34 45 66
  42. 33 21 27
  43. 23 12 01 77
  44. 36 31 74
  45. EOS
  46.  
  47. assert_equal 35, f[i]
  48. end
  49.  
  50.  
  51. it 'passes test case #3' do
  52. i = <<-EOS
  53. 35 36 77 22 23 32 54 24
  54. 33 07 02 04 21 54 07 07 07 76
  55. 20 04 07 07 01 20 54 11 81 81 07 76
  56. 20 67 67 22 07 01 78 54 07 81 07 81 09 76
  57. 20 67 07 67 22 22 07 44 55 54 07 81 07 07 61 07 20
  58. 67 57 50 50 07 07 14 03 02 15 81 99 91 07 81 04
  59. 67 07 50 50 87 39 45 41 34 81 07 07 89 07 81 79
  60. 67 07 50 50 07 07 07 27 07 27 81 07 07 79 81 78
  61. 20 67 67 07 07 07 07 99 33 46 02 81 07 07 81 01 20
  62. 33 07 07 01 05 01 92 20 02 81 07 81 15 32
  63. 22 07 20 20 07 20 63 02 80 81 15 32
  64. 45 20 01 20 39 20 15 07 15 32
  65. 23 20 20 29 43 21 18 41 20 66 66 43 21
  66. 90 99 47 07 20
  67. 50 20 02 48
  68. 70 56 20
  69. 90
  70. EOS
  71.  
  72. assert_equal 1432, f[i]
  73. end
  74. end
  75.  
Success #stdin #stdout 0.12s 10712KB
stdin
Standard input is empty
stdout
Run options: --seed 52011

# Running tests:

35
.1432
.2
.

Finished tests in 0.086488s, 34.6868 tests/s, 34.6868 assertions/s.

3 tests, 3 assertions, 0 failures, 0 errors, 0 skips