34
\$\begingroup\$

Today while playing with my kids I noticed that an apparently simple toy in the park hid a challenge.

Clock

The wheel has a triangle that points to a number, but also has three circles that point to the numbers every 90 degrees from the first one. So:

Challenge (really simple)

Given an integer between 1 and 12 (the one pointed by the triangle) in any acceptable form, output also in any acceptable form and order the three numbers pointed by the circles (the ones every 90 degrees).

Test cases

In       Out
1        4, 7, 10
2        5, 8, 11
3        6, 9, 12
4        7, 10, 1
5        8, 11, 2
6        9, 12, 3
7        10, 1, 4
8        11, 2, 5
9        12, 3, 6
10       1, 4, 7
11       2, 5, 8
12       3, 6, 9

This is , so may the shortest code for every language win!

\$\endgroup\$
6
  • 11
    \$\begingroup\$ @Mr.Xcoder sorry, this time I'm going to say no. \$\endgroup\$
    – Charlie
    Dec 29, 2017 at 21:26
  • 4
    \$\begingroup\$ Is this the fourth challenge now based on some activity involving your kids? :P \$\endgroup\$
    – FlipTack
    Dec 29, 2017 at 21:58
  • 4
    \$\begingroup\$ @FlipTack Perhaps we need an inspired-by-kids tag ;) \$\endgroup\$
    – Steadybox
    Dec 29, 2017 at 22:13
  • 9
    \$\begingroup\$ @FlipTack I've lost count. :-) But given that I spent most of my free time with my kids, guess where does my inspiration come from... \$\endgroup\$
    – Charlie
    Dec 29, 2017 at 22:17
  • 2
    \$\begingroup\$ @Steadybox That would be a meta tag, which is BAD. But yes, we do. (Perhaps a meta question with links would suffice?) \$\endgroup\$
    – wizzwizz4
    Jan 1, 2018 at 10:32

48 Answers 48

16
\$\begingroup\$

Python 3, 33 bytes

lambda n:{*range(n%3,13,3)}-{n,0}

Try it online!


Python 2, 35 bytes

lambda n:[(n+c)%12+1for c in 2,5,8]

Try it online!

\$\endgroup\$
11
\$\begingroup\$

Jelly, 8 bytes

12Rṙ’m3Ḋ

A monadic link taking a number and returning a list of numbers.

Try it online! or see all cases.

How?

12Rṙ’m3Ḋ - Link: number, n   e.g. 5
12       - literal twelve         12
  R      - range                  [1,2,3,4,5,6,7,8,9,10,11,12]
    ’    - decrement n            4
   ṙ     - rotate left            [5,6,7,8,9,10,11,12,1,2,3,4]
      3  - literal three          3
     m   - modulo slice           [5,8,11,2]
       Ḋ - dequeue                [8,11,2]
\$\endgroup\$
1
  • \$\begingroup\$ Alternative solution: 12Rṙm-3Ḋ (output in reverse order) \$\endgroup\$
    – DELETE_ME
    Dec 30, 2017 at 2:14
8
\$\begingroup\$

Python 2, 35 bytes

lambda n:(range(1,13)*2)[n+2:n+9:3]

Try it online!

\$\endgroup\$
7
\$\begingroup\$

MATL, 9 bytes

I:I*+12X\

Try it online!

Explanation

Consider input 4 as an exmaple.

I:     % Push [1 2 3]
       % STACK: [1 2 3]
I      % Push 3
       % STACK: [1 2 3], 3
*      % Multiply, element-wise
       % STACK: [3 6 9]
+      % Add implicit input, element-wise
       % STACK: [7 10 13]
12     % Push 12
X\     % 1-based modulus. Implicit display
       % STACK: [7 10 1]
\$\endgroup\$
7
\$\begingroup\$

R,  29  28 bytes

Thanks to @Giuseppe for saving a byte!

function(n)(n+1:3*3-1)%%12+1

Try it online!

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

APL+WIN, 13bytes

(⎕⌽⍳12)[3×⍳3]

Explanation:

⎕ Prompt for screen input of indicated time t

⍳12 Create a vector of integers from 1 to 12

⌽ Rotate the vector by t elements front to back

[3×⍳3] Select 3rd, 6th and 9th elements.
\$\endgroup\$
1
  • \$\begingroup\$ Quite neat. I like that indexed rotation approach \$\endgroup\$
    – Uriel
    Dec 30, 2017 at 18:47
4
\$\begingroup\$

C (gcc), 48 bytes

i;f(n){for(i=4;--i;printf("%d ",n%12?:12))n+=3;}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 29 bytes

Similar to xnor's answer.

n=>[2,5,8].map(k=>(n+k)%12+1)

Demo

let f =

n=>[2,5,8].map(k=>(n+k)%12+1)

for(n = 1; n <= 12; n++) {
  console.log(n + ' -> ' + f(n));
}

\$\endgroup\$
2
  • \$\begingroup\$ I had n=>[3,6,9].map(v=>(v+n)%12) then realized that it returns 0, not 12... \$\endgroup\$
    – ericw31415
    Dec 30, 2017 at 0:47
  • \$\begingroup\$ @ericw31415 Actually, my 1st approach was n=>[3,6,9].map(v=>(v+n)%12||12) (but that's 31 bytes). \$\endgroup\$
    – Arnauld
    Dec 30, 2017 at 1:19
4
\$\begingroup\$

Octave, 25 bytes

@(x)[a=1:12 a](3+x:3:9+x)

Try it online!

Fairly simple anonymous function.

We first create an array of [1:12 1:12] - so two copies of the full number set. Then we index in to select the values of x+3, x+6, x+9, where x is the number input.

Octave is 1-indexed, so we can simply select the array elements based on the input (although to be honest 0-indexed would use the same number of bytes here).

This seems to use a method that is unique to the other answers in that by having two copies of the array, we don't have to wrap the indices using modulo.

\$\endgroup\$
2
  • \$\begingroup\$ Very nice, but the "boring" mod approach is shorter! Try it online! \$\endgroup\$
    – Giuseppe
    Dec 30, 2017 at 12:45
  • \$\begingroup\$ @Giuseppe lol, I'm sure I tried using mod and couldn't make it shorter. Well done! Feel free to post as an answer. \$\endgroup\$ Dec 30, 2017 at 13:02
3
\$\begingroup\$

Befunge-93, 20 19 18 bytes

852<_@#:.+1%+66+&p

Try it online!

Explanation

852                   Push 8, 5 and 2 onto the stack - the offsets we're going to add.
   <                  Reverse direction, and start the main loop.
852                   Push 2, 5, and 8 onto the stack, but we don't actually want these.
                 p    So we use a "put" operation to drop the top three values.
                &     Read the hour from stdin.
               +      Add it to the topmost offset.
         +1%+66       Mod 12 and add 1 to get it in the range 1 to 12.
        .             Then output the result to stdout.
    _@#:              Exit if the next offset is zero (i.e. nothing more on the stack).
   <                  Otherwise start the main loop again. 

This relies on behaviour specific to the reference interpreter: on end-of-file, the & operator returns the last value that was read. That's why we can safely re-read the hour from stdin on each iteration of the loop.

\$\endgroup\$
2
  • \$\begingroup\$ Neat. I didn’t know & did that \$\endgroup\$
    – Jo King
    Dec 30, 2017 at 6:14
  • 3
    \$\begingroup\$ @JoKing Technically it's a bug in the interpreter, another related side-effect being that & can also be used as a kind of one-off random number generator. That is less reliable, though, since it depends on the compiler that was used to build it. It's working on TIO at the moment, but there was a time when Dennis changed to a different version of gcc and we lost that functionality for a while. \$\endgroup\$ Dec 30, 2017 at 13:45
3
\$\begingroup\$

APL (Dyalog), 12 bytes

1+12|2 5 8∘+

Try it online!

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

05AB1E, 8 bytes

ƵžS+12%>

At the time of writing this, this submission has the lowest score submitted. Technically I am winning!

Try it online!

How?

ƵžS                   # Push the list [2, 5, 8]
   +                  # Add the input to each number
    12%               # Mod each result by 12
       >              # Increment each
                      # And print the ending list implicitly!

See this tip of Kevin to know why Ƶž is 258!

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

SOGL V0.12, 9 bytes

3{3+:6«‰P

Try it Here!

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

Clean, 44 bytes

import StdEnv
@n=[(n+i)rem 12+1\\i<-[2,5,8]]

Try it online!

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

C# (.NET Core), 42 bytes

h=>new[]{(2+h)%12+1,(5+h)%12+1,(8+h)%12+1}

Try it online!

Essentially just a port of many of the other answers to C#.

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

Japt, 11 bytes

3ÆU±3 uC ªC

Try it


Explanation

Implicit input of integer U. Generate a 3 element array () and, for each element, increment U by 3 (U±3), modulo by 12 (uC) and, because 12%12=0, return the result OR 12 (ªC).

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

K (oK), 11 bytes

Solution:

1+12!2 5 8+

Try it online!

Examples:

1+12!2 5 8+1
4 7 10
1+12!2 5 8+2
5 8 11
1+12!2 5 8+3
6 9 12
1+12!2 5 8+4
7 10 1

Explanation:

This was the first solution that came to mind. Might not be the best or shortest.

1+12!2 5 8+ / the solution
     2 5 8+ / add 2, 5 and 8 to the input
  12!       / apply modulo 12 to the results
1+          / add 1
\$\endgroup\$
2
\$\begingroup\$

GolfScript, 46 bytes

~13,1>:x?:y;0:i;x y 3+12%=x y 6+12%=x y 9+12%=

This is the first time I'm doing code golf, so with my lack of experience I've probably not found the best solution, but I need to start somewhere, right?

Try it online or try all cases

\$\endgroup\$
1
  • \$\begingroup\$ Hello, welcome to the site! This looks like a nice first answer :) Unfortunately, I don't know anything about golfscript, but you might be able to get some tips here \$\endgroup\$
    – DJMcMayhem
    Dec 31, 2017 at 19:20
2
\$\begingroup\$

Brain-Flak, 84 bytes

(()()()){({}<(()(){})(((()()()){}){}<>){(({})){({}[()])<>}{}}<>(([{}]{})<>)>[()])}<>

Try it online!

At least I beat Doorknob's face solution...

Explanation:

LOOP 3 TIMES: (()()()){({}<

  n += 2:
   (()(){})
  push 12:
   (((()()()){}){}<>)
  n mod 12 + 1; pushing to both stacks:
   {(({})){({}[()])<>}{}}<>(([{}]{})<>)

END LOOP: >[()])}<>
\$\endgroup\$
2
\$\begingroup\$

Pyth, 12 bytes

%R12+LQ*R3S3    

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to the site! :) \$\endgroup\$
    – DJMcMayhem
    Jan 5, 2018 at 18:54
2
\$\begingroup\$

Wolfram Language (Mathematica) 20 bytes

Mod[#+{3,6,9},12,1]&

The normal modulus operation, threaded over a list, offset by 1.

Try it online!

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

Vyxal, 8 bytes

⁺ẇf+12%›

Try it online! (All test cases)

Port of SunnyMoon's 05AB1E solution

How it works:

⁺ẇf+12%›
⁺ẇ        The number 258
  f       ...as a list of digits ⟨ 2 | 5 | 8 ⟩
   +      Add the input to each
    12%   Mod-12 each
       ›  Increment each
          (implicit output)
\$\endgroup\$
2
\$\begingroup\$

Thunno 2, 8 bytes

ṇɗd+12%⁺

Try it online!

Explanation

ṇɗd+12%⁺  # Implicit input
ṇɗ        # Push compressed integer 258
  d+      # Add [2,5,8] to input
    12%   # Mod each by 12
       ⁺  # Increment each
          # Implicit output
\$\endgroup\$
2
\$\begingroup\$

Nekomata, 9 bytes

12Rᶠ{-Z3¦

Attempt This Online!

12Rᶠ{-Z3¦
12R         [1,2,...,12]
   ᶠ{       Filter by
     -          Subtract the input
      Z         Check if the result is non-zero
       3¦       Check if the result is divisible by 3

Nekomata, 10 bytes

258¢D+12%→

Attempt This Online!

258¢D+12%→
258¢D       Decimal digits of 258
     +      Add
      12%   Modulo 12
         →  Increment
\$\endgroup\$
2
+100
\$\begingroup\$

Piet + ascii-piet, 45 bytes (5×18=90 codels)

tAaaqreeumccsqqqijlVa rbiqaaueljnvea?_ t?B tt

Try Piet online!

My looping template is getting weirder and weirder... :P

inN 4    [n 4]; force toggle CC at the exit of 4 region
Loop:
  dup CC+    [n flag] switch CC on 3rd iteration so it will exit after the loop
  2 1 roll   [flag n]
  2 +        [flag n+2]
  3 dup dup * + %  [flag (n+2)%12]
  1 + d outN [flag next_n] compute and print the next number
  1 outC     [flag next_n] print the separator (ASCII 1)
  2 1 roll   [next_n flag]
  2 /        [next_n flag/2] divide flag by 2; flag becomes odd after two loops
\$\endgroup\$
2
\$\begingroup\$

Piet + ascii-piet, 62 bytes (46×2=92 codels)

TABdDTvVvVnNNFJAQUiIiIIAeEeEuUUMTJBCSBvVvVNMLnNnNfFFVBQIMEiCcc

Try Piet online!

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

Pushy, 12 bytes

258s{K+12%h_

Try it online!

258            \ Push 258                            
   s           \ Split into digits, yielding [2, 5, 8]
    {K+        \ Add input to each
       12%     \ Modulo each by 12
          h    \ Increment each
           _   \ Print (space separated)

12 bytes

An alternative for the same byte count:

12R{:{;$...#

Try it online!

12R            \ Push range(1, 12), inclusive
   {: ;        \ Input times do:
     {         \   Rotate left
       $       \ While there are items on stack:
        ...    \   Pop the top three
           #   \   Print top item
\$\endgroup\$
1
\$\begingroup\$

Haskell, 29 bytes

-2 bytes thanks to Laikoni.

f n=[mod(n+i)12+1|i<-[2,5,8]]

Try it online!

Not very original, no...

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

Swift, 30 bytes

{n in[2,5,8].map{($0+n)%12+1}}

Try it online!

Port of many other answers to Swift

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

Wolfram Language (Mathematica) 35 bytes

Range@12~RotateLeft~#~Take~{3,9,3}&

The above asserts, in infix notation, what can be expressed more clearly as

Function[Take[RotateLeft[Range[12],Slot[1]],List[3,9,3]]]

RotateLeft rotates Range[12], the sequence 1,2,...12, leftward by the input number. Slot[1] or # holds the input number, n.

For example, with n = 4,

Function[RotateLeft[Range[12],4]]]

returns the list

{5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4}

Take...{3,9,3} returns every third element in that list from position 3 through position 9, namely

{7, 10, 1}
\$\endgroup\$
1

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.