22
\$\begingroup\$

Challenge:

Create a function or program that will take an input N (a number from 0 - 24) which represents the HOUR.

Output should draw an analog clock out of asterisks showing the hour N.

🕛🕐🕑🕒🕓🕔🕕🕖🕗🕘🕙🕚🕛🕐🕑🕒🕓🕔🕕🕖🕗🕘🕙🕚🕛


Notes:

•The minute hand must be longer than the hour hand (in terms of number of asterisks used)

•The minute hand must be at least 2 asterisks.

•Noon, 3pm, 6pm, and 9pm will always be orthogonal. Your output for intermediary hours, when arranged by degrees, should be in the same order as the hours of the day. What I mean is at noon the hour hand is 0º and at 3pm the hour hand is 90º so for your output cases of 1pm and 2pm the following must be true: 0pm_deg < 1pm_deg < 2pm_deg < 3pm_deg. Apart from this rule the actual degree amount can be whatever you decide, obviously it will change depending on font and other native factors.

•The connecting joint between the hour hand and minute hand is a circular/oval unicode character of any kind. o, O, •, 0, etc.


Example Input / Output (May vary depending on drawing styles)

 N = 3;

 *
 *
 *
 O  *  *

 N = 4;

 *
 *
 *
 O
    *
       *

 N = 5;

 *
 *
 *
 0
  *
   *

 N = 6;

 *
 *
 o
 *

 N = 7;

      *
      *
      *
      •
    *
  *

 N = 0,12,24;

 *
 *
 *
 o

Example of Differentiating Outputs:

 N = 4     VS     N = 5     VS     N = 6

  *                *                *
  *                *                *
  O                O                O
      *              *              *

This is , so the program with the shortest bytecount wins!

\$\endgroup\$
  • 8
    \$\begingroup\$ Hours usually are from 0-23 or 1-12 not 0-24 unless you have 25 hours in a day \$\endgroup\$ – fəˈnɛtɪk Feb 16 '17 at 19:16
  • 1
    \$\begingroup\$ The minute hand must be longer than the hour hand, but do they need to maintain their sizes? E.g., could the minute hand for 3 be a different size than the minute hand for 7? \$\endgroup\$ – AdmBorkBork Feb 16 '17 at 19:18
  • \$\begingroup\$ @LliwTelracs but 23:00 is 11:00. In my mind I was trying to loop around back to the 12 hand again \$\endgroup\$ – Albert Renshaw Feb 16 '17 at 19:19
  • 6
    \$\begingroup\$ @LliwTelracs Some days sure feel like they do! \$\endgroup\$ – user65606 Feb 16 '17 at 21:18
  • 1
    \$\begingroup\$ Are leading or trailing blanks/newlines accepted? What about padding? \$\endgroup\$ – Titus Feb 17 '17 at 11:33

12 Answers 12

18
\$\begingroup\$

Javascript (ES6), 105 76 65 bytes

F=N=>`  *
ab*12
 9@3
87654`.replace(/\w/g,y=>'0x'+y^N%12?' ':'*')
<!-- Snippet Demo: -->
N =  <input type="number" min="0" max="24" value="2" id="I" />
<button onclick="console.log(F(I.value))">Run</button>

  • -29 bytes:

    • Reduced size of minute and hour hand to 2 and 1 asterisks respectively. Smaller clock = less bytes :-P
  • -11 bytes:

    • Changed string comparison to numeric comparison.
    • y==(N%12).toString(16)?'*':' ' -> '0x'+y^N%12?' ':'*'

Original with longer hands: (105 94 bytes)

F=N=>`    *
a b * 1 2
  ab*12
  99@33
  87654
8 7 6 5 4`.replace(/\w/g,y=>'0x'+y^N%12?' ':'*')
<!-- Snippet Demo: -->
N =  <input type="number" min="0" max="24" value="2" id="I" />
<button onclick="console.log(F(I.value))">Run</button>

\$\endgroup\$
  • 1
    \$\begingroup\$ That's really clever! \$\endgroup\$ – AdmBorkBork Feb 16 '17 at 20:27
  • 1
    \$\begingroup\$ Oh wow I like this a lot! \$\endgroup\$ – Albert Renshaw Feb 16 '17 at 20:34
  • \$\begingroup\$ Oops. My Python answer looks a bit like your original. I posted before I saw your answer, honestly. Guess we had the same idea with hex. \$\endgroup\$ – ElPedro Feb 16 '17 at 20:43
  • 2
    \$\begingroup\$ and I somehow read it as "The hour hand must be at least 2..." so +1 for reading it properly and shortening the hands. \$\endgroup\$ – ElPedro Feb 16 '17 at 20:51
  • 1
    \$\begingroup\$ @ElPedro that's what I thought I read originally also :) \$\endgroup\$ – nderscore Feb 16 '17 at 20:54
14
\$\begingroup\$

CSS/HTML(JavaScript), 62 + 106 = 180 168 bytes

pre{position:absolute;left:99px;bottom:99px;transform-origin:50% 80%}
<input oninput=h.style.transform=`rotate(${this.value*30}deg)`><pre>*
*
o</pre><pre id=h>*

</pre>

Edit: Saved 9 bytes thanks to @nderscore.

\$\endgroup\$
  • \$\begingroup\$ Very cool idea! \$\endgroup\$ – nderscore Feb 16 '17 at 22:16
  • \$\begingroup\$ Really nice answer. \$\endgroup\$ – ElPedro Feb 16 '17 at 22:28
  • \$\begingroup\$ You can shave 4 bytes off your input tag by swapping it with this <body onload=f(prompt())> \$\endgroup\$ – Albert Renshaw Feb 16 '17 at 22:48
  • 1
    \$\begingroup\$ You could also embed the function code in the oninput attribute directly: <input oninput=h.style.transform=`rotate(${this.value*30}deg)`> \$\endgroup\$ – nderscore Feb 16 '17 at 23:08
9
\$\begingroup\$

Python 2, 148 140 135 bytes

-8 by deleting some leftover spaces and an unneeded newline.

-5 by changing (' ','*') to ' *'

a=input()
b='''    *
a b * 1 2
  ab*12
9 9 o 3 3
  87654
8 7 6 5 4'''
for x in range(1,12):b=b.replace(hex(x)[2],' *'[x==a%12])
print b

Try it online!

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

C (gcc), 144 bytes

#define v ,t
h;t(i){putchar(i>12?i-9:h^i?32:42);}n(i){h=i%12;puts("  *")v(10)v(11)v(h)v(1)v(2)v(19)v(12)v(9)v(57)v(3)v(19)v(8)v(7)v(6)v(5)v(4);}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ +1; Holy recursion! I find it hilarious that you can save bytes in a code-golf challenge by macroing a comma ahha \$\endgroup\$ – Albert Renshaw Feb 17 '17 at 3:16
  • \$\begingroup\$ I wasn't expecting it either, though there's no recursion in sight. This is a hard coded answer, albeit slightly obfuscated. \$\endgroup\$ – Ahemone Feb 17 '17 at 4:53
  • \$\begingroup\$ I don't think you need the space before the comma \$\endgroup\$ – ceilingcat Nov 13 at 3:23
4
\$\begingroup\$

SmileBASIC, 90 88 74 bytes

INPUT H?("  *"+" "*47)*2;"  o
R=H/1.9LOCATE 2.5+SIN(R)*2,3.5-COS(R)*2?"*

Example output:

? 5
  *
  *
  o

   *

? 3
  *
  *
  o *
\$\endgroup\$
3
\$\begingroup\$

Python 3, 86 85 bytes

-1 byte, better clockface (credit DuctrTape, as used in my Jelly answer)

def f(h):print(*([c,' *'[h%12+64==ord(c)]][c>'@']for c in'''
K*A
J*B
I0C
HFD
G E'''))

Try it online!

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

Jelly, 35 34 33 bytes

ị“ tI©’Œ?Ḋ¤µ⁶ẋ13”*⁸¦Ṗ;“**o”ṙ7s5ZY

Try it online! (tI©!) or see all (0 - 24).

How?

ị“ tI©’Œ?Ḋ¤µ⁶ẋ13”*⁸¦Ṗ;“**o”ṙ7s5ZY - Main link: h
          ¤                       - nilad followed by link(s) as a nilad:
 “ tI©’                           -     base 250 number: 522956007
       Œ?                         - shortest permutation of [1,N] that would be at
                                     that index in a lexicographically sorted
                                     list: [2,3,4,5,6,7,1,12,11,10,9,8,13]
         Ḋ                        - dequeue: [3,4,5,6,7,1,12,11,10,9,8,13]
ị                                 - index into (1-based and modular, so h=2 picks 4,
                                     as does h=14 [as do h=26, h=-10, etc.])
           µ                      - monadic chain separation (call the result j)
            ⁶                     - space character
             ẋ13                  - repeat 13 times
                   ¦              - apply to index...
                  ⁸               - ...left argument (j)
                ”*                -     an asterisk character (replacemes the space
                                         at index j with an asterisk)
                    Ṗ             - pop the last character off the list †
                     ;“**o”       - concatenate "**o"
                           ṙ7     - rotate left by 7 ‡
                             s5   - split into chunks of length 5 (columns)
                               Z  - transpose (get the rows of the clock)
                                Y - join with line feeds
                                  - implicit print

Note that “ tI©’Œ?Ḋ (permutation at index, dequeued) is a byte save over “9ȧỤ_ÑḶ’b⁴ (base 16 of), which is a byte save over “¦þxṙ<ȥ’ḃ13 (bijective base 13 of).

Regarding the pop - we have an extra space in the string, which is where the asterisk for 0, 12, 24 will go to to allow mod-12 indexing of the list of indexes, popping the last character off there is byte-cheaper than doing the concatenation of “**o” beforehand and overwriting one of those asterisks.

Regarding the rotation - this is a byte save over constructing a string with the `“**o” in the middle (either with it before or after the asterisk placement).

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

Haskell, 148 bytes

c n=mapM_ putStrLn$(t(n-3)!(t n!(\_->'*')))(map(\x->(2!(\_->x))"     ")"**O  ")
n!f=(\(x,a:y)->x++f a:y).splitAt n
t n=2+truncate(2.5*sin(n*pi/6))

Function c is the one that solves the given task

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

Bash + Unix utilities, 57 bytes

tr `dc -e12o$1O%p` \*<<<'B*1
A*2
9O3
864
7 5'|tr 1-9AB \ 

(There's a space after the final backslash.)

Try it online!

Not a very pretty clock, but it meets all the requirements.

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

PHP, 71 bytes

1 asterisk hours hand, 2 asterisk minutes hand.

$s="  *
  *  
  O  
";$s[ord(BGHNTSRQPJDE[$argv[1]%12])&31]="*";echo$s;

takes input from command line argument; run with -nr.

  1. define template
  2. map hour to position (decode from letter) and set character at position to asterisk
  3. print
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 41 bytes

This is too slow for TIO.

14Ýœ•2!&ô÷‰•èv… *@y_2L11+¹12%0‚ìyå+èJ}3ô»

For similar code working on TIO try this

Pretty sure this can still be both golfed and sped up.
Explanation coming later.

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

Logicode 634 bytes

Couldn't figure out how to input so there is a var a=... for the input at the top of tio link

circ v(a)->cond((a&1000)+0)&a->0/a
circ n(a)->cond a&10000->((a&100)+0)|!(a&100)&((a&011)|100)/a
circ y(a)->cond((a&100)+0)&a->a&10011/a
var s=@100000
var m=@101010
var x=y(n(v(a)))
var e=x&1000
var f=(x&100)+0
var t=(x&10)+00
var o=(x&1)+000
circ r(a,b)->cond(a)->b/@a
out s+s+s+s+m
out r(e&t&!o,m)+s+r(o&e&t,m)+s+m+s+r(o&!e&!f&!t,m)+s+r(t&!o&!f&!e,m)
out s+s+r(e&t&!o,m)+r(e&t&o,m)+m+r(o&!t&!f&!e,m)+r(t&!o&!f&!e,m)
out r(e&o&!t,m)+s+r(e&o&!t,m)+s+@110000+s+r(t&o&!f&!e,m)+s+r(t&o&!f&!e,m)
out s+s+r(e&!t&!o,m)+r((f&t&o),m)+r(f&t&!o,m)+r(f&!t&o,m)+r(f&!t&!o,m)
out r(e&!t&!o,m)+s+r(f&t&o,m)+s+r(f&t&!o,m)+s+r(f&o&!t,m)+s+r(f&!t&!o,m)

Have given up on golfing this. Could make it shorter at the cost of making the clock look horrible though.

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.