16
\$\begingroup\$

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
\$\endgroup\$

16 Answers 16

8
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ @chunes 2 and 5 tie \$\endgroup\$
    – l4m2
    Nov 27, 2023 at 9:01
7
\$\begingroup\$

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!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Hard to believable how the original complexity of the algorithm melted down into this few bytes! \$\endgroup\$
    – Philippos
    Nov 28, 2023 at 6:47
6
\$\begingroup\$

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.

\$\endgroup\$
7
  • \$\begingroup\$ Ah, now I understand the ~t/-t trick! Funny. \$\endgroup\$
    – Philippos
    Nov 27, 2023 at 9:54
  • 1
    \$\begingroup\$ @Philippos Admittedly, 1+1/t would be same length and less fancy. But sometimes one might save a pair of parentheses. \$\endgroup\$ Nov 27, 2023 at 10:09
  • 1
    \$\begingroup\$ @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 \$\endgroup\$
    – justhalf
    Nov 29, 2023 at 7:29
  • \$\begingroup\$ Would you explain your solution? I really enjoyed it! \$\endgroup\$ Nov 30, 2023 at 14:22
  • \$\begingroup\$ @AmirrezaRiahi Sure, but I'd appreciate if you could point out specifically which aspects you want clarified. \$\endgroup\$ Nov 30, 2023 at 23:43
5
\$\begingroup\$

05AB1E, 14 bytes

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

1-based.

Try it online or verify all test cases.

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)
\$\endgroup\$
1
  • \$\begingroup\$ Great, thank you for the contribution and the explanation! \$\endgroup\$
    – Philippos
    Nov 27, 2023 at 10:52
5
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Nice, thank you. \$\endgroup\$
    – Philippos
    Nov 27, 2023 at 8:46
  • \$\begingroup\$ Can you add an explanation for the updated answer? Thank you! \$\endgroup\$
    – Philippos
    Nov 27, 2023 at 10:47
  • 1
    \$\begingroup\$ @Philippos done \$\endgroup\$
    – lyxal
    Nov 27, 2023 at 11:28
4
\$\begingroup\$

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).

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ Now that was fast and quite short. I think I understand the approach, although I don't know that -~t syntax. Thank you. \$\endgroup\$
    – Philippos
    Nov 27, 2023 at 8:00
  • \$\begingroup\$ @Philippos There's no -~t in code \$\endgroup\$
    – l4m2
    Nov 27, 2023 at 8:02
  • \$\begingroup\$ 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. \$\endgroup\$
    – Philippos
    Nov 27, 2023 at 8:41
2
\$\begingroup\$

Charcoal, 19 bytes

I⊟⌈Eθ⟦X∕⊕ιι⊕№÷θ⊕ι⁰κ

Try it online! Link is to verbose version of code. 0-indexed. Explanation: Port of @l4m2's JavaScript answer.

    θ               Input list
   E                Map over elements
         ι          Current element
        ⊕           Incremented
       ∕            Divided by
          ι         Current element
      X             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
I                   Cast to string
                    Implicitly print
\$\endgroup\$
2
\$\begingroup\$

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]
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

Scala, 171 bytes

A port of @matteo_c's Haskell answer in Scala.


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
  }
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Philippos
    Dec 1, 2023 at 13:39
  • \$\begingroup\$ @Philippos Thanks a lot. \$\endgroup\$
    – 138 Aspen
    Dec 1, 2023 at 14:11
1
\$\begingroup\$

Excel (ms365), 89 bytes

enter image description here

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))
\$\endgroup\$
4
  • 1
    \$\begingroup\$ 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! \$\endgroup\$
    – Philippos
    Dec 1, 2023 at 16:52
  • 1
    \$\begingroup\$ 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)) \$\endgroup\$
    – Philippos
    Dec 1, 2023 at 17:25
  • \$\begingroup\$ 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. 👍 \$\endgroup\$
    – JvdV
    Dec 1, 2023 at 22:43
  • \$\begingroup\$ Also, in that case '{2;3;4;5;6}' could be written as 'ROW(2:6)' which saves another 3 bytes. \$\endgroup\$
    – JvdV
    Dec 1, 2023 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.