Output the hours at 90 degrees

26

1

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!

Charlie

Posted 2017-12-29T21:03:52.077

Reputation: 11 448

May we take the input as 0-indexed? Like, 0 -> 4, 7, 10? – Mr. Xcoder – 2017-12-29T21:20:04.253

8@Mr.Xcoder sorry, this time I'm going to say no. – Charlie – 2017-12-29T21:26:37.580

3Is this the fourth challenge now based on some activity involving your kids? :P – FlipTack – 2017-12-29T21:58:12.407

3@FlipTack Perhaps we need an inspired-by-kids tag ;) – Steadybox – 2017-12-29T22:13:31.500

6@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... – Charlie – 2017-12-29T22:17:05.080

@Steadybox That would be a meta tag, which is BAD. But yes, we do. (Perhaps a meta question with links would suffice?) – wizzwizz4 – 2018-01-01T10:32:56.533

Answers

16

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!

xnor

Posted 2017-12-29T21:03:52.077

Reputation: 115 687

9

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]

Jonathan Allan

Posted 2017-12-29T21:03:52.077

Reputation: 67 804

Alternative solution: 12Rṙm-3Ḋ (output in reverse order) – user202729 – 2017-12-30T02:14:36.140

8

Python 2, 35 bytes

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

Try it online!

notjagan

Posted 2017-12-29T21:03:52.077

Reputation: 4 011

6

R,  29  28 bytes

Thanks to @Giuseppe for saving a byte!

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

Try it online!

Steadybox

Posted 2017-12-29T21:03:52.077

Reputation: 15 798

6

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]

Luis Mendo

Posted 2017-12-29T21:03:52.077

Reputation: 87 464

5

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.

Graham

Posted 2017-12-29T21:03:52.077

Reputation: 3 184

Quite neat. I like that indexed rotation approach – Uriel – 2017-12-30T18:47:25.543

4

C (gcc), 48 bytes

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

Try it online!

Steadybox

Posted 2017-12-29T21:03:52.077

Reputation: 15 798

4

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));
}

Arnauld

Posted 2017-12-29T21:03:52.077

Reputation: 111 334

I had n=>[3,6,9].map(v=>(v+n)%12) then realized that it returns 0, not 12... – ericw31415 – 2017-12-30T00:47:45.227

@ericw31415 Actually, my 1st approach was n=>[3,6,9].map(v=>(v+n)%12||12) (but that's 31 bytes). – Arnauld – 2017-12-30T01:19:27.230

4

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.

Tom Carpenter

Posted 2017-12-29T21:03:52.077

Reputation: 3 990

Very nice, but the "boring" mod approach is shorter! Try it online!

– Giuseppe – 2017-12-30T12:45:10.590

@Giuseppe lol, I'm sure I tried using mod and couldn't make it shorter. Well done! Feel free to post as an answer. – Tom Carpenter – 2017-12-30T13:02:56.150

3

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.

James Holderness

Posted 2017-12-29T21:03:52.077

Reputation: 8 298

Neat. I didn’t know & did that – Jo King – 2017-12-30T06:14:22.803

3

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

– James Holderness – 2017-12-30T13:45:16.370

3

APL (Dyalog), 12 bytes

1+12|2 5 8∘+

Try it online!

Uriel

Posted 2017-12-29T21:03:52.077

Reputation: 11 708

2

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

Shaggy

Posted 2017-12-29T21:03:52.077

Reputation: 24 623

2

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: >[()])}<>

MegaTom

Posted 2017-12-29T21:03:52.077

Reputation: 3 787

2

Pyth, 12 bytes

%R12+LQ*R3S3    

Try it online!

Dave

Posted 2017-12-29T21:03:52.077

Reputation: 21

1Welcome to the site! :) – James – 2018-01-05T18:54:11.963

1

SOGL V0.12, 9 bytes

3{3+:6«‰P

Try it Here!

dzaima

Posted 2017-12-29T21:03:52.077

Reputation: 19 048

1

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

FlipTack

Posted 2017-12-29T21:03:52.077

Reputation: 13 242

1

Clean, 44 bytes

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

Try it online!

Οurous

Posted 2017-12-29T21:03:52.077

Reputation: 7 916

1

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

Ayb4btu

Posted 2017-12-29T21:03:52.077

Reputation: 541

1

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

streetster

Posted 2017-12-29T21:03:52.077

Reputation: 3 635

1

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

QunSyBer

Posted 2017-12-29T21:03:52.077

Reputation: 111

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

– James – 2017-12-31T19:20:57.317

1

Perl 6, 23 bytes

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

Try it online!

Sean

Posted 2017-12-29T21:03:52.077

Reputation: 4 136

1

PHP, 37+1 bytes

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

Run as pipe with -nR or try it online.

Titus

Posted 2017-12-29T21:03:52.077

Reputation: 13 814

1

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

Doorknob

Posted 2017-12-29T21:03:52.077

Reputation: 68 138

Creating a variable that holds the value 3 m3*33 allows you to replace the three +nn1s with one +nn3 https://tio.run/##BcE9DoMwDAbQPbf4pDiAKUNkiYGB9gAcoUvFj5TBTtQObLl6@t712c/Wejrca3h7fuydzzklJdbIM41EFKOC1ViFRbSyQAO@CEg/BF9MsGyjmZAZlbr6ZHdda56A@MTmWpv/

– user41805 – 2018-01-01T15:56:22.580

@Cowsquack Note that the output in your link is wrong. – Doorknob – 2018-01-01T16:29:19.090

1

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 

reffu

Posted 2017-12-29T21:03:52.077

Reputation: 1 361

0

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

totallyhuman

Posted 2017-12-29T21:03:52.077

Reputation: 15 378

0

Tcl, 45 bytes

proc R i {lmap c {2 5 8} {expr ($i+$c)%12+1}}

Try it online!


Different approach using the command line arguments:

Tcl, 39 bytes

time {puts [expr [incr argv 3]%12+1]} 3

Try it online!

sergiol

Posted 2017-12-29T21:03:52.077

Reputation: 3 055

0

Swift, 30 bytes

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

Try it online!

Port of many other answers to Swift

Herman L

Posted 2017-12-29T21:03:52.077

Reputation: 3 611

0

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}

DavidC

Posted 2017-12-29T21:03:52.077

Reputation: 24 524

34 bytes – user202729 – 2017-12-31T02:20:32.520

0

Pyt, 12 11 bytes

258á←+26*%⁺

Explanation:

258               Pushes 2,5,8 onto the stack
   á              Converts the stack to an array, and pushes the array onto the now-empty stack
    ←+            Adds input to [2,5,8]
      26*%⁺       Mod 12 plus 1 (the 'plus 1' allows for '12' to be output)

Try it online!

mudkip201

Posted 2017-12-29T21:03:52.077

Reputation: 833

0

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

stevefestl

Posted 2017-12-29T21:03:52.077

Reputation: 539

0

Perl 5, 30 bytes

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

Try it online!

mik

Posted 2017-12-29T21:03:52.077

Reputation: 208

0

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.

AdmBorkBork

Posted 2017-12-29T21:03:52.077

Reputation: 41 581

0

Octave, 23 bytes

@(x)mod(x+[2 5 8],12)+1

Try it online!

Takes the same approach as most others; and it is golfier than Tom Carpenter's somewhat more unique solution.

Giuseppe

Posted 2017-12-29T21:03:52.077

Reputation: 21 077

0

Java 8, 46 bytes

n->new int[]{(2+n)%12+1,(5+n)%12+1,(8+n)%12+1}

Try it online.

Kevin Cruijssen

Posted 2017-12-29T21:03:52.077

Reputation: 67 575

0

Ruby, 29 bytes

->n{3.times{p 12+(n+=3)%-12}}

G B

Posted 2017-12-29T21:03:52.077

Reputation: 11 099