Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Write a program which displays the current system time as an analog clock, using ASCII graphics. The clock must show at least the hour and minute pointers, and must have enough resolution of at least 5 minutes.

The display can be as small and as ugly as you wish, but the shown time should be easily recognizable.

The program has to run continuously and update its status. If your system can clear the screen, use it, otherwise it's enough to repaint the clock when its state changes.

You get a -20% bonus on the character count if the clock has at least a 1 minute resolution.

share|improve this question
When you say analog clock do you mean the usual one or any form of analog rappresentation of time? – Marco Martinelli Feb 25 at 19:08
Extra bonus for 1 second resolution? – Shmiddty Feb 25 at 20:10

4 Answers

up vote 2 down vote accepted

Mathematica 213 - 42 = 171

The ticks and hour labels are placed on the unit circle. H and M revolve around the clock center showing the whole number of completed hours and minutes, respectively. S updates its position several times each second.

Two versions are shown: a version that plots text in the Cartesian plane, and another that displays text characters in a grid.

This version plots the characters into the Cartesian plane.

d = Dynamic; t = Table; x = Text;i_~u~k_ := {Sin[2 \[Pi] i/k], Cos[2 \[Pi] i/k]};
d[{f = Date[], Clock[{1, 1}, 1]}]
Graphics[d@{t[x[".", u[i, 60]], {i, 60}],t[x[i, u[i, 12]], {i, 12}],
x["H", .7 u[f[[4]], 12]],x["M", .8 u[f[[5]], 60]],x["S", .9 u[f[[6]], 60]]}]

The clock below shows the time 3:08:17.

clock

Terminal or Grid Version: 430 316 chars (253 with bonus discount)

This version works much the same, but places the characters in a 61 x 61 cell grid rather than in the Cartesian plane. It could still be golfed a bit, but I merely wanted to show a (sloppier) terminal-like output in Mathematica.

d = Dynamic; i_~u~k_ := Round /@ (10 {Sin[2 \[Pi] (i + 3 k/4)/k], 
Cos[2 \[Pi] (i + 3 k/4)/k]}); d[{f = Date[], Clock[]}]
z = Round /@ (# u[f[[#2]], #3] + 11) -> #4 &;
t = Table[( u[i, 12] + 11) -> i, {i, 12}];
d@Grid[ReplacePart[ConstantArray["", {21, 21}],
Join[z @@@ {{.9, 5, 60, "M"}, {.8, 4, 12, "H"}},
DeleteCases[Table[( u[i, 60] + 11) -> "*", {i, 60}], x_ /; MemberQ[t[[All, 1]], x[[1]]]], t]]]

The clock below displays 11:06.

terminal clock

share|improve this answer
2  
Doesn't look like ASCII art to me. – Joe Z. Feb 23 at 20:22
1  
I followed the definition given in Wikipedia: "ASCII art is a graphic design technique that uses computers for presentation and consists of pictures pieced together from the 95 printable (from a total of 128) characters defined by the ASCII Standard from 1963 and ASCII compliant character sets with proprietary extended characters (beyond the 128 characters of standard 7-bit ASCII). The term is also loosely used to refer to text based visual art in general." – David Carraher Feb 23 at 20:27
1  
Hmm. I suppose that works. I expect that vsz was expecting terminal art, though. Perhaps he can clarify. – Joe Z. Feb 23 at 20:28

Python, 328 - 65 = 263

Prints a new clock every second, with the minute hand updating every minute.

import math,time
def p(t,r):c[int(25-r*math.cos(t))][int(25+r*math.sin(t))]='*'
while 1:
 time.sleep(1);c=[[' ']*50 for i in range(50)];t=time.localtime();h=t.tm_hour*6.283+t.tm_min/9.549
 for i in range(999):
    p(i/158.0,24);p(h,i*.02);p(h/12,i*.01)
    for q in range(12):p(q/1.91,24-i*.005)
 for y in range(50):print''.join(c[y])

The clocks it prints look like this (it's not as stretched in my terminal):

                  **************                  
               ****      *     ****               
             ***         *        ***             
           ***           *          ***           
          ** **          *         ** **          
         **   *                    *   **         
        **    **                  **    **        
       *       *                  *      **       
      **                                  **      
     **                                    **     
    **                                      **    
    *                                        *    
   ***                                      ***   
   * ***                                  *** *   
  **   **                                **   **  
  *                                            *  
  *                                            *  
 **                                            ** 
 *                                              * 
 *   *                                          * 
 *   ******                                     * 
 *        ******                                * 
 *             ******                           * 
 *                  *****                       * 
 *****                   *                  ******
 *                       **                     * 
 *                        **                    * 
 *                         *                    * 
 *                         **                   * 
 *                          **                  * 
 **                          **                ** 
  *                           *                *  
  *                           *                *  
  **   **                                **   **  
   * ***                                  *** *   
   ***                                      ***   
    *                                        *    
    **                                      **    
     **                                    **     
      **                                  **      
       *       *                  *       *       
        **    **                  **    **        
         **   *                    *   **         
          ** **          *         ** **          
           ***           *          ***           
             ***         *        ***             
               ****      *     ****               
                  **************                  
                         *                        
share|improve this answer
How does one read off the seconds? – David Carraher Feb 24 at 13:23
1  
One doesn't. It prints every second but only updates every minute. – cardboard_box Feb 24 at 13:49

Javascript 370 - 74 = 296

http://jsfiddle.net/wBKQ6/7/

(function loop(){
    M=Math;p=M.PI/2;z=M.pow;q=M.sqrt;d=new Date();h=(d.getHours()%12/3*p+p)%(p*4);m=(d.getMinutes()/15*p+p)%(p*4);s=(d.getSeconds()/15*p+p)%(p*4);e=49;o='';

    for(r=0;r<99;r++){
        for(c=0;c<99;c++){           
            d=q(z(r-e,2)+z(c-e,2));
            a=(M.atan2(e-r,e-c)+p*4)%(p*4);
            E=(d<e*.8&&M.abs(m-a)*d<.5) || (d<e*.5&&M.abs(h-a)*d<.5) || (d<e*1&&M.abs(s-a)*d<.5);
            o+=d-e>0||d<1||E||(e-d<5&&a%p==0)?'●':'○';
            //■□●○
        }
        o+='\n';
    }
    O.innerText=o
    setTimeout(loop,1000);
})()

Golfed (370):

!function L(){p=M.PI/2;q=p*4;P=M.pow;d=new Date();s=(d.getSeconds(S=d.getMinutes(e=40))/15*p+p)%q;m=(S/15*p+p)%q;h=(d.getHours(A=M.abs)%12/3*p+S/180*p+p)%q;for(r=o='';r<81;r++,o+='\n')for(c=0;c<81;){d=M.sqrt(P(r-e,2)+P(c-e,2));a=(M.atan2(e-r,e-c++)+q)%q;o+='○●'[d-e>0|d<e*.8&A(m-a)*d<1|d<e/2&A(h-a)*d<1|d<e&A(s-a)*d<1|e-d<5&a%p==0]}O.innerText=o;setTimeout(L,9)}(M=Math)

Sample Output (much more condensed in demo):

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
●●●●●●●●●●●●●●○○○○○○●○○○○○○●●●●●●●●●●●●●●
●●●●●●●●●●●●○○○○○○○○●○○○○○○○○●●●●●●●●●●●●
●●●●●●●●●●○○○○○○○○○○●○○○○○○○○○○●●●●●●●●●●
●●●●●●●●○○○○○○○○○○○○●○○○○○○○○○○○○●●●●●●●●
●●●●●●●○○○○○○○○○○○○○○○○○○○○○○○○○○○●●●●●●●
●●●●●●○○○○○○○○○○○○○○○○○○○○○○○○○○○○○●●●●●●
●●●●●○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○●●●●●
●●●●○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○●●●●
●●●●○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○●●●●
●●●○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○●●●
●●●○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○●●●
●●○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○●●
●●○○○○○○○○○○○○○○○○○○○○○○○○○●○○○○○○○○○○○●●
●○○○○○○○○○○○○○○○○○○○○○○○○○●●○○○○○○○○○○○○●
●○○○○○○○○○○○○○○○○○○○○○○○○●●○○○○○○○○○○○○○●
●○○○○○○○○○○○○○○○○○○○○○○○●●○○○○○○○○○○○○○○●
●○○○○○○○○○○○○○○○○○○○○○○●●○○○○○○○○○○○○○○○●
●○○○○○○○○○○○○○○○○○○○○○●○○○○○○○○○○○○○○○○○●
●○○○○○○○○○○○○○○○○○○○○●○○○○○○○○○○○○○○○○○○●
●●●●●○○○○○○○○○○○○○○○●○○○○○○○○○○○○○○○●●●●●
●○○○○○○○○○○○○○○○○○○●○●○○○○○○○○○○○○○○○○○○●
●○○○○○○○○○○○○○○○○○○●○○●○○○○○○○○○○○○○○○○○●
●○○○○○○○○○○○○○○○○○●○○○○●●○○○○○○○○○○○○○○○●
●○○○○○○○○○○○○○○○○○●○○○○○●●○○○○○○○○○○○○○○●
●○○○○○○○○○○○○○○○○●○○○○○○○●●○○○○○○○○○○○○○●
●○○○○○○○○○○○○○○○●●○○○○○○○○●●○○○○○○○○○○○○●
●●○○○○○○○○○○○○○○●○○○○○○○○○○○●○○○○○○○○○○●●
●●○○○○○○○○○○○○○●○○○○○○○○○○○○○●○○○○○○○○○●●
●●●○○○○○○○○○○○○●○○○○○○○○○○○○○○●○○○○○○○●●●
●●●○○○○○○○○○○○●○○○○○○○○○○○○○○○○●○○○○○○●●●
●●●●○○○○○○○○○○●○○○○○○○○○○○○○○○○○●○○○○●●●●
●●●●○○○○○○○○○●○○○○○○○○○○○○○○○○○○○●●○○●●●●
●●●●●○○○○○○○●●○○○○○○○○○○○○○○○○○○○○●●●●●●●
●●●●●●○○○○○○○○○○○○○○○○○○○○○○○○○○○○○●●●●●●
●●●●●●●○○○○○○○○○○○○○○○○○○○○○○○○○○○●●●●●●●
●●●●●●●●○○○○○○○○○○○○●○○○○○○○○○○○○●●●●●●●●
●●●●●●●●●●○○○○○○○○○○●○○○○○○○○○○●●●●●●●●●●
●●●●●●●●●●●●○○○○○○○○●○○○○○○○○●●●●●●●●●●●●
●●●●●●●●●●●●●●○○○○○○●○○○○○○●●●●●●●●●●●●●●
●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
share|improve this answer
I could probably bring the score below 263 if I remove the second hand, but I like it! – Shmiddty Feb 26 at 0:31
1  
Smiley faces like to be clocks too: jsfiddle.net/wBKQ6/17/show/light – Shmiddty Feb 26 at 17:25

Python 2 - 207

import time as T
while 1:t=T.localtime();m=t.tm_min/5;l=[12]+range(1,12);l[m]='';l[t.tm_hour%12]='H';l[m]+='M';a='  %s\n';print(a+('%s'+a)*5+a)%tuple(str(l[x])for x in[0,11,1,10,2,9,3,8,4,7,5,6]);T.sleep(9)

It is very ugly, but readable. Prints every 9 sec (you can change to 1 sec if you prefer), updates every 5 min. I don't have much experience with code golfing in python so I expect it can be improved.

Example output:

  12
11  1
10  2
9  M
8  4
7  H
  6
share|improve this answer
7  
Your clock appears to be a Dali. Bravo! – Shmiddty Feb 24 at 21:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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