# Which skill to train?

Story (skip, if you prefer the naked task): You need five skills for an imaginary sport: Speed, strength, endurance, accuracy and tactics. If you achieve a score in each of these disciplines, you can work out how well you have mastered the sport as a whole. But, as your coach always says: concentrate on your strengths, because they count more!

Rule: The weakest score counts to the second power, the second weakest to the third power and so on. The strongest score counts with the sixth power!

Let's take an example: A beginner has the scores 3, 2, 4, 1, 2. Then they achieve a total of 1*1 * 2*2*2 * 2*2*2*2 * 3*3*3*3*3 * 4*4*4*4*4*4 = 127401984.

And what should they train, their greatest strength, i.e. improve the third discipline from 4 to 5? That would give them a score of 486000000. Or would it be better to work on their weakness, the fourth discipline? Great, that would give them 509607936. But even better would be to work on the second or fifth skill, then they could achieve 644972544!

So this is the task: name the number of the skill that needs to be improved by 1 to achieve the highest score!

Input: a list of five positive integers

Output: the index of the number to be increased for the maximum product (write in the answer whether the index is 0-based or 1-based). If more than one share the same result, name only one of them, no matter which.

The shortest code wins!

Test data (index 1-based)

3, 2, 4, 1, 2      --> 2
7, 19, 12, 20, 14  --> 4
13, 12, 19, 9, 20  --> 1
13, 18, 12, 12, 14 --> 5
18, 19, 18, 16, 13 --> 2
14, 14, 19, 17, 11 --> 3


# APL(Dyalog Unicode), 14 bytes SBCS

⊃∘⍒(1+÷)*1+⍋∘⍋


Try it on APLgolf!

         ⍋∘⍋    relative ranking
1+       exponents
1+÷          1+1/n = (n+1)/n
*         power
⊃∘⍒             max index

• @chunes 2 and 5 tie
– l4m2
Nov 27, 2023 at 9:01

# Jelly, 9 8 bytes

‘÷*ỤỤ‘ƊM


Try it online!

A monadic link taking a list of five integers and returning the index of the skill to improve (wrapped in a length 1 list). Run as a full program, will print the integer without any list braces.

## Explanation

‘         | Increment by 1
÷        | Divide by original list
*   Ɗ   | To the power of:
ỤỤ‘    | - The result of finding the indices needed to sort the list, repeating that process and then incrementing by 1
M  | Indices of maximum


This should work even where there are ties either in the original list or created by increasing one of the skills.

I’ve subsequently realised this uses the same method as the existing APL answer, so be sure to upvote that one!

• Hard to believable how the original complexity of the algorithm melted down into this few bytes! Nov 28, 2023 at 6:47

# Python, 69 bytes

lambda s:s.index(max(s,key=lambda t:(~t/-t)**sum(map(t.__ge__,s),1)))


Attempt This Online!

0-based.

• Ah, now I understand the ~t/-t trick! Funny. Nov 27, 2023 at 9:54
• @Philippos Admittedly, 1+1/t would be same length and less fancy. But sometimes one might save a pair of parentheses. Nov 27, 2023 at 10:09
• @Philippos yep, usually golfers will use -~x for x+1, which also works in this case (-~t/t). Albert choosing to use ~t/-t is I think an artistic choice, which I agree looks more beautiful :D Nov 29, 2023 at 7:29
• Would you explain your solution? I really enjoyed it! Nov 30, 2023 at 14:22
• @AmirrezaRiahi Sure, but I'd appreciate if you could point out specifically which aspects you want clarified. Nov 30, 2023 at 23:43

# 05AB1E, 14 bytes

āΣIāyQ+{ā>mP}θ


1-based.

Explanation:

ā            # Push a list in the range [1, input-length]
Σ           # Sort it by:
I          #  Push the input-list again
ā         #  Push a list in the range [1, input-length] again
y        #  Push the current item we're sorting on
Q       #  Equals check, so we have a list of 0s with a 1 at the current item
+      #  Add the two lists together, so the current position is increased by 1
{     #  Now sort this updated input-list
ā    #  Push a list in the range [1, input-length] once again
>   #  Increase it by 1
m  #  Take the items in the sorted updated list to the power these values
P #  Take the product of that
}θ          # After the sort-by: pop and push the last item
# (so Σ...}θ basically translates to a maximum-by builtin)
# (which is output implicitly as result)

• Great, thank you for the contribution and the explanation! Nov 27, 2023 at 10:52

# Vyxal 3, 11 bytes

∥ϩėꜝ↑↑2+*ƓḞ


Try it Online!

Port of l'apl

## Explained

∥ϩėꜝ↑↑2+*ƓḞ­⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁢⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁢⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁪‏‏​⁡⁠⁡‌⁢⁡​‎‎⁪⁡⁪⁠⁪⁣⁡⁪‏‏​⁡⁠⁡‌⁢⁢​‎‎⁪⁡⁪⁠⁪⁣⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁣⁪‏‏​⁡⁠⁡‌­
∥            # ‎⁡Apply both of these to the input:
ϩėꜝ         # ‎⁢  Reciprocals of each, incremented
↑↑       # ‎⁣  Grade up, twice
2+     # ‎⁤Increment the result of ^ by 2
*    # ‎⁢⁡Power of reciprocals to ^
ƓḞ  # ‎⁢⁢Find the maximum index by finding the maximum without popping and finding its position
💎


Created with the help of Luminespire.

• Nice, thank you. Nov 27, 2023 at 8:46
• Can you add an explanation for the updated answer? Thank you! Nov 27, 2023 at 10:47
• @Philippos done Nov 27, 2023 at 11:28

# R, 35 bytes

\(x,-=order)(-(1+1/x)^(--x+1))[5]


Attempt This Online!

An anonymous function taking an integer vector and returning an integer. Uses the same method as my Jelly answer (and therefore some other answers including @att’s APL one).

# Uiua, 13 bytes

⊢⍖ⁿ+2⍏⍏:+1÷,1


Try it!

0-indexed. Port of lyxal's Vyxal 3 answer.

⊢⍖ⁿ+2⍏⍏:+1÷,1
1  # push 1
,   # copy the input to top of stack
÷    # divide (reciprocals)
+1     # increment
:       # flip (bring second copy of input to top)
⍏⍏        # rise (grade up) twice
+2          # add two to each
ⁿ            # power
⊢⍖             # index of maximum


# Nekomata, 12 bytes

çᵚᶠ≥Mŗ→ᵐ∏x$Ṃ  Attempt This Online! Based on @att's APL answer, with some hacks because Nekomata doesn't have built-ins for ranking, grade-up, or sort-by. çᵚᶠ≥Mŗ→ᵐ∏x$Ṃ    Takes [3,2,4,1,2] as an example
ç               Prepend 0
-> [0,3,2,4,1,2]
ᵚ              For each element of the original list:
ᶠ≥              Filter the list by <= that element
-> [[0,3,2,1,2],[0,2,1,2],[0,3,2,4,1,2],[0,1],[0,2,1,2]]
M           Element-wise max with the original list
-> [[3,3,3,3,3],[2,2,2,2],[4,4,4,4,4,4],[1,1],[2,2,2,2]]
ŗ→         1 + 1 / that
-> [[4/3,4/3,4/3,4/3,4/3],[3/2,3/2,3/2,3/2],[5/4,5/4,5/4,5/4,5/4,5/4],[2,2],[3/2,3/2,3/2,3/2]]
ᵐ∏       Product of each
-> [1024/243,81/16,15625/4096,4,81/16]
x$Ṃ 0-based index of the maximum -> 1 or 4  This returns all possible results. If you want only one result, you can add the -1 flag. # JavaScript (Node.js), 67 bytes x=>x.map((t,i)=>[(t/++t)**x.filter(v=>v<t).push(0),i]).sort()[0][1]  Try it online! 0-index • Now that was fast and quite short. I think I understand the approach, although I don't know that -~t syntax. Thank you. Nov 27, 2023 at 8:00 • @Philippos There's no -~t in code – l4m2 Nov 27, 2023 at 8:02 • Oh. I saw x=>x.map((t,i)=>[(t/-~t)**x.filter(v=>v<=t).push(0),i]).sort()[0][1], but it went away without visible edit. Maybe edited within 5 minutes? Never mind. Nov 27, 2023 at 8:41 # Charcoal, 19 bytes Ｉ⊟⌈Ｅθ⟦Ｘ∕⊕ιι⊕№÷θ⊕ι⁰κ  Try it online! Link is to verbose version of code. 0-indexed. Explanation: Port of @l4m2's JavaScript answer.  θ Input list Ｅ Map over elements ι Current element ⊕ Incremented ∕ Divided by ι Current element Ｘ Raised to power № Count of ⁰ Literal integer 0 in θ Input list ÷ Integer divided by ι Current element ⊕ Incremented ⊕ Incremented κ Current index ⟦ Make into list ⌈ Take the maximum ⊟ Get just the index Ｉ Cast to string Implicitly print  # Desmos, 49 bytes  f(l)=[(1+1/i)^{6-l[l>i].\count}\for i=l].\argmax  Port of Albert.Lang's Python answer, so make sure to upvote that answer too! Try It On Desmos! This uses a so-called fragile function argmax which means that the expression containing the function (which looks empty because it doesn't render properly) will break if basically any key is pressed when you are focused on that expression. Also, because argmax isn't an "official" function, I don't have a prettified version of the code as simply typing argmax into the calculator won't yield a valid function. For reference, this is the shortest answer I could come up with without the usage of the argmax fragile function (54 bytes): f(l)=sort([1...5],[(1+1/i)^{6-l[l>i].count}fori=l])[5]  # GAWK, 104 101 bytes {for(i=m=1;i<6;i++){++$i;split($0,_);asort(_,a);for(k=t=1;k<6;)if((t*=a[k]^++k)>m){m=t;n=i}--$i}}$0=n  Try it online! • -3 bytes, forgot about default value of fieldsep for split() function. It's index 1-based. # Haskell, 111 bytes import Data.List f y=snd$maximum[(product$zipWith(^)(sort$t++(x+1):d)[2..],i)|i<-[0..4],(t,x:d)<-[splitAt i y]]


Attempt This Online!

# Python 3 (197 bytes)

e=enumerate
x=lambda s:max((((lambda s:(f:=lambda n:n[0]if len(n)==1 else n[0]*f(n[1:]))([j**i for i,j in e(sorted(s),2)]))(j[:i]+[j[i]+1]+j[i+1:]),i)for i,j in e([s]*len(s))),key=lambda i:i[0])[1]


Try it online

# Scala, 171 bytes

Golfed version. Try it online!

y=>(0 to 4).map{i=>val(s,l)=y.splitAt(i);val t=s++((l.headOption.getOrElse(0)+1)::l.tail);(t.sorted.zipWithIndex.map{case(v,j)=>math.pow(v,j+2)}.product,i)}.maxBy(_._1)._2


Ungolfed version. Try it online!

object Main {
def main(args: Array[String]): Unit = {
val testcases = List(
(List(3, 2, 4, 1, 2     ),   2),
(List(7, 19, 12, 20, 14 ), 4),
(List(13, 12, 19, 9, 20 ), 1),
(List(13, 18, 12, 12, 14), 5),
(List(18, 19, 18, 16, 13), 2),
(List(14, 14, 19, 17, 11), 3))

val results = testcases.map { case (i, o) => f(i) == o - 1 }

println(results)
}

def f(y: List[Int]): Int = {
val results = for {
i <- 0 until 5
split = y.splitAt(i)
(t, d) = (split._1, split._2.headOption.getOrElse(0) + 1 :: split._2.tail)
sorted = (t ++ d).sorted
product = sorted.zipWithIndex.map { case (v, idx) => Math.pow(v, idx + 2) }.product
} yield (product, i)

results.maxBy(_._1)._2
}
}

• As far as I see, it succeeds on (List(3, 2, 4, 1, 2 ), 2), because it gives the first best value, not the last. Dec 1, 2023 at 13:39
• @Philippos Thanks a lot. Dec 1, 2023 at 14:11

# Excel (ms365), 89 bytes

Formula in G1:

=LET(x,BYROW(A1:E1+MUNIT(5),LAMBDA(y,PRODUCT(SORT(y,,,1)^{2,3,4,5,6}))),XMATCH(MAX(x),x))

• Wonderful! I didn't know this could be done so elegant in Excel. I may even use such a thing in real life one day. Thank you! Dec 1, 2023 at 16:52
• I think you can save 4 bytes by placing the skill values in A1:A5 and have =LET(x;BYCOL(A1:A5+MUNIT(5);LAMBDA(y;PRODUCT(SORT(y)^{2;3;4;5;6})));XMATCH(MAX(x);x)) Dec 1, 2023 at 17:25
• Thanks @Philippos. Excel is a wonderful tool imp. Anyways, thought I'd present a horizontal input , but yes you are right in that you can save a few chars. 👍
– JvdV
Dec 1, 2023 at 22:43
• Also, in that case '{2;3;4;5;6}' could be written as 'ROW(2:6)' which saves another 3 bytes.
– JvdV
Dec 1, 2023 at 22:45