Post History
JavaScript, 227 bytes (n,k,A,C=(a,b)=>a?[...C(--a,b),b(a)]:[],f=m=>m?C(n+1,i=>f(m-1).map(([p,s])=> [[...p,i],s+i])).flat():[[[],0]])=>f(k).map(([b,s])=>a=s-n?a:b.map((a,b)=>...
Answer
#2: Post edited
- # JavaScript, 227 bytes
- <!-- language-all: lang-javascript -->
(n,k,A,C=(a,b)=>a?[...C(--a,b),b(a)]:[],f=m=>m?C(n+1,i=>f(m-1).map(([p,s])=>[[...p,i],s+i])).flat():[[[],0]])=>f(k).map(([b,s])=>a=s-n?a:b.map((a,b)=>(q=A[b])<a&&(e++,f+=q),e=f=0,[,c,d]=a)&&e<c|e==c&f<d?a:[b,e,f],a=[,-1])&&a[0]- Explanation: recursively generates all possible troop arrangements of length $k$ whose sum is $n$ and picks the one with the maximum score.
- [Try it online!][TIO-kkzx8nsi] <small>(229 bytes, because TIO uses an old version of Node.js that has a parsing issue when an arrow function appears inside a call)</small>
- [TIO-kkzx8nsi]: https://tio.run/##RZDBboMwDIbvPEVOyBEOhXa90LpV1VfYLcoh0GRiLYQWttPenTmj26RElr/f/x8r7/bTjs2jHSbVh4ubPc3Q4xVPeCawWEs62KPO8/wMSsUea7DSVNqgp44O3fEMfVZiSwcPnSpl3tkBQA84GvbqaB2wNThmrZEy9zc7gay05oDCxBEP119TvZgsjao/2qpe8LIF3OmkayP3Nk3BZRn6jO4SwZGnAjU2eDFkpUxTt2@@HFGT@v2FUzjVoTdoSaMqDQ9YXZi5Cf04icmN0yhI6EQIXaLgw6LB2K5R8GFaPkFZoNgwefkpmz/6xFuu6228i7JaPUVmehMd0ciVUXxDKCVe286FjykxuyTx4QH/W4ngl@0kR0Ucbi6/hTfwwF8aFSl38zc "JavaScript (Node.js) – Try It Online"
- # JavaScript, 227 bytes
- <!-- language-all: lang-javascript -->
- (n,k,A,C=(a,b)=>a?[...C(--a,b),b(a)]:[],f=m=>m?C(n+1,i=>f(m-1).map(([p,s])=>
- [[...p,i],s+i])).flat():[[[],0]])=>f(k).map(([b,s])=>a=s-n?a:b.map((a,b)=>(q
- =A[b])<a&&(e++,f+=q),e=f=0,[,c,d]=a)&&e<c|e==c&f<d?a:[b,e,f],a=[,-1])&&a[0]
- Explanation: recursively generates all possible troop arrangements of length $k$ whose sum is $n$ and picks the one with the maximum score.
- [Try it online!][TIO-kkzx8nsi] <small>(229 bytes, because TIO uses an old version of Node.js that has a parsing issue when an arrow function appears inside a call)</small>
- [TIO-kkzx8nsi]: https://tio.run/##RZDBboMwDIbvPEVOyBEOhXa90LpV1VfYLcoh0GRiLYQWttPenTmj26RElr/f/x8r7/bTjs2jHSbVh4ubPc3Q4xVPeCawWEs62KPO8/wMSsUea7DSVNqgp44O3fEMfVZiSwcPnSpl3tkBQA84GvbqaB2wNThmrZEy9zc7gay05oDCxBEP119TvZgsjao/2qpe8LIF3OmkayP3Nk3BZRn6jO4SwZGnAjU2eDFkpUxTt2@@HFGT@v2FUzjVoTdoSaMqDQ9YXZi5Cf04icmN0yhI6EQIXaLgw6LB2K5R8GFaPkFZoNgwefkpmz/6xFuu6228i7JaPUVmehMd0ciVUXxDKCVe286FjykxuyTx4QH/W4ngl@0kR0Ucbi6/hTfwwF8aFSl38zc "JavaScript (Node.js) – Try It Online"
#1: Initial revision
# JavaScript, 227 bytes <!-- language-all: lang-javascript --> (n,k,A,C=(a,b)=>a?[...C(--a,b),b(a)]:[],f=m=>m?C(n+1,i=>f(m-1).map(([p,s])=>[[ ...p,i],s+i])).flat():[[[],0]])=>f(k).map(([b,s])=>a=s-n?a:b.map((a,b)=>(q=A[b]) <a&&(e++,f+=q),e=f=0,[,c,d]=a)&&e<c|e==c&f<d?a:[b,e,f],a=[,-1])&&a[0] Explanation: recursively generates all possible troop arrangements of length $k$ whose sum is $n$ and picks the one with the maximum score. [Try it online!][TIO-kkzx8nsi] <small>(229 bytes, because TIO uses an old version of Node.js that has a parsing issue when an arrow function appears inside a call)</small> [TIO-kkzx8nsi]: https://tio.run/##RZDBboMwDIbvPEVOyBEOhXa90LpV1VfYLcoh0GRiLYQWttPenTmj26RElr/f/x8r7/bTjs2jHSbVh4ubPc3Q4xVPeCawWEs62KPO8/wMSsUea7DSVNqgp44O3fEMfVZiSwcPnSpl3tkBQA84GvbqaB2wNThmrZEy9zc7gay05oDCxBEP119TvZgsjao/2qpe8LIF3OmkayP3Nk3BZRn6jO4SwZGnAjU2eDFkpUxTt2@@HFGT@v2FUzjVoTdoSaMqDQ9YXZi5Cf04icmN0yhI6EQIXaLgw6LB2K5R8GFaPkFZoNgwefkpmz/6xFuu6228i7JaPUVmehMd0ciVUXxDKCVe286FjykxuyTx4QH/W4ngl@0kR0Ucbi6/hTfwwF8aFSl38zc "JavaScript (Node.js) – Try It Online"