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)

BQN, 44 bytesSBCS +´{(≠↑𝕨-𝔽)⊸+2↓⊑∨(≍×𝕨≥⊢)○𝔽˜⟜×⊸∾¨(<1+𝕩)×⥊↕2¨𝕩} Run online! The solution is a function that takes n as the left argument (𝕨) and A as the right argument (𝕩). k is just the le...

posted 3y ago by Marshall Lochbaum‭

Answer
#1: Initial revision by user avatar Marshall Lochbaum‭ · 2021-04-24T13:17:19Z (almost 3 years ago)
# [BQN](https://mlochbaum.github.io/BQN/), 44 bytes<sup>[SBCS](https://github.com/mlochbaum/BQN/blob/master/commentary/sbcs.bqn)</sup>
```
+´{(≠↑𝕨-𝔽)⊸+2↓⊑∨(≍×𝕨≥⊢)○𝔽˜⟜×⊸∾¨(<1+𝕩)×⥊↕2¨𝕩}
```
[Run online!](https://mlochbaum.github.io/BQN/try.html#code=V1cg4oaQICvCtHso4omg4oaR8J2VqC3wnZS9KeKKuCsy4oaT4oqR4oioKOKJjcOX8J2VqOKJpeKKoinil4vwnZS9y5zin5zDl+KKuOKIvsKoKDwxK/Cdlakpw5fipYrihpUywqjwnZWpfQoKV1fCtMKo4p+oCiAgMSAg4oC/4p+oMOKfqQogIDIgIOKAv+KfqDEsMeKfqQogIDEwIOKAv+KfqDQsIDMsIDPin6kKICAxMDDigL/in6g1MCwgMjUsIDI14p+pCiAgMTAw4oC/4p+oMzMsIDM0LCAzMywgMCwgMOKfqQrin6k=)

The solution is a function that takes *n* as the left argument (`𝕨`) and *A* as the right argument (`𝕩`). *k* is just the length of *A*, so it's not used as an argument. The function builds a list of all possible combinations, then calculates a score for each one: the number of battles won followed by the number of troops required, but reduced to 0 if it requires more troops than are available. It gives a wrong answer if no battles can be won since all scores are 0 (could be fixed by incrementing the not-impossible scores). Note that the number of troops required is always one more than the number of enemy troops.

Range (`↕`) makes the list of combinations. When given a shape argument, it returns an array of that shape where each element's value is its own index. With a list of 2s, the possible indices for each axis are 0 and 1, and the result contains every combination of choices. But it's an array, so [Deshape](https://mlochbaum.github.io/BQN/doc/reshape.html#deshape) (`⥊`) is needed to turn it into a list that can be sorted.

[Combinators](https://mlochbaum.github.io/BQN/tutorial/combinator.html) and [trains](https://mlochbaum.github.io/BQN/doc/train.html) are used several times.

The function consists of a [block](https://mlochbaum.github.io/BQN/doc/block.html) 1-modifier (inside `{}`) applied to the operand `+´` (Plus Fold, that is, sum) to give a derived function. Inside the modifier `𝔽` is the operand (sum) `𝕨` is the left argument *A*, and `𝕩` is the right argument *n*. Explanation for the three sections:

```
(<1+𝕩)×⥊↕2¨𝕩         # Create list of combinations
         2¨𝕩         # List with each element of 𝕩 replaced with 2
        ↕            # Range: array of index lists with that shape
       ⥊             # Flatten from 2×2×2… array to list
(<1+𝕩)               # Enclose 1+𝕩, giving a 0-axis array
      ×              # Multiply: turn 1s in ⥊↕2¨𝕩 into entries of 1+𝕩

2↓⊑∨(≍×𝕨≥⊢)○𝔽˜⟜×⊸∾¨  # Select the best list
                  ¨  # On each list:
              ⟜×     #   Use sign (1 if some, 0 if none) as right argument
             ˜       #   No wait, left argument
           ○𝔽        #   Sum both arguments (values and signs)
     ≍               #   Pair sums: ⟨battles won, troops required⟩
       𝕨≥⊢           #   Do we have the troops? ⊢ is right argument
    ( ×   )          #   Multiply: set score list to 0 if impossible
                ⊸∾   #   Prepend to original list
   ∨                 # Sort descending
  ⊑                  # First—highest—entry
2↓                   # Remove score, leaving required troop counts

(≠↑𝕨-𝔽)⊸+            # Add in remaining troops
   𝕨-𝔽               # Troops left over
 ≠                   # Length of result list
( ↑   )              # Take, padding leftovers with 0
       ⊸+            # And add to troop counts
```