46
\$\begingroup\$

Many electronic devices, specially old ones, will show a blinking 12:00 when the time has not been set. The purpose of this challenge is to recreate this.

Specifically, the task is to display 12:00 and --:-- alternatively in an infinite loop.

The period should be 1 second, divided evenly in two periods of 0.5 seconds. Here "1 second" and "evenly" can be interpreted loosely. For example, it is acceptable if the code pauses for 0.5 seconds between displaying the strings, even if the resulting period will then be a little higher than 1 second. An initial pause before displaying the first string is acceptable.

Each new string can be displayed either by replacing the former string or in a new line. Trailing whitespace is allowed, except that if each new string is on a different line there should be no empty lines between consecutive strings.

Shortest code in bytes wins.

\$\endgroup\$
9
  • 1
    \$\begingroup\$ does this count as kolmogorov-complexity? \$\endgroup\$
    – FlipTack
    Jan 2, 2017 at 12:36
  • \$\begingroup\$ @FlipTack I think so, but I wasn't sure. Thoughts, anyone? \$\endgroup\$
    – Luis Mendo
    Jan 2, 2017 at 12:45
  • 1
    \$\begingroup\$ @LuisMendo I don't think so, I think the KG tag is mostly for a fixed string. This has more to it, the waiting and alternating strings. \$\endgroup\$
    – Riker
    Jan 2, 2017 at 13:25
  • \$\begingroup\$ Can submissions wait 0.5 seconds before showing initial output? \$\endgroup\$
    – FlipTack
    Jan 2, 2017 at 18:20
  • 1
    \$\begingroup\$ IMHO, the statement "Each new string can be displayed either by replacing the former string or in a new line" made this challenge not fun. \$\endgroup\$
    – Setop
    Jan 3, 2017 at 0:53

71 Answers 71

2
\$\begingroup\$

Q/KDB+, 61 Bytes

s:("12:00";"--:--")
b:1b
.z.ts:{b::not b;show s[b]}
\t 500

Explanation:

s:("12:00";"--:--")

Assign the values we wish to show in a list.

b:1b

Assign a boolean variable to flip between 0 and 1 easily.

.z.ts:{b::not b;show s[b]}

This is a KDB+ function which is invoked in set intervals by the timer function (\t) http://code.kx.com/wiki/Reference/dotzdotts
This executes the code inside the curly braces when \t is set. Show the value in s and then flipping the value of b between 0 and 1.

\t 500

Effectively calls .z.ts every 500ms.

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

Pushy, 22 bytes (non-competing)

`--:`wO`12:0`&["500oWF

This answer makes use of the two stacks, by flipping between them, printing the characters in turn:

`--:`    \ Push these characters to stack 1
 w       \ Mirror around center, yielding "--:--"
 O       \ On the second stack...
 `12:0`  \ Push these characters
 &       \ Duplicate the last (for :00)

 [        \ Infinitely:
  "       \   Print the current stack
  500oW   \   Wait 500 milliseconds
  F       \   Swap stacks
          \ (Implicit end loop)

The oW command is part of a set of experimental commands that postdate the challenge, making this answer non-competing.

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

Windows PowerShell, 46 55 bytes

function a{sleep -m 500;cls};for(){"12:00";a;"--:--";a}

Original Code:

a{sleep -m 500;cls};for(){"12:00";a;"--:--";a}
^ this won't work on other PCs. No idea why.
\$\endgroup\$
2
\$\begingroup\$

Powershell, 46 45 bytes

-1 byte, thanks @AdmBorkBork

for(){('12:00','--:--')[$i++%2]
sleep -m 500}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You shouldn't need the ; in the for loop. Link. \$\endgroup\$ Nov 5, 2018 at 16:58
2
\$\begingroup\$

C# (.NET Core), 138 131 127 bytes

class H{static int Main(){for(int n=1;;System.Threading.Thread.Sleep(500))System.Console.WriteLine((n^=1)>0?"--:--":"12:00");}}

Try it online!

Simple one-liner code.


C# (.NET Core), 127 bytes

class H{static int Main(){for(int n=1;;System.Threading.Thread.Sleep(500))System.Console.Write((n^=1)>0?"\r--:--":"\r12:00");}}

Try it online!

This code has same length with the code above, it does replace previous line.


-7 bytes by using for loops.
-4 bytes by using ternary operator.

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

Postscript 225 214

For fun only! Don't send this to a real printer!!

/Courier findfont 9 scalefont setfont{usertime 500 mod 5 lt{newpath 9 9 moveto 1 setgray 99 setlinewidth 99 99 lineto stroke 0 setgray}if newpath 9 9 moveto usertime 999 mod 500 lt{(--:--)}{(12:00)}ifelse show}loop

Try it:

gs -c '/Courier findfont 9 scalefont setfont{usertime 500 mod 5 lt{newpath 9 9 moveto 1 setgray 99 setlinewidth 99 99 lineto stroke 0 setgray}if newpath 9 9 moveto usertime 999 mod 500 lt{(--:--)}{(12:00)}ifelse show}loop'

or

cat <<eops >/tmp/blinkingTwelve.ps
%!
/Courier findfont
9 scalefont
setfont
{
  usertime 500 mod 5 lt {
    newpath
    9 9 moveto
    1 setgray
    99 setlinewidth
    99 99 lineto
    stroke
    0 setgray
  } if
  newpath
  9 9 moveto
  usertime 999 mod 500 lt {
    (--:--)
  } {
    (12:00)
  } ifelse
  show
} loop
eops

then

gs /tmp/blinkingTwelve.ps
gv /tmp/blinkingTwelve.ps

But don't try to open this with more sophisticated viewer and care about desktop thumbnailer!

\$\endgroup\$
1
  • \$\begingroup\$ If gs -c '...' command prompt a white page, you may have to grow the display window or use a smaller resolution: gs -r45 -c '...' or smaller papersize gs -r600 -g360x200 -c '...' \$\endgroup\$ Jan 3, 2017 at 12:27
2
\$\begingroup\$

QBasic (QB64) 55 53 Bytes

A$="12:00
B$="--:--
DO
?A$
_DELAY .5
SWAP A$,B$
LOOP

Saved 2 bytes as newline appears to terminate the string definitions.

SWAP makes all the difference. For vanilla QBASIC replace _DELAY with SLEEP S where S is an integer in seconds, but then the frequency is wrong.

\$\endgroup\$
1
  • \$\begingroup\$ Disappointingly replacing DO..LOOP with 1..GOTO 1 results in the same byte count \$\endgroup\$
    – Chris H
    Jan 3, 2017 at 15:07
1
\$\begingroup\$

Haskell, 79 bytes

import System.Posix.Unistd
m=(>>usleep 500000).putStrLn
g=m"12:00">>m"--:--">>g

m is a helper function that waits half a second before printing its argument. The main function g calls m "12:00", then m "--:--" and then itself again.

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

S.I.L.O.S, 61 bytes

lbla
def p printLine
p 12:00
wait 500
p --:--
wait 500
GOTO a

Try it online!

Unfortunately TIO will just do nothing forever, as it waits until execution to print this. But, if you are eager to test, you can use the GitHub link.

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

Arduino CC, 79 bytes

void loop(){Serial.print("12:00");delay(500);Serial.print("--:--");delay(500);}

I have no experience with C/C++, so I don't really know how I can golf this.

\$\endgroup\$
2
  • \$\begingroup\$ You could group Serial.print and delay into one separated function. \$\endgroup\$ Jan 3, 2017 at 7:39
  • \$\begingroup\$ @F.Hauri, for me it ended up being the same length \$\endgroup\$
    – Daniel
    Jan 3, 2017 at 7:43
1
\$\begingroup\$

Processing, 69 bytes

void draw(){println("12:00");delay(500);println("--:--");delay(500);}
\$\endgroup\$
1
\$\begingroup\$

Python 2, 68 Bytes

import time
while 1:
 for i in'12:00','--:--':print i;time.sleep(.5)
\$\endgroup\$
1
\$\begingroup\$

Java, 152 151 bytes (Thanks @AlexRacer)

public class c{public static void main(String[] a)throws Exception{int i=1;while(i>0){System.out.println(i++%2<1?"--:--":"12:00");Thread.sleep(500);}}}

Try it here! * (this compiler prints only once a second, couldn't find a better one)

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Change while loop to for(int i=0;;){...} and also i++%2==1 to i++%2<1 \$\endgroup\$
    – AlexRacer
    Jan 3, 2017 at 2:18
  • \$\begingroup\$ Changed this part (i++%2==1), but kept the while. Using your suggestion would even lose some bytes. Thanks for the feedback ;) \$\endgroup\$
    – Bonifacio
    Jan 3, 2017 at 10:11
  • \$\begingroup\$ You can save a few bytes by limiting this to a function, not a whole program (which is allowed): void f()throws Exception{ ... } \$\endgroup\$
    – user18932
    Jan 3, 2017 at 16:56
  • 1
    \$\begingroup\$ You can remove public in front of class (-7 bytes); remove the space between String[] a (-1 byte), change the while to for so you can put the int inside it: for(int i=1;i>0;){...} (-1 byte). And welcome to PPCG! :) If you haven't checked it out yet, I can recommend looking through Tips for golfing in Java. \$\endgroup\$ Jan 6, 2017 at 13:01
  • 1
    \$\begingroup\$ Wow, thanks for the tips and for the welcome, @KevinCruijssen! \$\endgroup\$
    – Bonifacio
    Jan 6, 2017 at 16:02
1
\$\begingroup\$

C, 77 bytes

#import<windows.h>
i;f(){for(;;i^=1)Sleep(83*printf(i?"\r--:--":"\r12:00"));}
\$\endgroup\$
2
  • \$\begingroup\$ Just curious, don't you need to write the complete program i.e. include main()? \$\endgroup\$
    – Aaron S
    Jan 4, 2017 at 3:43
  • \$\begingroup\$ @6packkid: If not explicitly specified the default is program or function. See this meta discussion: Default for Code Golf: Program, Function or Snippet? \$\endgroup\$
    – raznagul
    Jan 4, 2017 at 11:33
1
\$\begingroup\$

GameMaker Language, 47 bytes

a="12:00"if 9<c mod 20a="--:--"draw_text(0,0,a)

(Assuming a room speed of 20 fps)

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

Scala, 100 bytes

object C extends App{var p=1>2;while(2>1){p= !p;println(if(p)"12:00"else"--:--");Thread.sleep(500)}}
\$\endgroup\$
1
\$\begingroup\$

HTML + CSS, 147, 148 142 Bytes

*{font-family:Courier;left 0;background-color:white;position:absolute}@keyframes d{0%,49%{left:0}50%,to{left:-10em}}a{animation:d 1s infinite}
12:00<a>--:--

Explanation:

Basically, this uses two HTML elements: A text node containing 12:00 and an <a> tag containing --:--.

The selector in the CSS applies to both elements, giving both absolute positioning 0 to the left. This allows both elements to overlap. Then, I gave both elements a monospace font, making each take up equal widths. Finally, I set the background color to white, making sure that the <a> tag hides the text it overlaps.

Then, I use an animation to move the <a> tag offscreen every .5 seconds. To do this, the animation lasts one second, and shows the tag for the first 49%, then hides it for the last 50%.

Oh - finally, Courier is shorter than monospace, saving 3 bytes, but it might not work in all browsers/operating systems.

Update: +1 Byte

Turns out that Monaco isn't on everyone's computer. changed to using Courier instead at the expense of a byte.

Update: -6 Bytes

Forgot to remove a bit of whitespace. Oops.

Non minified code:

* {
  position: absolute;
  left: 0;
  font-family: Courier;
  background-color: white;
}
@keyframes d {
  0%, 49% {
    left: 0
  }
  50%, to {
    left: -10em
  }
}
a{animation: d 1s infinite}
12:00<a>--:--

\$\endgroup\$
3
  • \$\begingroup\$ @LuisMendo I saved 3 bytes by using the font Monaco instead of monospace :P - didn't know about support. I'll change it. \$\endgroup\$
    – Ben Aubin
    Jan 4, 2017 at 1:30
  • \$\begingroup\$ Yes, it works for me now \$\endgroup\$
    – Luis Mendo
    Jan 4, 2017 at 1:35
  • \$\begingroup\$ @LuisMendo cool. thanks a lot for letting me know. \$\endgroup\$
    – Ben Aubin
    Jan 4, 2017 at 1:38
1
\$\begingroup\$

tcl, 49

while 1 {lmap x 12:00\ --:-- {puts $x;after 500}}

Can be tried on https://goo.gl/8gXPzC

Thanks to people on tcl IRC channel.

tcl, 51

while 1 {puts 12:00;after 500;puts --:--;after 500}

Can be tried on https://goo.gl/Hb3sHq

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

PHP, 48 bytes

for(;;usleep(5e5))echo($n^=1)?"
12:00":"
--:--";

Run with -nr.

Replace instead of multiple lines with \r instead of \n (+2 bytes):

for(;;usleep(5e5))echo($n^=1)?"\r12:00":"\r--:--";
\$\endgroup\$
1
\$\begingroup\$

R 69 65 61 bytes

y=1;while(1){cat('\r',c('12:00','--:--')[y]);Sys.sleep(.5);y=(y==1)+1}

while(1){cat('\r12:00');Sys.sleep(.5);cat('\r--:--');Sys.sleep(.5)}

f=Sys.sleep;while(1){cat('\r12:00');f(.5);cat('\r--:--');f(.5)}

First time using the f = blah_function() trick, and it saved a few bytes. Used \r to replace existing string.

Fairly simple implementation:

f = Sys.sleep;      #store function for repeated usage
while(1){
    cat('\r12:00'); #print 12
    f(.5);          #sleep
    cat('\r--:--'); #print --
    f(.5)           #sleep again
}
\$\endgroup\$
1
\$\begingroup\$

REXX, 60, 54 bytes

d.='--:--'
d.1='12:00'
a=1
do b=0
  sleep(.5)
  a=\a
  say d.a
  end

(Indented for readability)

Edit: Turns out that the most naïve solution is even shorter:

do b=0
say '12:00'
sleep(.5)
say '--:--'
sleep(.5)
end

Tested with Regina REXX interpreter; some older implementations may not have the Delay function at all, or it may only support whole seconds.

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

Lua, 94 bytes

Unfortunately Lua has no built-in sleep function, so a loop is necessary.

x=1 z=os.clock while 1 do x=-x print(x<0 and"12:00"or"--:--") y=z()+.5 repeat until z()>y end

As a bonus, here's a Windows only one which is 109 bytes and which clears the screen each time:

x=1 z=os.clock while 1 do x=-x print(x<0 and"12:00"or"--:--") y=z()+.5 repeat until z()>y os.execute"cls"end
\$\endgroup\$
1
\$\begingroup\$

Python 2, 92 bytes

import time
t=0
while 1:u=t%2;v='--'*u;w=1-u;print v+'12'*w+':'+v+'00'*w;t+=1;time.sleep(.5)

Try it online!

Explanation

import time                                   Import the time module
t=0                                           Set a counter, 't', to 0
while 1:                                      Trigger an infinite loop
    u = t%2                                   Let 'u' be the remainder of t/2. This will alternate between 0 and 1 
    w = 1 - u                                 Let 'w' be the opposite of u
    v = '--'*u                                Python lets the repetition of strings by multiplying them by an int. Here we are either mulitplying by 1 (no effect) or 0 (turns it to an empty string)
    print v + '12'*w + ':' + v + '00'*w       Concatonating 4 strings and printing them. We are using string multiplication again on the '12' and '00'
    t += 1                                    Increment the counter variable
    time.sleep(0.5)                           Wait half a second
\$\endgroup\$
1
1
\$\begingroup\$

*><>, 30 bytes

cn"00:"ooo5Sdo"--:--"ooooo5Sdo

Online test.

I feel this is much simpler and more different than redstarcoder's algorithm, and thus I posted it as separate.

Replaces output in-place.

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

8th, 62 58 51 bytes

: p . cr .5 sleep ; : f "12:00" p "--:--" p recurse ;

When invoked, the word f will display blinking twelve according the "new line" format. This code leverages "tail-call elimination" implemented in 8th.

The following code (95 bytes long) will let word f display blinking twelve according the "replacing the former string" format:

: p 0 0 con:gotoxy con:print ; 
: f con:cls repeat "12:00" p .5 sleep "--:--" p .5 sleep again ;
\$\endgroup\$
1
\$\begingroup\$

ForceLang, 79 bytes

def d datetime.wait 500
def w io.writeln
label 1
w "12:00"
d
w "--:--"
d
goto 1

And here's a version in 125 bytes that actually clears the screen each time and blinks:

set z string.char 8
def d datetime.wait 500
set z z+z+z+z+z
label 1
io.write "12:00"
d
io.write z+"--:--"
d
io.write z
goto 1
\$\endgroup\$
1
\$\begingroup\$

SmileBASIC, 35 bytes

WAIT 30?"12:00
WAIT 30?"--:--
EXEC.

WAIT times are in frames (1/60 second), and EXEC. restarts the program (equivilant to EXEC 0 which runs the code that's in slot 0)

Version that clears the screen and adds 6 bytes:

WAIT 30CLS?"12:00
WAIT 30CLS?"--:--
EXEC.
\$\endgroup\$
1
\$\begingroup\$

Excel VBA (Win, Excel 32 bit Only), 97 Bytes

In a module:

Because VBA's Application.Wait() method cannot handle input for time periods of less than one second, a declaration of the kernel32 sleep method is used for the 500 ms wait period.

Declare Sub Sleep Lib "kernel32" (ByVal s&)

Note: 64-bit versions of VBA (VB7) require a PtrSafe tag after the Declare tag, and changing the ByVal s& call to ByVal s as LongPtr.

In the vbe immediates window:

Do:DoEvents:?"12:00":Sleep 500:?"--:--":Sleep 500:Loop
\$\endgroup\$
1
\$\begingroup\$

PKod, 26 bytes

PKod is an esoteric language that only has one accessible variable

Code:

l12=:o00yyl=-oo=:o=-ooyy<

Explanation:
l - if first char in code, print any NOP chars found
12 - two NOP chars to print
=: - set variable as :
00 - two more NOP chars
o - print variable's corresponding ascii char
y - wait 0.25 seconds
l - if not first char in code, clear the console
=-oo - set variable as - and print twice
< - go back to the start of the code
\$\endgroup\$
1
\$\begingroup\$

Dart, 103 bytes

import'dart:io';f({i=1}){while(i>0){print(i++%2>0?"12:00":"--:--");sleep(Duration(milliseconds:500));}}

Try it online!

Sometimes I hate how verbose and strongly typed Dart has to be :D

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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