10
\$\begingroup\$

Introduction

When playing Mario Kart the other day, an interesting question popped up when a Grand Prix with my 2 roommates, 9 AI drivers and myself seemed to be fairly close and therefore exciting until the very end.

We asked ourselves: How close can the point difference between first and last place (#1 and #12) after exactly N races be?

This Code Golf challenge occured to me after the underlying problem for the distribution of the minimum difference in points was answered here by user mlk.

Challenge

Mario Kart races consist of 12 racers which in every race receive the points represented in this 1-indexed array: [15, 12, 10, 8, 7, 6, 5, 4, 3, 2, 1, 0].

In a Grand Prix, the points for each driver from every of the N races are simply added up to form the final point distribution. The difference between the lowest and the highest number of points in this final point distribution is called point difference (alas between places #1 and #12).

Your task is to write a program which takes as input the number of races N (1 <= N <= 32) and outputs the corresponding point difference.

The first 13 correct inputs and outputs can be found below. You will find that after the special cases N = 1 and N = 2, the correct answer will be 0 if N is divisible by 12 and 1if not.

Example Input and Output

As the desired output is correctly defined for every natural number, here are the input/output examples until the repeating pattern described above begins:

1 -> 15
2 -> 4
3 -> 1
4 -> 1
5 -> 1
6 -> 1
7 -> 1
8 -> 1
9 -> 1
10 -> 1
11 -> 1
12 -> 0
13 -> 1

Goal

This is Code Golf, so shortest answer in bytes wins!

Standard loopholes apply.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to Code Golf and nice first question! For future reference, we recommend using the Sandbox to get feedback on challenge ideas before posting them to main. I'd suggest editing in an example of what the final results look like to produce the gaps in the question, as it isn't fully clear \$\endgroup\$ – caird coinheringaahing May 3 at 14:54
  • \$\begingroup\$ Related topic, not really related question: codegolf.stackexchange.com/questions/139513/… \$\endgroup\$ – BLT May 5 at 22:32

21 Answers 21

15
\$\begingroup\$

JavaScript (ES7), 19 bytes

n=>14/n**n+1^n%12<1

Try it online!

We compute:

$$\left\lfloor\frac{14}{n^n}+1\right\rfloor$$

which is \$\lfloor 14/1+1\rfloor=15\$ for \$n=1\$, \$\lfloor 14/4+1 \rfloor=4\$ for \$n=2\$, or \$1\$ for \$n>2\$.

We then XOR the result with \$1\$ if \$n \bmod 12=0\$.

\$\endgroup\$
6
\$\begingroup\$

Python 3, 28 bytes

lambda x:max(26-x*11,x%12>0)

Try it online!

-4 bytes thanks to ovs

If the boolean output is invalid (in Python, True == 1 and False == 0), add + before the max to turn it into an int.

\$\endgroup\$
3
  • \$\begingroup\$ 29 bytes by shortening 1-(x%12<1) to x%12>0. You might not need the leading + if you are fine with booleans in the output. \$\endgroup\$ – ovs May 3 at 16:52
  • \$\begingroup\$ @ovs Ah, thanks \$\endgroup\$ – hyper-neutrino May 3 at 17:01
  • \$\begingroup\$ Exact same code works for Julia, but with x-> instead of lambda x: :) \$\endgroup\$ – sporklpony May 5 at 20:28
3
+50
\$\begingroup\$

Vyxal, 11 bytes

12Ḋ⌐₄?11*-∴

Try it online!

Port of hyper-neutrino's Python answer

How it works

12Ḋ⌐₄?11*-∴ - Program. N is on the stack
12Ḋ         - Is N divisible by 12, 12 | N?
   ⌐        - Logical NOT
    ₄?11    - Push 26, N, 11
        *   - Yield 11×N
         -  - Yield 26 - 11×N
          ∴ - Maximum of (26 - 11 × N) and ¬(12 | N)
\$\endgroup\$
2
  • \$\begingroup\$ Oh shoot, didn't see taking maximum directly works too. Nice. \$\endgroup\$ – Manish Kundu May 3 at 16:06
  • 1
    \$\begingroup\$ Ninja'd \$\endgroup\$ – Aaron Miller May 3 at 16:10
3
\$\begingroup\$

Vim, 29 28 bytes

Thanks to @kops for -1 byte.

C<C-r>=ma<S-Tab>[!!(<C-r>"%12),26-11*<C-r>"])

Try it online!

Vim port of @hyper-neutrino's python answer. In TIO, I couldn't figure out how to make Shift+Tab work, so it is 29 bytes there, but in Vim, you can use Shift+Tab to autocomplete max(, which is 1 less byte.

\$\endgroup\$
1
  • \$\begingroup\$ c$->C saves a byte \$\endgroup\$ – kops May 3 at 20:03
2
\$\begingroup\$

PowerShell 7, 34 bytes

param($n)$n-1?$n-2?1-!($n%12):4:15

No TIO because PS7 (and so the ternary operator) is not supported.

\$\endgroup\$
2
  • \$\begingroup\$ would you like to try a switch($args){...? \$\endgroup\$ – mazzy May 3 at 18:27
  • \$\begingroup\$ @mazzy I can't get a switch-based version of this nearly as short as my solution; but you're the switch guru, I'd love to see what you can come up with! \$\endgroup\$ – Zaelin Goodman May 3 at 19:40
2
\$\begingroup\$

Python 3, 35 bytes

lambda x:x<3and 26-x*11or x%12and 1

Try it online!

-3 bytes thanks to hyper-neutrino
-1 byte thanks to ophact

\$\endgroup\$
6
  • 1
    \$\begingroup\$ -3 bytes if you use 26-x*11 instead of indexing \$\endgroup\$ – hyper-neutrino May 3 at 15:21
  • \$\begingroup\$ @hyper-neutrino Nice! \$\endgroup\$ – caird coinheringaahing May 3 at 15:22
  • 1
    \$\begingroup\$ Can you do or x%12and 1 instead of or(x%12and 1) \$\endgroup\$ – ophact May 3 at 15:22
  • \$\begingroup\$ @ophact I can, I always forget Python's precedence rules :) \$\endgroup\$ – caird coinheringaahing May 3 at 15:23
  • \$\begingroup\$ edited my above comment, my bad. you can get another -1 by swapping 11*x to x*11 so you can remove the space before the or \$\endgroup\$ – hyper-neutrino May 3 at 15:29
2
\$\begingroup\$

Jelly, 14 12 11 bytes

%12a*`14:‘Ʋ

Try it online!

This used hyper-neutrino's method and now uses a version of Arnauld's, so be sure to give them an upvote!

A direct translation of Arnauld's answer also comes out to 11 bytes:

*`14:‘_12ḍ$

Try it online!

How they work

%12a*`14:‘Ʋ - Main link. Takes N on the left
%12         - N % 12
          Ʋ - Previous 4 links as a dyad f(N):
    *`      -   N to the power N
      14:   -   Floor divide 14 by that
         ‘  -   Increment
   a        - If N % 12 is non-zero, replace it by f(N)

*`14:‘_12ḍ$ - Main link. Takes N on the left
*`          - Raise N to its own power
  14:       - Floor divide 14 by N to the N
     ‘      - Increment it
          $ - Previous 2 links as a monad f(N):
       12ḍ  -   Is N divisible by 12?
      _     - Subtract f(N) from the incremented division
\$\endgroup\$
4
  • \$\begingroup\$ Your code outputs every minimum point difference from 1 to N. I would prefer a solution that only outputs the single valid value for N. Could you help me with improving the challenge to better encorporate this fact? \$\endgroup\$ – Der_Reparator May 3 at 15:16
  • \$\begingroup\$ @Der_Reparator The code itself takes N and outputs the minimum point difference. The footer runs the code over each N, I've edited the TIO links to demonstrate that \$\endgroup\$ – caird coinheringaahing May 3 at 15:18
  • \$\begingroup\$ Ah, you got this approach to work. I was trying to make it work but chaining rules didn't seem to want to co-operate lol. Well done :P \$\endgroup\$ – hyper-neutrino May 3 at 15:44
  • 1
    \$\begingroup\$ Not that it saves any byte, but 14:‘ can also be 15÷Ċ. \$\endgroup\$ – Arnauld May 3 at 16:57
2
\$\begingroup\$

Snap!, 6 blocks

enter image description here

straightforward implementation of the question, self explanatory

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

JavaScript, 27 bytes

n=>n<3?[,15,4][n]:+!!(n%12)

bet you that I will get more upvotes on this answer than on some of my more complex ones.

-6 thanks to @CommandMaster

\$\endgroup\$
8
  • \$\begingroup\$ Can't you do !!(n%12) instead of [1,0][+!(n%12)]? \$\endgroup\$ – Command Master May 3 at 15:21
  • \$\begingroup\$ @CommandMaster that returns a boolean, not a number. And that's a problem, because we can't index arrays with booleans. \$\endgroup\$ – ophact May 3 at 15:22
  • 1
    \$\begingroup\$ Than you can do +!!(n%12). Why do you need to index an array? \$\endgroup\$ – Command Master May 3 at 15:23
  • \$\begingroup\$ @CommandMaster thanks for that one! I will edit that into my post.$ \$\endgroup\$ – ophact May 3 at 15:25
  • 1
    \$\begingroup\$ 23 bytes \$\endgroup\$ – Arnauld May 3 at 16:33
1
\$\begingroup\$

MMIX, 32 bytes (8 instrs)

jxd

00000000: 1dff000c feff0006 7b01ff01 31ff0001  ø”¡€“”¡©{¢”¢1”¡¢
00000010: 6301ff0f 31ff0002 6301ff04 f8020000  c¢”Đ1”¡£c¢”¥ẏ£¡¡

Disassembly:

pdif    DIV  $255,$0,12
        GET  $255,rR        // t = n % 12
        ZSNZ $1,$255,1      // i = (bool)t
        CMP  $255,$0,1
        CSZ  $1,$255,15     // if(n == 1) i = 15
        CMP  $255,$0,2
        CSZ  $1,$255,4      // if(n == 2) i = 4
        POP  2,0            // return i

Straight-line code!

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

Husk, 15 bytes

!:15:4↓2J0C11∞1

Try it online!

The caveman approach: manually create the infinite list and index into it.

Explanation

!:15:4↓2J0C11∞1
             ∞1  infinite list of 1s
          C11    cut in pieces of 11
        J0       join with zeroes
      ↓2         drop 2 elements
    :4           prepend 4
 :15             prepend 15
!                index into it
\$\endgroup\$
0
1
\$\begingroup\$

Vyxal, 15 bytes

12Ḋ⌐₄?11*-?2≤e*

Try it Online!

12Ḋ       # Is the input divisible by 12
⌐         # Negate that. So stack contains 0 for numbers divisible by 12 and 1 otherwise
₄?11*-    # 26-11*n
?2≤e      # Is the input less than equal to 2?
e         # Take exponent
*         # Multiply the two numbers in the stack and print the result.
\$\endgroup\$
1
\$\begingroup\$

Whispers v2, 79 bytes

> Input
> 26
> 11
> 12
> 1
>> 3⋅1
>> 2-6
>> 1∣4
>> 5-8
>> 7»9
>> Output 10

Try it online!

Same formula as my other answers, implemented in the verbosity of Whispers

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

05AB1E, 11 bytes

A port of hyper-neutrino's answer.

«₂s-I12Ö_‚à

Try it online! (No, END does not actually end the program.)

Commented:

«            # concatenate the input with itself
             # this is the same as multiplying by 11 for small integers
 ₂s-         # subtract this from 26
    I        # push the input again
     12Ö_    # does 12 divide not divide the input? Booleans are represented as 0 and 1.
         ‚   # pair both values
          à  # take the maximum

Constructing the infinite list and indexing into it takes 12 bytes:

₄;16вλèN12Ö_

Try it online!

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

PowerShell, 38 bytes

$args|%{26-$_*11;+!!($_%12)}|sort -b 1

Try it online!

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

Python 2, 25 bytes

lambda x:2/x*7/x+(x%12>0)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Retina 0.8.2, 36 bytes

.+
$*_
^(_{12})+$
0
___+
1
__
4
_
15

Try it online! Link includes test suite that outputs the answers from 1 to N. Explanation:

.+
$*_

Convert N to unary.

^(_{12})+$
0

The result is 0 if N is a multiple of 12, ...

___+
1

... 1 if N>2, ...

__
4

... 4 if N=2, ...

_
15

... and 15 if N=1.

\$\endgroup\$
0
\$\begingroup\$

Charcoal, 15 bytes

I⌈⟦‹⁰﹪N¹²⁻²⁶ײθ

Try it online! Link is to verbose version of code. Explanation: Port of @ovs's 05AB1E answer.

      N         Input as a number
     ﹪ ¹²       Modulo 12
   ‹⁰           Is greater than 0
              θ Input as a string
            ײ  Duplicated i.e. multiplied by 11
         ⁻²⁶    Subtracted from 26
  ⟦             Pair into a list
 ⌈              Take the maximum
I               Cast to string
                Implicitly print
\$\endgroup\$
0
\$\begingroup\$

Zsh, 27 bytes

Boring ternary math function.

()(($1<3?26-11*$1:$1%12>0))

Try it online!


Zsh --cbases, 29 bytes

k=e3
<<<$[0x$k[$1]+!!($1%12)]

Try it online!

More interesting, using hexadecimal 0xe and 0x3 to add 14 and 3 to the first two cases.

\$\endgroup\$
0
\$\begingroup\$

C (gcc), 27 bytes

f(n){n=n<3?26-n*11:n%12>0;}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Lua, 52 bytes

function f(n)return 14/n^n+1|((n%12<1)and 1 or 0)end

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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