fork download
  1. #! ruby -Ku
  2.  
  3. def back(s)
  4. return [0, ''] if s.empty?
  5. (P - s).inject([Float::INFINITY, nil]) do |min, x|
  6. t, w = go(s + [x])
  7. t += @t[x]
  8. t < min[0] ? [t, x + '← ' + w] : min
  9. end
  10. end
  11.  
  12. def go(s)
  13. s.combination(2).inject([Float::INFINITY, nil]) do |min, pair|
  14. t, w = back(s - pair)
  15. t += pair.map{|x| @t[x]}.max
  16. t < min[0] ? [t, pair.join + '→ ' + w] : min
  17. end
  18. end
  19.  
  20. P = ('A'..'D').to_a
  21. @t = Hash[*P.zip([1, 2, 5, 10]).flatten]
  22. puts (result = go(P))[1]
  23. print result[0], 'minutes'
Success #stdin #stdout 0.01s 7476KB
stdin
Standard input is empty
stdout
AB→ A← CD→ B← AB→ 
17minutes