#!/usr/bin/env ruby
def step(barrels, slaves, n_parts)
hit = barrels / n_parts
miss = barrels - ((barrels / [slaves, n_parts].min))
return [{ barrels: hit, slaves: slaves - 1 },
{ barrels: miss, slaves: slaves }]
end
results = { 0 => [{ barrels: 250, slaves: 4, steps: [] }] }
(1..3).each do |step|
results[step] = []
results[step - 1].each do |res|
(2..10).each do |n|
hit, miss = step(res[:barrels], res[:slaves], n)
steps = [*res[:steps], n]
hit[:steps] = steps
miss[:steps] = steps
results[step] << hit
results[step] << miss
end
end
end
results[3].sort_by { |r| r[:steps] }.each_slice(8) { |res| p res if res.map { |r| r[:barrels] }.inject(:+) <= 8 }
IyEvdXNyL2Jpbi9lbnYgcnVieQoKZGVmIHN0ZXAoYmFycmVscywgc2xhdmVzLCBuX3BhcnRzKQogIGhpdCA9IGJhcnJlbHMgLyBuX3BhcnRzCiAgbWlzcyA9IGJhcnJlbHMgLSAoKGJhcnJlbHMgLyBbc2xhdmVzLCBuX3BhcnRzXS5taW4pKQogIHJldHVybiBbeyBiYXJyZWxzOiBoaXQsIHNsYXZlczogc2xhdmVzIC0gMSB9LAogICAgICAgICAgeyBiYXJyZWxzOiBtaXNzLCBzbGF2ZXM6IHNsYXZlcyB9XQplbmQKCnJlc3VsdHMgPSB7IDAgPT4gW3sgYmFycmVsczogMjUwLCBzbGF2ZXM6IDQsIHN0ZXBzOiBbXSB9XSB9CigxLi4zKS5lYWNoIGRvIHxzdGVwfAogIHJlc3VsdHNbc3RlcF0gPSBbXQogIHJlc3VsdHNbc3RlcCAtIDFdLmVhY2ggZG8gfHJlc3wKICAgICgyLi4xMCkuZWFjaCBkbyB8bnwKICAgICAgaGl0LCBtaXNzID0gc3RlcChyZXNbOmJhcnJlbHNdLCByZXNbOnNsYXZlc10sIG4pCiAgICAgIHN0ZXBzID0gWypyZXNbOnN0ZXBzXSwgbl0KICAgICAgaGl0WzpzdGVwc10gPSBzdGVwcwogICAgICBtaXNzWzpzdGVwc10gPSBzdGVwcwogICAgICByZXN1bHRzW3N0ZXBdIDw8IGhpdAogICAgICByZXN1bHRzW3N0ZXBdIDw8IG1pc3MKICAgIGVuZAogIGVuZAplbmQKCnJlc3VsdHNbM10uc29ydF9ieSB7IHxyfCByWzpzdGVwc10gfS5lYWNoX3NsaWNlKDgpIHsgfHJlc3wgcCByZXMgaWYgcmVzLm1hcCB7IHxyfCByWzpiYXJyZWxzXSB9LmluamVjdCg6KykgPD0gOCB9