Heavy Box StackingFind the optimal set of weights to add to a certain set of weightsDraw Growing Stacks of BoxesImplode the BoxStack ExchangingAutomatic box expanderCode golf ABC's: The ASCII Box ChallengeMake An ASCII Poker Chip Stack ArrangementExplode the BoxA pile of weights
Under GDPR, can I give permission once to allow everyone to store and process my data?
Calculate Landau's function
How do I get my neighbour to stop disturbing with loud music?
Cheap oscilloscope showing 16 MHz square wave
IList<T> implementation
Can authors email you PDFs of their textbook for free?
Could a complex system of reaction wheels be used to propel a spacecraft?
What was Captain Marvel supposed to do once she reached her destination?
In Endgame, wouldn't Stark have remembered Hulk busting out of the stairwell?
Why does the U.S. military maintain their own weather satellites?
New coworker has strange workplace requirements - how should I deal with them?
Resources to learn about firearms?
When did coal replace firewood in early America?
Storing milk for long periods of time
How to differentiate between two people with the same name in a story?
Is it good practice to speed up and slow down where not written in a song?
How did medieval manors handle population growth? Was there room for more fields to be ploughed?
Is Borg adaptation only temporary?
Journal published a paper, ignoring my objections as a referee
Why haven't the British protested Brexit as ardently like Hong Kongers protest?
What is this "opened" cube called?
Ask one verbal question to figure out who is blind and who is mute among three persons
math mode in ticks ( tikzpicture )
Can inductive kick be discharged without freewheeling diode, in this example?
Heavy Box Stacking
Find the optimal set of weights to add to a certain set of weightsDraw Growing Stacks of BoxesImplode the BoxStack ExchangingAutomatic box expanderCode golf ABC's: The ASCII Box ChallengeMake An ASCII Poker Chip Stack ArrangementExplode the BoxA pile of weights
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
You have a bunch of heavy boxes and you want to stack them in the fewest number of stacks possible. The issue is that you can't stack more boxes on a box than it can support, so heavier boxes must go on the bottom of a stack.
The Challenge
Input: A list of weights of boxes, in whole kg.
Output: A list of lists describing the stacks of boxes. This must use the fewest number of stacks possible for the input. To be a valid stack, the weight of each box in the stack must be greater than or equal to the sum of the weight of all boxes above it.
Examples of Valid stacks
(In bottom to top order)
- [3]
- [1, 1]
- [3, 2, 1]
- [4, 2, 1, 1]
- [27, 17, 6, 3, 1]
- [33, 32, 1]
- [999, 888, 99, 11, 1]
Examples of Invalid stacks
(In order from bottom to top)
- [1, 2]
- [3, 3, 3]
- [5, 5, 1]
- [999, 888, 777]
- [4, 3, 2]
- [4321, 3000, 1234, 321]
Example Test Cases
1
IN: [1, 2, 3, 4, 5, 6, 9, 12]
OUT: [[12, 6, 3, 2, 1], [9, 5, 4]]
2
IN: [87, 432, 9999, 1234, 3030]
OUT: [[9999, 3030, 1234, 432, 87]]
3
IN: [1, 5, 3, 1, 4, 2, 1, 6, 1, 7, 2, 3]
OUT: [[6, 3, 2, 1], [7, 4, 2, 1], [5, 3, 1, 1]]
Rules and Assumptions
- Standard I/O rules and banned loopholes apply
- Use any convenient format for I/O
- Stacks may be described top to bottom or bottom to top, as long as you are consistent.
- The order of stacks (rather than boxes within those stacks) does not matter.
- If there is more than one optimal configuration of stacks, you may output any one of them
- You may assume that there is at least one box and that all boxes weigh at least 1 kg
- You must support weights up to 9,999 kg, at minimum.
- You must support up to 9,999 total boxes, at minimum.
- Boxes with the same weight are indistinguishable, so there is no need to annotate which box was used where.
Happy golfing! Good luck!
code-golf optimization
$endgroup$
add a comment |
$begingroup$
You have a bunch of heavy boxes and you want to stack them in the fewest number of stacks possible. The issue is that you can't stack more boxes on a box than it can support, so heavier boxes must go on the bottom of a stack.
The Challenge
Input: A list of weights of boxes, in whole kg.
Output: A list of lists describing the stacks of boxes. This must use the fewest number of stacks possible for the input. To be a valid stack, the weight of each box in the stack must be greater than or equal to the sum of the weight of all boxes above it.
Examples of Valid stacks
(In bottom to top order)
- [3]
- [1, 1]
- [3, 2, 1]
- [4, 2, 1, 1]
- [27, 17, 6, 3, 1]
- [33, 32, 1]
- [999, 888, 99, 11, 1]
Examples of Invalid stacks
(In order from bottom to top)
- [1, 2]
- [3, 3, 3]
- [5, 5, 1]
- [999, 888, 777]
- [4, 3, 2]
- [4321, 3000, 1234, 321]
Example Test Cases
1
IN: [1, 2, 3, 4, 5, 6, 9, 12]
OUT: [[12, 6, 3, 2, 1], [9, 5, 4]]
2
IN: [87, 432, 9999, 1234, 3030]
OUT: [[9999, 3030, 1234, 432, 87]]
3
IN: [1, 5, 3, 1, 4, 2, 1, 6, 1, 7, 2, 3]
OUT: [[6, 3, 2, 1], [7, 4, 2, 1], [5, 3, 1, 1]]
Rules and Assumptions
- Standard I/O rules and banned loopholes apply
- Use any convenient format for I/O
- Stacks may be described top to bottom or bottom to top, as long as you are consistent.
- The order of stacks (rather than boxes within those stacks) does not matter.
- If there is more than one optimal configuration of stacks, you may output any one of them
- You may assume that there is at least one box and that all boxes weigh at least 1 kg
- You must support weights up to 9,999 kg, at minimum.
- You must support up to 9,999 total boxes, at minimum.
- Boxes with the same weight are indistinguishable, so there is no need to annotate which box was used where.
Happy golfing! Good luck!
code-golf optimization
$endgroup$
$begingroup$
May we take the weights in sorted order? (either ascending or descending)
$endgroup$
– Arnauld
8 hours ago
$begingroup$
"You must support up to 9,999 total boxes, at minimum." How is "support" interpreted here? Does it merely mean the program should be able to take such size of input, or does it mean that the program should actually provide the answer in a reasonable amount of time? If it is the latter, there should be much larger test cases provided.
$endgroup$
– Joel
7 hours ago
$begingroup$
Suggested test case:[8, 8, 8, 5, 1]
->[[8, 8], [8, 5, 1]]
$endgroup$
– Hiatsu
5 hours ago
1
$begingroup$
Or even better:[8, 5, 8, 8, 1, 2]
->[[8, 8], [8, 5, 2, 1]]
$endgroup$
– Hiatsu
5 hours ago
add a comment |
$begingroup$
You have a bunch of heavy boxes and you want to stack them in the fewest number of stacks possible. The issue is that you can't stack more boxes on a box than it can support, so heavier boxes must go on the bottom of a stack.
The Challenge
Input: A list of weights of boxes, in whole kg.
Output: A list of lists describing the stacks of boxes. This must use the fewest number of stacks possible for the input. To be a valid stack, the weight of each box in the stack must be greater than or equal to the sum of the weight of all boxes above it.
Examples of Valid stacks
(In bottom to top order)
- [3]
- [1, 1]
- [3, 2, 1]
- [4, 2, 1, 1]
- [27, 17, 6, 3, 1]
- [33, 32, 1]
- [999, 888, 99, 11, 1]
Examples of Invalid stacks
(In order from bottom to top)
- [1, 2]
- [3, 3, 3]
- [5, 5, 1]
- [999, 888, 777]
- [4, 3, 2]
- [4321, 3000, 1234, 321]
Example Test Cases
1
IN: [1, 2, 3, 4, 5, 6, 9, 12]
OUT: [[12, 6, 3, 2, 1], [9, 5, 4]]
2
IN: [87, 432, 9999, 1234, 3030]
OUT: [[9999, 3030, 1234, 432, 87]]
3
IN: [1, 5, 3, 1, 4, 2, 1, 6, 1, 7, 2, 3]
OUT: [[6, 3, 2, 1], [7, 4, 2, 1], [5, 3, 1, 1]]
Rules and Assumptions
- Standard I/O rules and banned loopholes apply
- Use any convenient format for I/O
- Stacks may be described top to bottom or bottom to top, as long as you are consistent.
- The order of stacks (rather than boxes within those stacks) does not matter.
- If there is more than one optimal configuration of stacks, you may output any one of them
- You may assume that there is at least one box and that all boxes weigh at least 1 kg
- You must support weights up to 9,999 kg, at minimum.
- You must support up to 9,999 total boxes, at minimum.
- Boxes with the same weight are indistinguishable, so there is no need to annotate which box was used where.
Happy golfing! Good luck!
code-golf optimization
$endgroup$
You have a bunch of heavy boxes and you want to stack them in the fewest number of stacks possible. The issue is that you can't stack more boxes on a box than it can support, so heavier boxes must go on the bottom of a stack.
The Challenge
Input: A list of weights of boxes, in whole kg.
Output: A list of lists describing the stacks of boxes. This must use the fewest number of stacks possible for the input. To be a valid stack, the weight of each box in the stack must be greater than or equal to the sum of the weight of all boxes above it.
Examples of Valid stacks
(In bottom to top order)
- [3]
- [1, 1]
- [3, 2, 1]
- [4, 2, 1, 1]
- [27, 17, 6, 3, 1]
- [33, 32, 1]
- [999, 888, 99, 11, 1]
Examples of Invalid stacks
(In order from bottom to top)
- [1, 2]
- [3, 3, 3]
- [5, 5, 1]
- [999, 888, 777]
- [4, 3, 2]
- [4321, 3000, 1234, 321]
Example Test Cases
1
IN: [1, 2, 3, 4, 5, 6, 9, 12]
OUT: [[12, 6, 3, 2, 1], [9, 5, 4]]
2
IN: [87, 432, 9999, 1234, 3030]
OUT: [[9999, 3030, 1234, 432, 87]]
3
IN: [1, 5, 3, 1, 4, 2, 1, 6, 1, 7, 2, 3]
OUT: [[6, 3, 2, 1], [7, 4, 2, 1], [5, 3, 1, 1]]
Rules and Assumptions
- Standard I/O rules and banned loopholes apply
- Use any convenient format for I/O
- Stacks may be described top to bottom or bottom to top, as long as you are consistent.
- The order of stacks (rather than boxes within those stacks) does not matter.
- If there is more than one optimal configuration of stacks, you may output any one of them
- You may assume that there is at least one box and that all boxes weigh at least 1 kg
- You must support weights up to 9,999 kg, at minimum.
- You must support up to 9,999 total boxes, at minimum.
- Boxes with the same weight are indistinguishable, so there is no need to annotate which box was used where.
Happy golfing! Good luck!
code-golf optimization
code-golf optimization
asked 9 hours ago
BeefsterBeefster
3,15118 silver badges57 bronze badges
3,15118 silver badges57 bronze badges
$begingroup$
May we take the weights in sorted order? (either ascending or descending)
$endgroup$
– Arnauld
8 hours ago
$begingroup$
"You must support up to 9,999 total boxes, at minimum." How is "support" interpreted here? Does it merely mean the program should be able to take such size of input, or does it mean that the program should actually provide the answer in a reasonable amount of time? If it is the latter, there should be much larger test cases provided.
$endgroup$
– Joel
7 hours ago
$begingroup$
Suggested test case:[8, 8, 8, 5, 1]
->[[8, 8], [8, 5, 1]]
$endgroup$
– Hiatsu
5 hours ago
1
$begingroup$
Or even better:[8, 5, 8, 8, 1, 2]
->[[8, 8], [8, 5, 2, 1]]
$endgroup$
– Hiatsu
5 hours ago
add a comment |
$begingroup$
May we take the weights in sorted order? (either ascending or descending)
$endgroup$
– Arnauld
8 hours ago
$begingroup$
"You must support up to 9,999 total boxes, at minimum." How is "support" interpreted here? Does it merely mean the program should be able to take such size of input, or does it mean that the program should actually provide the answer in a reasonable amount of time? If it is the latter, there should be much larger test cases provided.
$endgroup$
– Joel
7 hours ago
$begingroup$
Suggested test case:[8, 8, 8, 5, 1]
->[[8, 8], [8, 5, 1]]
$endgroup$
– Hiatsu
5 hours ago
1
$begingroup$
Or even better:[8, 5, 8, 8, 1, 2]
->[[8, 8], [8, 5, 2, 1]]
$endgroup$
– Hiatsu
5 hours ago
$begingroup$
May we take the weights in sorted order? (either ascending or descending)
$endgroup$
– Arnauld
8 hours ago
$begingroup$
May we take the weights in sorted order? (either ascending or descending)
$endgroup$
– Arnauld
8 hours ago
$begingroup$
"You must support up to 9,999 total boxes, at minimum." How is "support" interpreted here? Does it merely mean the program should be able to take such size of input, or does it mean that the program should actually provide the answer in a reasonable amount of time? If it is the latter, there should be much larger test cases provided.
$endgroup$
– Joel
7 hours ago
$begingroup$
"You must support up to 9,999 total boxes, at minimum." How is "support" interpreted here? Does it merely mean the program should be able to take such size of input, or does it mean that the program should actually provide the answer in a reasonable amount of time? If it is the latter, there should be much larger test cases provided.
$endgroup$
– Joel
7 hours ago
$begingroup$
Suggested test case:
[8, 8, 8, 5, 1]
-> [[8, 8], [8, 5, 1]]
$endgroup$
– Hiatsu
5 hours ago
$begingroup$
Suggested test case:
[8, 8, 8, 5, 1]
-> [[8, 8], [8, 5, 1]]
$endgroup$
– Hiatsu
5 hours ago
1
1
$begingroup$
Or even better:
[8, 5, 8, 8, 1, 2]
-> [[8, 8], [8, 5, 2, 1]]
$endgroup$
– Hiatsu
5 hours ago
$begingroup$
Or even better:
[8, 5, 8, 8, 1, 2]
-> [[8, 8], [8, 5, 2, 1]]
$endgroup$
– Hiatsu
5 hours ago
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
JavaScript (Node.js), 139 bytes
Much faster.
f=(A,s=[])=>(g=([n,...a],s)=>n?s.some((b,i,[...c])=>n<eval(b.join`+`)?0:g(a,c,c[i]=[n,...b]))&&A:A=s)(A.sort((a,b)=>a-b),s)||f(A,[[],...s])
Try it online!
JavaScript (ES6), 157 143 bytes
A=>(g=([n,...a],s=[])=>n?s.map((b,i,[...c])=>n<eval(b.join`+`)||g(a,c,c[i]=[n,...b]),g(a,[...s,[n]]))&&A:A[s.length]?A=s:A)(A.sort((a,b)=>a-b))
Try it online!
Commented
A => ( // A[] = input array, re-used to store the best stacks
g = ( // g is a recursive function taking:
[n, // n = next weight to process
...a], // a[] = array of remaining weights
s = [] // s[] = list of stacks
) => //
n ? // if n is defined:
s.map((b, i, // for each stack b[] at position i in s[],
[...c]) => // using c[] as a copy of s[]:
n < eval(b.join`+`) || // if n is heavy enough to support all values in b[]:
g(a, c, c[i] = [n, ...b]), // do a recursive call with n prepended to c[i]
g(a, [...s, [n]]) // do a recursive call with a new stack containing n
) && A // end of map(); yield A
: // else:
A[s.length] ? A = s : A // if s[] is shorter than A[], update A[] to s[]
)(A.sort((a, b) => a - b)) // initial call to g with A[] sorted in ascending order
$endgroup$
add a comment |
$begingroup$
Jelly, 22 bytes
Œ!ŒṖ€ẎṢ€€ṖÄ>ḊƲ€¬ȦƊƇLÞḢ
Try it online!
Top to bottom.
$endgroup$
add a comment |
$begingroup$
Python 3, 112 bytes
R=range
f=lambda b:[[b[j::i]for j in R(i)]for i in R(1,len(b))if all(b[j]*2>=sum(b[j::i])for j in R(len(b)))][0]
Try it online!
Takes input in sorted decreasing order. If that isn't allowed, it can be done for 148 bytes: (-10 bytes thanks to @Joel)
R=range
V=lambda a:sorted(a)[::-1]
f=lambda b:[[V(b)[j::i]for j in R(i)]for i in R(1,len(b))if all(V(b)[j]*2>=sum(V(b)[j::i])for j in R(len(b)))][0]
or in Python 3.8 for 131:
R=range
f=lambda b:[[(b:=sorted(b)[::-1])[j::i]for j in R(i)]for i in R(1,len(b))if all(b[j]*2>=sum(b[j::i])for j in R(len(b)))][0]
$endgroup$
1
$begingroup$
list(reversed(sorted(a)))
can be written assorted(a)[::-1]
for golfing purpose.
$endgroup$
– Joel
7 hours ago
$begingroup$
You would think I would know that by now, especially since I do so much other indexing. Thanks.
$endgroup$
– Hiatsu
7 hours ago
$begingroup$
Just as a side remark, if not for golfing it would be better writesorted(a, reverse=True)
instead.
$endgroup$
– Joel
6 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "200"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f191086%2fheavy-box-stacking%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
JavaScript (Node.js), 139 bytes
Much faster.
f=(A,s=[])=>(g=([n,...a],s)=>n?s.some((b,i,[...c])=>n<eval(b.join`+`)?0:g(a,c,c[i]=[n,...b]))&&A:A=s)(A.sort((a,b)=>a-b),s)||f(A,[[],...s])
Try it online!
JavaScript (ES6), 157 143 bytes
A=>(g=([n,...a],s=[])=>n?s.map((b,i,[...c])=>n<eval(b.join`+`)||g(a,c,c[i]=[n,...b]),g(a,[...s,[n]]))&&A:A[s.length]?A=s:A)(A.sort((a,b)=>a-b))
Try it online!
Commented
A => ( // A[] = input array, re-used to store the best stacks
g = ( // g is a recursive function taking:
[n, // n = next weight to process
...a], // a[] = array of remaining weights
s = [] // s[] = list of stacks
) => //
n ? // if n is defined:
s.map((b, i, // for each stack b[] at position i in s[],
[...c]) => // using c[] as a copy of s[]:
n < eval(b.join`+`) || // if n is heavy enough to support all values in b[]:
g(a, c, c[i] = [n, ...b]), // do a recursive call with n prepended to c[i]
g(a, [...s, [n]]) // do a recursive call with a new stack containing n
) && A // end of map(); yield A
: // else:
A[s.length] ? A = s : A // if s[] is shorter than A[], update A[] to s[]
)(A.sort((a, b) => a - b)) // initial call to g with A[] sorted in ascending order
$endgroup$
add a comment |
$begingroup$
JavaScript (Node.js), 139 bytes
Much faster.
f=(A,s=[])=>(g=([n,...a],s)=>n?s.some((b,i,[...c])=>n<eval(b.join`+`)?0:g(a,c,c[i]=[n,...b]))&&A:A=s)(A.sort((a,b)=>a-b),s)||f(A,[[],...s])
Try it online!
JavaScript (ES6), 157 143 bytes
A=>(g=([n,...a],s=[])=>n?s.map((b,i,[...c])=>n<eval(b.join`+`)||g(a,c,c[i]=[n,...b]),g(a,[...s,[n]]))&&A:A[s.length]?A=s:A)(A.sort((a,b)=>a-b))
Try it online!
Commented
A => ( // A[] = input array, re-used to store the best stacks
g = ( // g is a recursive function taking:
[n, // n = next weight to process
...a], // a[] = array of remaining weights
s = [] // s[] = list of stacks
) => //
n ? // if n is defined:
s.map((b, i, // for each stack b[] at position i in s[],
[...c]) => // using c[] as a copy of s[]:
n < eval(b.join`+`) || // if n is heavy enough to support all values in b[]:
g(a, c, c[i] = [n, ...b]), // do a recursive call with n prepended to c[i]
g(a, [...s, [n]]) // do a recursive call with a new stack containing n
) && A // end of map(); yield A
: // else:
A[s.length] ? A = s : A // if s[] is shorter than A[], update A[] to s[]
)(A.sort((a, b) => a - b)) // initial call to g with A[] sorted in ascending order
$endgroup$
add a comment |
$begingroup$
JavaScript (Node.js), 139 bytes
Much faster.
f=(A,s=[])=>(g=([n,...a],s)=>n?s.some((b,i,[...c])=>n<eval(b.join`+`)?0:g(a,c,c[i]=[n,...b]))&&A:A=s)(A.sort((a,b)=>a-b),s)||f(A,[[],...s])
Try it online!
JavaScript (ES6), 157 143 bytes
A=>(g=([n,...a],s=[])=>n?s.map((b,i,[...c])=>n<eval(b.join`+`)||g(a,c,c[i]=[n,...b]),g(a,[...s,[n]]))&&A:A[s.length]?A=s:A)(A.sort((a,b)=>a-b))
Try it online!
Commented
A => ( // A[] = input array, re-used to store the best stacks
g = ( // g is a recursive function taking:
[n, // n = next weight to process
...a], // a[] = array of remaining weights
s = [] // s[] = list of stacks
) => //
n ? // if n is defined:
s.map((b, i, // for each stack b[] at position i in s[],
[...c]) => // using c[] as a copy of s[]:
n < eval(b.join`+`) || // if n is heavy enough to support all values in b[]:
g(a, c, c[i] = [n, ...b]), // do a recursive call with n prepended to c[i]
g(a, [...s, [n]]) // do a recursive call with a new stack containing n
) && A // end of map(); yield A
: // else:
A[s.length] ? A = s : A // if s[] is shorter than A[], update A[] to s[]
)(A.sort((a, b) => a - b)) // initial call to g with A[] sorted in ascending order
$endgroup$
JavaScript (Node.js), 139 bytes
Much faster.
f=(A,s=[])=>(g=([n,...a],s)=>n?s.some((b,i,[...c])=>n<eval(b.join`+`)?0:g(a,c,c[i]=[n,...b]))&&A:A=s)(A.sort((a,b)=>a-b),s)||f(A,[[],...s])
Try it online!
JavaScript (ES6), 157 143 bytes
A=>(g=([n,...a],s=[])=>n?s.map((b,i,[...c])=>n<eval(b.join`+`)||g(a,c,c[i]=[n,...b]),g(a,[...s,[n]]))&&A:A[s.length]?A=s:A)(A.sort((a,b)=>a-b))
Try it online!
Commented
A => ( // A[] = input array, re-used to store the best stacks
g = ( // g is a recursive function taking:
[n, // n = next weight to process
...a], // a[] = array of remaining weights
s = [] // s[] = list of stacks
) => //
n ? // if n is defined:
s.map((b, i, // for each stack b[] at position i in s[],
[...c]) => // using c[] as a copy of s[]:
n < eval(b.join`+`) || // if n is heavy enough to support all values in b[]:
g(a, c, c[i] = [n, ...b]), // do a recursive call with n prepended to c[i]
g(a, [...s, [n]]) // do a recursive call with a new stack containing n
) && A // end of map(); yield A
: // else:
A[s.length] ? A = s : A // if s[] is shorter than A[], update A[] to s[]
)(A.sort((a, b) => a - b)) // initial call to g with A[] sorted in ascending order
edited 8 mins ago
answered 9 hours ago
ArnauldArnauld
91.5k7 gold badges107 silver badges373 bronze badges
91.5k7 gold badges107 silver badges373 bronze badges
add a comment |
add a comment |
$begingroup$
Jelly, 22 bytes
Œ!ŒṖ€ẎṢ€€ṖÄ>ḊƲ€¬ȦƊƇLÞḢ
Try it online!
Top to bottom.
$endgroup$
add a comment |
$begingroup$
Jelly, 22 bytes
Œ!ŒṖ€ẎṢ€€ṖÄ>ḊƲ€¬ȦƊƇLÞḢ
Try it online!
Top to bottom.
$endgroup$
add a comment |
$begingroup$
Jelly, 22 bytes
Œ!ŒṖ€ẎṢ€€ṖÄ>ḊƲ€¬ȦƊƇLÞḢ
Try it online!
Top to bottom.
$endgroup$
Jelly, 22 bytes
Œ!ŒṖ€ẎṢ€€ṖÄ>ḊƲ€¬ȦƊƇLÞḢ
Try it online!
Top to bottom.
answered 9 hours ago
Erik the OutgolferErik the Outgolfer
36k4 gold badges30 silver badges113 bronze badges
36k4 gold badges30 silver badges113 bronze badges
add a comment |
add a comment |
$begingroup$
Python 3, 112 bytes
R=range
f=lambda b:[[b[j::i]for j in R(i)]for i in R(1,len(b))if all(b[j]*2>=sum(b[j::i])for j in R(len(b)))][0]
Try it online!
Takes input in sorted decreasing order. If that isn't allowed, it can be done for 148 bytes: (-10 bytes thanks to @Joel)
R=range
V=lambda a:sorted(a)[::-1]
f=lambda b:[[V(b)[j::i]for j in R(i)]for i in R(1,len(b))if all(V(b)[j]*2>=sum(V(b)[j::i])for j in R(len(b)))][0]
or in Python 3.8 for 131:
R=range
f=lambda b:[[(b:=sorted(b)[::-1])[j::i]for j in R(i)]for i in R(1,len(b))if all(b[j]*2>=sum(b[j::i])for j in R(len(b)))][0]
$endgroup$
1
$begingroup$
list(reversed(sorted(a)))
can be written assorted(a)[::-1]
for golfing purpose.
$endgroup$
– Joel
7 hours ago
$begingroup$
You would think I would know that by now, especially since I do so much other indexing. Thanks.
$endgroup$
– Hiatsu
7 hours ago
$begingroup$
Just as a side remark, if not for golfing it would be better writesorted(a, reverse=True)
instead.
$endgroup$
– Joel
6 hours ago
add a comment |
$begingroup$
Python 3, 112 bytes
R=range
f=lambda b:[[b[j::i]for j in R(i)]for i in R(1,len(b))if all(b[j]*2>=sum(b[j::i])for j in R(len(b)))][0]
Try it online!
Takes input in sorted decreasing order. If that isn't allowed, it can be done for 148 bytes: (-10 bytes thanks to @Joel)
R=range
V=lambda a:sorted(a)[::-1]
f=lambda b:[[V(b)[j::i]for j in R(i)]for i in R(1,len(b))if all(V(b)[j]*2>=sum(V(b)[j::i])for j in R(len(b)))][0]
or in Python 3.8 for 131:
R=range
f=lambda b:[[(b:=sorted(b)[::-1])[j::i]for j in R(i)]for i in R(1,len(b))if all(b[j]*2>=sum(b[j::i])for j in R(len(b)))][0]
$endgroup$
1
$begingroup$
list(reversed(sorted(a)))
can be written assorted(a)[::-1]
for golfing purpose.
$endgroup$
– Joel
7 hours ago
$begingroup$
You would think I would know that by now, especially since I do so much other indexing. Thanks.
$endgroup$
– Hiatsu
7 hours ago
$begingroup$
Just as a side remark, if not for golfing it would be better writesorted(a, reverse=True)
instead.
$endgroup$
– Joel
6 hours ago
add a comment |
$begingroup$
Python 3, 112 bytes
R=range
f=lambda b:[[b[j::i]for j in R(i)]for i in R(1,len(b))if all(b[j]*2>=sum(b[j::i])for j in R(len(b)))][0]
Try it online!
Takes input in sorted decreasing order. If that isn't allowed, it can be done for 148 bytes: (-10 bytes thanks to @Joel)
R=range
V=lambda a:sorted(a)[::-1]
f=lambda b:[[V(b)[j::i]for j in R(i)]for i in R(1,len(b))if all(V(b)[j]*2>=sum(V(b)[j::i])for j in R(len(b)))][0]
or in Python 3.8 for 131:
R=range
f=lambda b:[[(b:=sorted(b)[::-1])[j::i]for j in R(i)]for i in R(1,len(b))if all(b[j]*2>=sum(b[j::i])for j in R(len(b)))][0]
$endgroup$
Python 3, 112 bytes
R=range
f=lambda b:[[b[j::i]for j in R(i)]for i in R(1,len(b))if all(b[j]*2>=sum(b[j::i])for j in R(len(b)))][0]
Try it online!
Takes input in sorted decreasing order. If that isn't allowed, it can be done for 148 bytes: (-10 bytes thanks to @Joel)
R=range
V=lambda a:sorted(a)[::-1]
f=lambda b:[[V(b)[j::i]for j in R(i)]for i in R(1,len(b))if all(V(b)[j]*2>=sum(V(b)[j::i])for j in R(len(b)))][0]
or in Python 3.8 for 131:
R=range
f=lambda b:[[(b:=sorted(b)[::-1])[j::i]for j in R(i)]for i in R(1,len(b))if all(b[j]*2>=sum(b[j::i])for j in R(len(b)))][0]
edited 6 hours ago
answered 7 hours ago
HiatsuHiatsu
1717 bronze badges
1717 bronze badges
1
$begingroup$
list(reversed(sorted(a)))
can be written assorted(a)[::-1]
for golfing purpose.
$endgroup$
– Joel
7 hours ago
$begingroup$
You would think I would know that by now, especially since I do so much other indexing. Thanks.
$endgroup$
– Hiatsu
7 hours ago
$begingroup$
Just as a side remark, if not for golfing it would be better writesorted(a, reverse=True)
instead.
$endgroup$
– Joel
6 hours ago
add a comment |
1
$begingroup$
list(reversed(sorted(a)))
can be written assorted(a)[::-1]
for golfing purpose.
$endgroup$
– Joel
7 hours ago
$begingroup$
You would think I would know that by now, especially since I do so much other indexing. Thanks.
$endgroup$
– Hiatsu
7 hours ago
$begingroup$
Just as a side remark, if not for golfing it would be better writesorted(a, reverse=True)
instead.
$endgroup$
– Joel
6 hours ago
1
1
$begingroup$
list(reversed(sorted(a)))
can be written as sorted(a)[::-1]
for golfing purpose.$endgroup$
– Joel
7 hours ago
$begingroup$
list(reversed(sorted(a)))
can be written as sorted(a)[::-1]
for golfing purpose.$endgroup$
– Joel
7 hours ago
$begingroup$
You would think I would know that by now, especially since I do so much other indexing. Thanks.
$endgroup$
– Hiatsu
7 hours ago
$begingroup$
You would think I would know that by now, especially since I do so much other indexing. Thanks.
$endgroup$
– Hiatsu
7 hours ago
$begingroup$
Just as a side remark, if not for golfing it would be better write
sorted(a, reverse=True)
instead.$endgroup$
– Joel
6 hours ago
$begingroup$
Just as a side remark, if not for golfing it would be better write
sorted(a, reverse=True)
instead.$endgroup$
– Joel
6 hours ago
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f191086%2fheavy-box-stacking%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
May we take the weights in sorted order? (either ascending or descending)
$endgroup$
– Arnauld
8 hours ago
$begingroup$
"You must support up to 9,999 total boxes, at minimum." How is "support" interpreted here? Does it merely mean the program should be able to take such size of input, or does it mean that the program should actually provide the answer in a reasonable amount of time? If it is the latter, there should be much larger test cases provided.
$endgroup$
– Joel
7 hours ago
$begingroup$
Suggested test case:
[8, 8, 8, 5, 1]
->[[8, 8], [8, 5, 1]]
$endgroup$
– Hiatsu
5 hours ago
1
$begingroup$
Or even better:
[8, 5, 8, 8, 1, 2]
->[[8, 8], [8, 5, 2, 1]]
$endgroup$
– Hiatsu
5 hours ago