fork download
  1. #!/usr/bin/env ruby
  2.  
  3. def step(barrels, slaves, n_parts)
  4. hit = barrels / n_parts
  5. miss = barrels - ((barrels / [slaves, n_parts].min))
  6. return [{ barrels: hit, slaves: slaves - 1 },
  7. { barrels: miss, slaves: slaves }]
  8. end
  9.  
  10. results = { 0 => [{ barrels: 250, slaves: 4, steps: [] }] }
  11. (1..3).each do |step|
  12. results[step] = []
  13. results[step - 1].each do |res|
  14. (2..10).each do |n|
  15. hit, miss = step(res[:barrels], res[:slaves], n)
  16. steps = [*res[:steps], n]
  17. hit[:steps] = steps
  18. miss[:steps] = steps
  19. results[step] << hit
  20. results[step] << miss
  21. end
  22. end
  23. end
  24.  
  25. results[3].sort_by { |r| r[:steps] }.each_slice(8) { |res| p res if res.map { |r| r[:barrels] }.inject(:+) <= 8 }
Success #stdin #stdout 0.1s 8520KB
stdin
Standard input is empty
stdout
Standard output is empty