Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

66%
+2 −0
Challenges Win a War (or at least a few battles)

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)=&gt...

posted 3y ago by Hakerh400‭  ·  edited 3y ago by Hakerh400‭

Answer
#2: Post edited by user avatar Hakerh400‭ · 2021-02-10T21:21:21Z (about 3 years ago)
  • # 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 by user avatar Hakerh400‭ · 2021-02-10T21:19:32Z (about 3 years ago)
# 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"