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

1 2
3
1
\$\begingroup\$

Japt, 27 bytes

500t@ßÃOq"12:00--:--"ò5 gT°

Try it

500t@ßà                         :Run the programme again in 500ms
       Oq                       :Clear the output
         "12:00--:--"ò5         :Split the string "12:00--:--" into chunks of length 5
                        gT°     :Get the element at index T (initially 0), postfix incremented
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Not quite real-time output, @LuisMendo; explanation added. Figured the other version wouldn't be valid for that reason, thanks for clarifying. \$\endgroup\$
    – Shaggy
    Nov 8, 2018 at 14:58
1
\$\begingroup\$

C (clang), 51 bytes

f(i){puts(i?"12:00":"--:--");usleep(500000);f(!i);}

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ This doesn't work. 5e5 is a double. The implicit declaration of usleep will cause Clang to assume it is int usleep(double), and depending on the calling convention, it will either wait for a garbage value or call it with 549606724824 (5e5 interpreted as an integer) which is an EINVAL. \$\endgroup\$
    – EasyasPi
    Jan 1, 2021 at 18:11
  • \$\begingroup\$ Suggest 1<<19 instead of 500000 \$\endgroup\$
    – ceilingcat
    Jan 2, 2021 at 7:13
  • \$\begingroup\$ @ceilingcat 2^19 doesn't exactly equal 500,000. \$\endgroup\$
    – user36046
    Jan 3, 2021 at 17:14
1
\$\begingroup\$

GNU AWK, (until version 5.0.1), 64 bytes

This piece of code prints the strings on a new line. Only works in GNU AWK up to version 5.0.1, while the time extension (module) was not obsolete.

@load"time";BEGIN{for(;;){sleep(.5);print++t%2?"12:00":"--:--"}}

Clearing the screen with clear command (80 bytes)

This example uses the clear command, not native to Awk.

@load"time";BEGIN{for(;;){sleep(.5);system("clear");print++t%2?"12:00":"--:--"}}

Clearing the screen with \33[H\33[2J\33[3J (85 bytes)

But don't despair if there is no command to clear the screen on your system. Just hard code it. \33 is the ASCII for Escape in octal.

@load"time";BEGIN{for(;;){sleep(.5);print"\33[H\33[2J\33[3J"(++t%2?"12:00":"--:--")}}
\$\endgroup\$
1
\$\begingroup\$

QBasic, 51 bytes

LOCATE 1
IF TIMER*2MOD 2THEN?"12:00"ELSE?"--:--
RUN

Try it at Archive.org (but note that it's an infinite loop, so there's no way to exit the program besides closing/refreshing your browser).

Displays both strings on the top line of the screen. If you're using actual QBasic rather than QB64, you'll notice that the program doesn't clear the screen first, so there may be other junk present as well. This issue can be fixed at a cost of 8 bytes: add CLS:DO: at the beginning of the program and change RUN to LOOP.

Explanation

Waiting for a fraction of a second is hard in QBasic, as other answers have mentioned, since the built-in SLEEP command only takes integers. However, TIMER (which represents the number of seconds since midnight) is a floating-point value. Whenever its fractional part is less than .5, we'll print --:--, and whenever its fractional part is greater than .5, we'll print 12:00. Conveniently, the MOD operator truncates to integer, so we can use TIMER * 2 MOD 2 and get values of 0 and 1.

This approach prints many, many times per second, but most of the time it's printing the same value over itself, so nothing changes.

Ungolfed and commented:

LOCATE 1                  ' Move the cursor to the top row
IF TIMER * 2 MOD 2 THEN   ' Alternates between 0 and 1 with a period of 1 second
  PRINT "12:00"
ELSE
  PRINT "--:--"
END IF
RUN                       ' Restart the program
\$\endgroup\$
1
\$\begingroup\$

Stax, 22 20 bytes

å√Φ←mp═╠╗⌐fâêÇY)♀╗}%

Run and debug it

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

Dyalog APL, 35 31 30 bytes

Full program.

{⌽⍵⊣⎕←5↑⍵⊣⎕DL÷2}⍣≡'12:00--:--'

'12:00--:--' a string

{...}⍣≡ Repeatedly apply the below anonymous function until two successive results are identical (i.e. never)

÷2 reciprocal of two (one half)

⎕DL Delay that many seconds (returns elapsed time)

discard that in favor of

the argument

5↑ take the first five characters

⎕← display with newline

discard that in favor of

⌽⍵ reverse argument (implicitly returned)

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

05AB1E, 19 bytes

…--:ûŽmy3'::[=s₄;.W

Try it online! Outputs each string on a new line.

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

05AB1E, 23 20 19 bytes

"12:00"…--:û[s=₄;.W

Try it online.

Explanation:

"12:00"            # Push string "12:00"
       …--:        # Push string "--:"
           û       # Palindromize it to "--:--"
[                  # Loop indefinitely:
 s                 #  Swap the two values on the stack
  =                #  Print the top with trailing newline (without popping)
   ₄;              #  Push 1000 halved: 500
     .W            #  Sleep that many ms

"12:00" could alternatively be Žmy3':: for the same byte-count.

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

under construction, I missed the 1 second delay

awk, 3534 bytes (+11 for 12:00 --:--)

{for(;++i;i=i%2)print$(i)"\033[A"}

for loop alternates i's value between 1 and 2. echo feeds the values to the program into fields $1and $2, we then print $(i). Try it:

$ echo 12:00 --:-- | awk '{for(;++i;i=i%2)print$(i)"\033[A"}'

Edit: Thanks for the ANSI tip to @F.Hauri.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Ansi sequence use 1 as default, so the 1 is useless in \033[A. \$\endgroup\$ Jan 3, 2017 at 8:31
0
\$\begingroup\$

C 79 Bytes

f(){while(1){printf("\n12:00");usleep(500000);printf("\n--:--");usleep(500000);}}

so the main would look like int main(){ f(); return 0;}

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! Just so you know, the downvote was cast automatically by the Community user when you edited your answer. I consider this a bug. \$\endgroup\$
    – Dennis
    Jan 3, 2017 at 18:43
  • \$\begingroup\$ You have to write a full program or a function, so you'd have to wrap your code in f(){...} or something. Also, the spec allows printing each output on a separate line, so system("clear"); is not required. \$\endgroup\$
    – Dennis
    Jan 3, 2017 at 18:45
  • \$\begingroup\$ You still need the while loop in your submission. Also, you cannot pass a float to usleep. \$\endgroup\$
    – Dennis
    Jan 3, 2017 at 18:58
0
\$\begingroup\$

Python 3, 78 bytes

import time;x=0
while 1:x=not x;print("12:00--:--"[x*5:x*5+5]);time.sleep(0.5)

Try it online!

\$\endgroup\$
1 2
3

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.