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.

Create a BCD binary clock in ASCII. As implied, it shall display time in six columns of dots:

Binary Clock

Each column is a binary-coded digit, reading hours, minutes and seconds from left to right. Hours are given in 24-hour format.

The time to display is read from the command-line or from stdin (whichever fits better). If empty, the current time should be used instead.

Output size is a little constrained here and we can only fit two lines of text. So you are required to put the dots into only two lines. The characters ., : and ' come in handy here.

Input

The time to display in ISO-8601 format, i.e. HH:MM:SS without fractional seconds. Mandatory 24-hour format. Input can be empty in which case the current time will be used.

Output

Two lines with represent the BCD binary clock as in the image above. Each line is exactly six characters long and the only allowed characters are space, period, colon and apostrophe.

Winner

Shortest code by byte count wins. In a tie, the solution posted first wins.

Sample input 1

 

(empty)

Sample output 1

     .
''. ::

Sample input 2

17:59:20

Sample output 2

 ..'  
.:..' 

Sample input 3

01:08:01

Sample output 3

   '  
 .   .
share|improve this question
add comment

12 Answers

up vote 5 down vote accepted

Golfscript (+ruby/date) - 46 chars

"#{`date`}"10>+9<" .':":S-.{4/12-S=}%n@{4%S=}%

You can replace `date` with Time.now if necessary (+2 chars).

share|improve this answer
    
I knew it was hopeless to even try Golfscripting ;-) –  Joey Feb 27 '11 at 2:25
add comment

Ruby, 107 103 99 94 90 characters

$><<[$/,(gets||Time.now.to_s[11,8]).tr(z=" .':","").bytes.map{|a|$><<z[a/4-12];z[a%4]}]*""

Note that the input shouldn't contain a linebreak, so to test the solution use something like echo -n "01:08:01" | ruby1.9 bcd.rb.

To test the default behavior, run it with ruby1.9 bcd.rb and enter ^D (i.e. a literal EOF).

share|improve this answer
    
There's no such thing as a literal EOF! :-) –  J B Mar 4 '11 at 20:17
1  
You can shave off 4 characters with Time.now.to_s[11,8]. –  Lars Haugseth May 4 '11 at 23:15
    
@Lars: Changed it, thanks! –  Ventero May 6 '11 at 17:23
add comment

Ruby - 124 chars

t=*((i=gets)=="
"? Time.now.strftime('%H%M%S'):i).tr(':
','').bytes
puts (0..1).map{|i|t.map{|_|" .':"[i<1? _/4-12:_%4]}*''}
share|improve this answer
add comment

C#

236 characters

using System;using System.Linq;class X{static void Main(){var d=Console.In.ReadToEnd().Replace(":","");for(int i=0;i<2;i++)Console.WriteLine(string.Join("",(d==""?DateTime.Now.ToString("HHmmss"):d).Select(c=>" .':"[i<1?c/4-12:c%4])));}}

Readable:

using System;
using System.Linq;
class X
{
    static void Main()
    {
        var d = Console.In.ReadToEnd().Replace(":", "");
        for (int i = 0; i < 2; i++)
            Console.WriteLine(string.Join("",
                (d == "" ? DateTime.Now.ToString("HHmmss") : d)
                .Select(c => " .':"[i < 1 ? c / 4 - 12 : c % 4])));
    }
}
share|improve this answer
1  
There's no need to do - 48 when you are doing % 4, as 48 % 4 == 0 –  Dogbert Feb 27 '11 at 0:14
    
@Dogbert: Brilliant, thank you! That also gave me the idea that c/4-12 is shorter than (c-48)/4... :) –  Timwi Feb 27 '11 at 1:36
add comment

Golfscript (+ Ruby), 63

."#{Time.now.strftime'%T'}"if':'-:|{4/12-" .':":@\=}%n|{4%@\=}%

Still a beginner with Golfscript. Don't hit me.

share|improve this answer
    
There's a few things to shorten (see my answer). Specific to what you have at the moment that isn't in my answer -- brackets can be omitted from ruby function calls, "" coerces to false so you can remove ,\. –  Nabb Feb 27 '11 at 2:30
    
@Nabb: Thanks. Well, Ventero already told me he had it down to 63, though I couldn't bring him to tell me how ;-). Still, dragging Ruby into this through the string interpolation feels like cheating, somehow. I think I'll stay with my preferred language below and just try to beat Ruby :-) –  Joey Feb 27 '11 at 2:33
    
well since the question required access to the current time, it's not really possible without resorting to ruby anyway –  gnibbler Feb 27 '11 at 23:48
    
@gnibbler: I know. Still, it's the only really unportable feature of Golfscript and I don't particularly like using it due to that ;-) –  Joey Feb 28 '11 at 18:53
add comment

Windows PowerShell, 81

Since I did not create an implementation when writing the task I feel myself eligible to golf it from the start, too :-)

-join" .':
"[(($d="$(date)$args"[-8..-1]-ne58)|%{($_-band12)/4})+,4+($d|%{$_%4})]

History:

  • 2011-02-26 23:50 (119) First attempt.
  • 2011-02-26 23:55 (108) Inlining fun.
  • 2011-02-27 00:03 (102) Optimized selecting the first line. [Math]::Floor is waaaayy too long. I hate that.
  • 2011-02-27 02:22 (101) I don't need : for the first line.
  • 2011-02-27 02:53   (95) Optimized getting input or current time.
  • 2011-03-04 20:36   (88) Only a single join remains. Indexing every character needed (including the line break) from a single string.
  • 2011-03-04 20:39   (83) Using 58 instead of : which auto-casts to integer as well – no more need to enclose $_ in quotes :-)
  • 2011-03-04 20:42   (81) The parentheses after the -join are no longer necessary.
share|improve this answer
add comment

Perl (84)

#!perl -pl
$_||=localtime;y/://d;/\d{6}/;$_=$&;y/0-9/    ....''/;$_.="\n$&";y/0-9/ .': .': ./
share|improve this answer
    
You can save one character by changing "\n$&" to $/.$& — common Perl-golf trick :) –  Timwi May 6 '11 at 1:05
    
@Timwi I could use a real newline there :) –  chinese perl goth May 7 '11 at 12:33
add comment

Python 3, 136 chars

import time
d=input()
for i in(0,1):print(''.join([" .':"[[c>>2,c&3][i]]for c in map(int,d.replace(':','')or time.strftime('%H%M%S'))]))
share|improve this answer
    
I get 137 when I do a wc -c. Good work tho. +1 –  Kazark Aug 18 '12 at 19:00
add comment

Python - 201 172 characters

from time import*
t=raw_input()
if not t:t=strftime('%H:%M:%S')
t=t.replace(':','')
for i in(0,2):print''.join(" .':"[int(bin(int(d))[2:].rjust(4,'0')[i:i+2],2)]for d in t)
share|improve this answer
    
How about: t=raw_input().replace(':','') / if not t:t=strftime('%H%M%S') — saves you 5 characters :) –  Timwi Feb 26 '11 at 23:00
    
@Timwi Thanks for that! –  Juan Feb 26 '11 at 23:04
add comment

Lua - 163 160 Chars

s=io.read():match"(%S+)"or os.date("%X",os.time())t=" .':"w=io.write for l=1,2 do for k in s:gmatch"(%d)"do y=1+select(l,(k-k%4)/4,k%4)w(t:sub(y,y))end w"\n"end
share|improve this answer
add comment

Scala, 222:

object C{def main(a:Array[String]){
val t=if(a.length==0)new java.text.SimpleDateFormat("HHmmss").format(new java.util.Date)else a(0).replaceAll(":","")
t.map(c=>print(" ,'"(c/4%4)));println;t.map(c=>print(" ,':"(c%4)))}}
share|improve this answer
add comment

VimScript, 143 characters

if getline(1)==''
norm"=strftime("%T")^Mp
en
nm- "ax"=@a/4^MgP
nm, "ax"=@a%4^MgP
norm0YP--x--x--xP^M,,x,,x,,xP
%s/0/ /g
%s/1/./g
%s/2/'/g
%s/3/:/g
share|improve this answer
add comment

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.