21
6
Your mission is to build an algorithm (program or function) that can optimize packing fruit from a conveyor belt into bags to be sent off to retailers, optimizing for a largest number of bags.
Each bag has to weight at least a certain amount, but any excess is lost profit since that weight could be used to fill another bag. Your bagging machine has always a lookahead of n
fruits from the queue and may only choose to add any of these n
fruits to the (single) bag that is being processed. It cannot look beyond the n
first elements in the queue. The program always knows exactly how much weight there already is in the bag.
Another way to visualize this is having a conveyor belt with a loading area of size n
at the end, from where a fruit has to be taken before a new fruit arrives. Any leftover fruit and a non-full bag at the end are discarded.
Inputs
- List/array of weights of fruits in queue (positive integers)
- Minimum total weight for bags (positive integer)
- Lookahead
n
(positive integer)
Output
Your algorithm should return for all bags the weights of the fruits in them, by whatever means is convenient for you and your language, be that stdin or a return value or something else. You should be able to run the program and calculate your score in one minute on your computer.
Example
Total weight 1000, lookahead of 3 and fruit queue:
[171,163,172,196,156,175,162,176,155,182,189,142,161,160,152,162,174,172,191,185]
One possible output (indented to show how the lookahead affects the bagging):
[171,163,172, 156,175, 176]
[162, 155,182,189, 161,160]
[152,162,174,172,191,185]
Scoring
Your algorithm will be tested on six runs on a batch of 10000 oranges I have prepared for you, on lookaheads ranging from 2 to 7, inclusive on both ends. You shall pack them into bags weighing at least 1000 units. The oranges are normally distributed with a mean weight of 170 and a standard deviation of 13, if that is of any help.
Your score will be the sum of number of bags from the six runs. The highest score wins. Standard loopholes are disallowed.
Simple example implementation and test suite boilerplate in Haskell
7Come on people, I think there are still some low hanging fruit algorithms still waiting to be picked… – Angs – 2018-04-18T07:31:53.353
2Can programs hardcode the mean weight / distribution? (assume that it works equally well on similar batches, of course hardcoding everything is invalid as it destroys the purpose of limited lookahead) – user202729 – 2018-04-18T11:17:15.123
@user202729: Yes they can. – Angs – 2018-04-18T11:18:44.870
And hardcoding everything is a forbidden standard loophole anyway.
– Angs – 2018-04-18T12:05:32.800I can't see what lookhead is – l4m2 – 2018-04-18T19:09:49.110
Also, if no time limit, no length limit, and question is clear, everyone write brute-force for best solution – l4m2 – 2018-04-18T19:11:04.090
@l4m2 since optimizing for the given test case is forbidden, brute-forcing a best solution is impossible. Basically this means it should work as well / properly if the test set is shuffled. The algorithm can only act on the first
n
items in the queue. – Angs – 2018-04-18T19:20:44.983@Angs Suggest a reasonable time requirement as it's not default – l4m2 – 2018-04-18T19:26:41.970
@l4m2 there are only 10000 test cases with always at most seven items to choose from - I can't imagine how it could take more than a few seconds but let's say a minute to be safe. – Angs – 2018-04-18T19:34:09.260
A bounty is maybe too soon? – user202729 – 2018-04-19T01:51:16.593
Now I'm more confused. What exactly is limited to 256 bits? How exactly are we supposed to return if the return value doesn't fit in memory? – user202729 – 2018-04-19T09:32:57.417
@user202729: You have a bag that weights
b
. You have to fill the bag up tot
. You haven
items with known weight and more coming. Which of then
items to choose next? That's the problem. – Angs – 2018-04-19T10:26:59.873So the algorithm can only process one bag at a time? – user202729 – 2018-04-19T10:36:59.103
@user202729 yes, I'll add that clarification – Angs – 2018-04-19T10:46:10.677
I think this problem would be more interesting with a lookahead of 10 or 20. – qwr – 2018-04-21T07:08:12.360
@qwr There are tradeoffs I suppose. My reasoning was that since a bag usually holds just six items, there are diminishing returns with larger lookaheads and being a model of a physical process, larger values require more space and are more error-prone. The smaller ones put on emphasis on how to guess what's coming, whereas larger lookaheads would probably favor brute-forcing all combinations (to a point of course, as it's exponential). At that point it becomes more about time constraints than the algorithm. – Angs – 2018-04-21T07:55:40.500
Or in other words: work smarter, not harder. – Angs – 2018-04-21T07:57:30.640
Anyway it is a fun problem and there's many interesting variations: 2 fruits with different distributions, k such fruits, ability to recycle some of wasted fruit into juice, etc. – qwr – 2018-04-21T16:46:36.560