26
\$\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\$
  • \$\begingroup\$ May we take the input as 0-indexed? Like, 0 -> 4, 7, 10? \$\endgroup\$ – Mr. Xcoder Dec 29 '17 at 21:20
  • 8
    \$\begingroup\$ @Mr.Xcoder sorry, this time I'm going to say no. \$\endgroup\$ – Charlie Dec 29 '17 at 21:26
  • 3
    \$\begingroup\$ Is this the fourth challenge now based on some activity involving your kids? :P \$\endgroup\$ – FlipTack Dec 29 '17 at 21:58
  • 3
    \$\begingroup\$ @FlipTack Perhaps we need an inspired-by-kids tag ;) \$\endgroup\$ – Steadybox Dec 29 '17 at 22:13
  • 6
    \$\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 '17 at 22:17

35 Answers 35

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!

| improve this answer | |
\$\endgroup\$
9
\$\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]
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Alternative solution: 12Rṙm-3Ḋ (output in reverse order) \$\endgroup\$ – user202729 Dec 30 '17 at 2:14
8
\$\begingroup\$

Python 2, 35 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
6
\$\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]
| improve this answer | |
\$\endgroup\$
6
\$\begingroup\$

R,  29  28 bytes

Thanks to @Giuseppe for saving a byte!

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

Try it online!

| improve this answer | |
\$\endgroup\$
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.
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Quite neat. I like that indexed rotation approach \$\endgroup\$ – Uriel Dec 30 '17 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!

| improve this answer | |
\$\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));
}

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I had n=>[3,6,9].map(v=>(v+n)%12) then realized that it returns 0, not 12... \$\endgroup\$ – ericw31415 Dec 30 '17 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 '17 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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Very nice, but the "boring" mod approach is shorter! Try it online! \$\endgroup\$ – Giuseppe Dec 30 '17 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\$ – Tom Carpenter Dec 30 '17 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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Neat. I didn’t know & did that \$\endgroup\$ – Jo King Dec 30 '17 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\$ – James Holderness Dec 30 '17 at 13:45
3
\$\begingroup\$

APL (Dyalog), 12 bytes

1+12|2 5 8∘+

Try it online!

| improve this answer | |
\$\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).

| improve this answer | |
\$\endgroup\$
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: >[()])}<>
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Pyth, 12 bytes

%R12+LQ*R3S3    

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to the site! :) \$\endgroup\$ – James Jan 5 '18 at 18:54
1
\$\begingroup\$

SOGL V0.12, 9 bytes

3{3+:6«‰P

Try it Here!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Clean, 44 bytes

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

Try it online!

| improve this answer | |
\$\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
| improve this answer | |
\$\endgroup\$
1
\$\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#.

| improve this answer | |
\$\endgroup\$
1
\$\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
| improve this answer | |
\$\endgroup\$
1
\$\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

| improve this answer | |
\$\endgroup\$
  • \$\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\$ – James Dec 31 '17 at 19:20
1
\$\begingroup\$

Perl 6, 23 bytes

{map ($_+*)%12+1,2,5,8}

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

PHP, 37+1 bytes

while($i++<3)echo(~-$argn+=3)%12+1,_;

Run as pipe with -nR or try it online.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

face, 96 94 bytes

(%d
@)\$*,c'$ooiim%*m1*6%+%%%11m!*mn*m~*3!m&!r!&!is!&$pn3!:L+nn1+nn1%nn%+nn1p~>$inw~>~o-!!1?!L

This simply adds two, mods by 12, adds one more, and prints. Then it does that two more times.

Commented version:

(%d
@)

\$*,c'$ooii     ( store format string in $, ip in *, get stdin/out )
m%*m1*6%+%%%11  ( initialize constants, %=12, 1=1 )
m!*mn*m~*       ( malloc space for a counter, input var, and length )
3!m&!r!&!i      ( read into & )
s!&$pn          ( scan into n )
3!:L            ( start of main loop, executed thrice )
  +nn1+nn1      ( add 2 to n )
  %nn%+nn1      ( mod by 12 and add 1 more )
  p~>$in        ( sprintf n into > )
  w~>~o         ( output to stdout )
  -!!1          ( decrement counter )
?!L             ( conditional jump back to loop start )

Try it online! (The trailing newline is required on TIO due to a bug that's been fixed in a newer version of face.)

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Creating a variable that holds the value 3 m3*33 allows you to replace the three +nn1s with one +nn3 tio.run/… \$\endgroup\$ – user41805 Jan 1 '18 at 15:56
  • \$\begingroup\$ @Cowsquack Note that the output in your link is wrong. \$\endgroup\$ – Doorknob Jan 1 '18 at 16:29
1
\$\begingroup\$

Forth (gforth), 39 bytes

Input is taken from the stack and output is placed on the stack

: a 2 + 12 mod 1+ ; : f a dup a dup a ;

Try it online!

Explanation

 : a 2 + 12 mod 1+ ; \ helper word to handle adding the hours
    2 +              \ Add 2 to the input
    12 mod           \ get the result modulo 12
    1+               \ add 1

 : f a dup a dup a ; \ word that calculates and outputs the result
    a dup            \ add 3 hours to the input and then duplicate the result
    a dup            \ add 3 hours to the duplicate then duplicate the result
    a                \ add 3 hours to the duplicate 
| improve this answer | |
\$\endgroup\$
0
\$\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...

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Swift, 30 bytes

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

Try it online!

Port of many other answers to Swift

| improve this answer | |
\$\endgroup\$
0
\$\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}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Windows Batch, 137 125 111 68 bytes

@set/ab=(%1+2)%%12+1,c=(%1+5)%%12+1,d=(%1+8)%%12+1
@echo %b% %c% %d%

Port of the add value to input and mod 12 + 1

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 30 bytes

sub{map{($_+$_[0])%12+1}2,5,8}

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 30 bytes

param($a)2,5,8|%{($_+$a)%12+1}

Try it online!

Port of the other answers (e.g., xnor's Python 2 answer). Ho-hum.

| improve this answer | |
\$\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.