44
\$\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 '17 at 12:36
  • \$\begingroup\$ @FlipTack I think so, but I wasn't sure. Thoughts, anyone? \$\endgroup\$ – Luis Mendo Jan 2 '17 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\$ – Rɪᴋᴇʀ Jan 2 '17 at 13:25
  • \$\begingroup\$ Can submissions wait 0.5 seconds before showing initial output? \$\endgroup\$ – FlipTack Jan 2 '17 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 '17 at 0:53

67 Answers 67

1 2
3
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\$
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 '18 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 at 18:11
  • \$\begingroup\$ Suggest 1<<19 instead of 500000 \$\endgroup\$ – ceilingcat Jan 2 at 7:13
  • \$\begingroup\$ @ceilingcat 2^19 doesn't exactly equal 500,000. \$\endgroup\$ – Logern Jan 3 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\$
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\$ – F. Hauri Jan 3 '17 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 '17 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 '17 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 '17 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, privacy policy and cookie policy

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