Take the 2-minute tour ×
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.

From codegolf.com (not loading as of Sept 15, 2011) was one that was driving me crazy. Print to STDOUT a clock of an exacting form, reading time from STDIN and marking an h at the hour and m at minute (rounded down to a multiple of 5) and an x if they overlap, unused ticks are marked with an o, so that

echo "23:13" | perl script.pl

yields

        o
    h       o

 o             m

o               o

 o             o

    o       o
        o

Mine is:

$/=':';($h,$m)=<>;printf'%9s
%5s%8s

%2s%14s

%s%16s

%2s%14s

%5s%8s
%9s',map{qw'o h m x'[2*($_==int$m/5)^$_==$h%12]}map{$_,11-$_}0..5

for 136 characters, using Perl. I wish I could get on the site, but I seem to recall that the leaders were under 100, also using Perl. Can someone else beat it?

share|improve this question
6  
perlmonks.org/?node_id=811919 –  Peter Taylor Sep 16 '11 at 6:13
    
A fascinating read :) –  J B Sep 16 '11 at 11:40
    
@Peter, its the goldmine! Thanks for finding that, I wonder if mine can adapt their techniques? –  Joel Berger Sep 16 '11 at 11:52
1  
Is there a serious reason why to use something in such lemon shape, instead of a circle? –  user unknown Jan 31 '12 at 3:39
1  
@userunknown: The original task was probably designed using a wider font. –  hammar Jan 31 '12 at 19:43

8 Answers 8

Golfscript, 75 bytes

Inspired by the article linked in the comments.

':'/{~}/5/:m;12%:h;"XXXXXXXXXXXX"{..318\%9/' '*@12%.h=2*\m=+'omhx'=@85/n*}%

The XXXXXXXXXXXX part represents 12 bytes of data, some of them unprintable, and should be replaced with the byte sequence 120 47 253 22 194 9 183 44 196 55 125 246.

For convenience, here is a base64 encoded version:

JzonL3t+fS81LzptOzEyJTpoOyJ4L/0Wwgm3LMQ3ffYiey4uMzE4XCU5LycgJypAMTIlLmg9Mipc
bT0rJ29taHgnPUA4NS9uKn0l

Example run:

$ echo -n "JzonL3t+fS81LzptOzEyJTpoOyJ4L/0Wwgm3LMQ3ffYiey4uMzE4XCU5LycgJypAMTIlLmg9MipcbT0rJ29taHgnPUA4NS9uKn0l" | base64 -d > saving-time.gs
$ ls -l saving-time.gs 
-rw-r--r-- 1 ahammar ahammar 75 2012-01-29 17:31 saving-time.gs
$ ruby golfscript.rb saving-time.gs <<< "15:37"
        o
    o       o

 o             o

o               h

 o             o

    m       o
        o
share|improve this answer

Python, 175 chars

h,m=map(int,raw_input().split(':'))
S=([' ']*17+['\n'])*11
for i in range(12):p=1j**(i/3.);S[98+int(8.5*p.imag)-18*int(5*p.real)]='ohmx'[2*(i==m/5)+(i==h%12)]
print''.join(S),

Doesn't beat your Perl code, but maybe a more concise language with built in complex numbers (or trig functions) could use this idea to do better.

share|improve this answer

C, 259 244 163 chars

Replaced command line argument with stdin (as required, also ends up shorter).
Removed support for minutes above 59 - a waste of 3 characters.
The code is shown with line breaks and indentation, but characters were counted without them.

main(i,h,m,x){
    scanf("%d:%d",&h,&m);
    for(i=0;i<12;
        printf("%*c","IEHBNAPBNEHI"[i++]-64,"ohmx"[(x==h%12)+2*(x==m/5)]))
        x=i%2?puts("\n"+(i%10==1)),11-i/2:i/2;
    puts("");
}

Older attempt (using ANSI escape codes), 244 chars:

f(x,y)char*y;{printf("\033[%d%c",x>0?x:-x,y[x<0]);}
main(i,h,m,x){
    char*v="HIJJJJIGFFFFPKJHFDCCDFHJ";
    f(i=f(scanf("%d:%d",&h,&m),"J")*0,v);
    for(;i<12;i++)
        (x=v[i+12]-72)&&f(x,"CD"),
        f(v[i]-72,"BA"),
        putchar("omhx"[2*(i==h%12)+(i==m%60/5)]);
    f(i,"B");
}
share|improve this answer

Python, 226 chars

h,p,s=raw_input().split(':'),['o']*12,[0,11,1,10,2,9,3,8,4,7,5,6]
a,b=int(h[0])%12,int(h[1])/5
p[a],p[b]='h','m' if a!=b else 'x'
print '%9s\n%5s%8s\n\n %s%14s\n\n%s%16s\n\n %s%14s\n\n%5s%8s\n%9s'%tuple([p[i] for i in s])

Usage: run 'python script.py' then type the required time. (Ex: 09:45)

Output:

        o
    o       o

 o             o

x               o

 o             o

    o       o
        o
share|improve this answer

My perl solution:

use POSIX;$/=':';@ss=(8,4,1,0,1,4,8);@sn=(0,7,13,15,13,7,0);$h=<stdin>;
$m=<stdin>;if($h>12){$h=$h-12;}$m=floor($m/5);
for($c=0;$c<7;$c++){for($s=0;$s<$ss[$c];$s++){printf(" ");}
$ac='o';if($h>5&&$h-6==6-$c){$ac='h';}if((($m>5)&&$m-6==6-$c)||($m==$c)&&($c==0)){
if($h>5&&$h-6==6-$c){$ac='x';}else{$ac='m';}}
print($ac);for($s=0;$s<$sn[$c];$s++){printf(" ");}$bc='o';if($h<6&&$h==$c){$bc='h';}
if($m<6&&$m==$c){if($h<6&&$h==$c){$bc='x';}else{$bc='m';}}
if($sn[$c]){print($bc);}printf("\n");if($c&&($c!=5)){printf("\n");}}

527 bytes

My C solution:

main(){int x[]={8,4,1,0,1,4,8},y[]={0,7,13,15,13,7,0}
,h,m,z,c,s;scanf("%d:%d",&h,&m);h>12?h-=12:h;m/=5;
for(c=0;c<7;c++){for(s=0;s<x[c];s++){printf(" ");}z='o';
if(h>5&h-6==6-c){z='h';}if((m>5&m-6==6-c)|(m==c)&!c){
z='m';if(h>5&h-6==6-c){z='x';}}printf("%c",z);
for(s=0;s<y[c];s++){printf(" ");}z='o';if(h<6&h==c){
z='h';}if(m<6&m==c){z='m';if(h<6&h==c){z='x';}}
if(y[c]){printf("%c",z);}printf("\n");if(c&&(c!=5)){printf("\n");}}}

440 bytes

share|improve this answer
1  
a quick suggestion on the Perl: you don't need posix, you can read from stdin with simply <> and I don't think you need to printf the way you are using it, print will work :) –  Joel Berger Jan 29 '12 at 14:24

Scala 327 chars

object C extends App{
val R=List(2,4,6,8,10)
val r=1::R:::11::R.reverse
val C=List(3,6,12,18,21)
val c=1::C:::21::C.reverse
def p(n:Int,i:Char){
val z=r(n)
val s=c((n+3)%12)
printf("[%d;%dH"+i,z,s)}
val t=readLine.split(":")
val h=t(0).toInt%12
val m=t(1).toInt/5
(0 to 11).map(x=>p(x,'o'))
p(h,'h')
p(m,'m')
if(h==m)p(h,'x')}

in use with a proper circle shape, not your lemon-XXXX:

clear  && echo 5:43 | scala C 
           o
     o           o

  o                 o

o                    o

  m                 o

     o           h
           o

ungolfed:

object Clock {
 val R = List (2,4,6,8,10)
 val r = 1 :: R ::: 11 :: R.reverse
 val C = List (3,6,12,18,21)
 val c = 1 :: C ::: 22 :: C.reverse

 def pos (n: Int, i: Char)={
   val z = r (n) 
   val s = c ((n+3) % 12) 
   printf ("[%d;%dH" + i, z, s)
 }

 def main (args: Array [String]) {
   val t = args (0).split (":")
   val h = t (0).toInt % 12
   val m = t (1).toInt /  5
   (0 to 11).map (x=> pos (x, 'o'))
   pos (h, 'h') 
   pos (m, 'm')
   if (h == m) pos (h, 'x')
  }
}

Uses Ansi-Code for output at pos(y,x). Since we only need 5 Minutes accuracy, precalculated values for x and y seem to be shorter than handling sin and cos functions with their import.

share|improve this answer

Python, 176 characters

o=map(ord,' .@Set~lWC2&!/0ABTUfgu')
h,m=map(int,raw_input().split(':'))
print''.join([' ',['\n','ohmx'[2*(i==o[m/5])+(i==o[h%12])]][i in o[:12]]][i in o]for i in range(24,127))
share|improve this answer

Perl 131 characters

<>=~/:/;$h=$`%12;$m=$'/5;printf'%9s
%12$5s%8s

%11$2s%14s

%10$s%16s

%9$2s%14s

%8$5s%8s
%9s',map$_^$h?$_^$m?o:'m':$h^$m?h:x,0..11
share|improve this answer

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.