25
\$\begingroup\$

This question already has an answer here:

I'm currently cooking us some cup noodles, but we're really sleepy at the moment. They'll be finished in 50 seconds from now, can you wake me up then?

Problem:

Write a program or function that waits 50 seconds of busy waiting (so that you too doesn't sleep) and then outputs at least 1 visible character to wake me up.

Busy waiting:

You can't use functions like sleep or delay or pause to wait for the 50 seconds, you have to keep checking the elapsed time.

Rules:

  • Your code must be somewhat consistent in time, between runs and between computers. A small error of +- 1 seconds is acceptable.
  • You can't output any visible characters between 1 and 50 seconds (let me sleep).
  • Whitespace characters (newline, space, tab, ...) are not considered visible characters for this challenge.
  • This is , so shortest code wins.
\$\endgroup\$

marked as duplicate by Beta Decay, Blue code-golf May 20 '17 at 10:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ Can the output be non-deterministic (something different every time)? \$\endgroup\$ – Comrade SparklePony May 7 '17 at 14:49
  • \$\begingroup\$ @ComradeSparklePony if it always outputs at least 1 visible character, sure \$\endgroup\$ – Felipe Nardi Batista May 7 '17 at 14:50
  • 26
    \$\begingroup\$ The problem with this approach is the risk that the CPU fan will wake you up early... \$\endgroup\$ – Neil May 7 '17 at 15:35
  • 4
    \$\begingroup\$ Is it ok to reboot the machine after printing the "visible characters"? \$\endgroup\$ – Matteo Italia May 7 '17 at 16:24
  • 1
    \$\begingroup\$ @TheLethalCoder it can be a value returned by a method, but when printed must have at least 1 visible character \$\endgroup\$ – Felipe Nardi Batista May 8 '17 at 14:33

36 Answers 36

0
\$\begingroup\$

Swift , 108 bytes

 var a=Date().addingTimeInterval(59.0);while(Date().timeIntervalSinceNow<a.timeIntervalSinceNow){};print("W")
\$\endgroup\$
  • \$\begingroup\$ Please add comment before doing a downvote. \$\endgroup\$ – Samaira May 8 '17 at 9:37
  • \$\begingroup\$ Welcome to PPCG! The goal of code-golf challenges is to use as few bytes as possible and the challenge allows to output only a single character as wake-up signal, so you don't need "WakeUp" as a string, just for example "W" would suffice. \$\endgroup\$ – Laikoni May 8 '17 at 9:38
  • \$\begingroup\$ Also can the spaces at the beginning be omitted? var a=Date(). \$\endgroup\$ – Laikoni May 8 '17 at 9:39
  • \$\begingroup\$ Agreed the space can be omitted also its written a tleast one character to wakeup that I didn't read. But there is no wrong logic used in this code. Neither a timer nor a sleep command. \$\endgroup\$ – Samaira May 8 '17 at 9:42
  • \$\begingroup\$ This solution is terribly inefficient. The purpose here is to shorten the code as much as possible. This answer is an outgolfing of this one. \$\endgroup\$ – Mr. Xcoder May 12 '17 at 12:44
0
\$\begingroup\$

VBA, 98 Bytes

Not sure if allowed...but here! It waits a single second, but doesn't wait the whole time. Still working on the language!

Sub Tm()
For i = 0 To 50
    If i = 50 Then
        MsgBox "!"
    Else
    Application.Wait (Now + TimeValue("0:00:01"))
    End If
Next i
End Sub
\$\endgroup\$
  • \$\begingroup\$ @Ben, by using Application.Wait(Now + TimeValue ("0:00:50"), it invalidates the entire code, as it cannot just wait 50 seconds. It specifically says in the challenge to check every second to determine when to ring. \$\endgroup\$ – Anoplexian - Reinstate Monica May 9 '17 at 14:03
  • \$\begingroup\$ that's not exactly right, you're not checking for the elapsed time, you're calling 50x the function that it is not allowed \$\endgroup\$ – Felipe Nardi Batista May 10 '17 at 11:00
0
\$\begingroup\$

JS (ES6), 64 bytes

i=0,r=Date.now();while(1)Math.abs(Date.now()-r-5e4)>50?0:alert()
\$\endgroup\$
0
\$\begingroup\$

C#, 72 bytes

for(var x=DateTime.Now;DateTime.Now<x.AddSeconds(50););Console.Write(0);
\$\endgroup\$
0
\$\begingroup\$

Windows Batch, 123 121 126 122 112 111 107 100 95 bytes

@set/ae=1%TIME:~6,2%+50-100  
@if %e% geq 60 @set/ae-=60
:w
@if %e% neq %TIME:~6,2% @goto w
@cd

Explanation:

@set /a e=1%Time:~6,2%+50-100               ::Calcuate the result time
@if %e% geq 60 @set/ae-=60                  ::If result time > 59, set it to correct time 

:w                                          :: Marker of the wait loop
@if %e% equ %TIME:~6,2% (cd)else (@goto w)  ::Returns current dir when program ended
                                            :: Or Else go back to wait for loop
\$\endgroup\$
  • \$\begingroup\$ you need to output to stdout \$\endgroup\$ – Felipe Nardi Batista May 10 '17 at 10:39
  • 1
    \$\begingroup\$ You didn't specific this in the rules.... \$\endgroup\$ – stevefestl May 10 '17 at 10:40
  • \$\begingroup\$ read comments in the post \$\endgroup\$ – Felipe Nardi Batista May 10 '17 at 10:41
  • \$\begingroup\$ Well then why wouldn't you tell the Python2 answerer, Bash answerer, Ruby answerer but ME \$\endgroup\$ – stevefestl May 10 '17 at 10:42
  • \$\begingroup\$ python2's is an alternative, the original is stdout, i haven't read all of them yet, i'm reading the new ones as they come. chill \$\endgroup\$ – Felipe Nardi Batista May 10 '17 at 10:44
-1
\$\begingroup\$

This may be foul play.

Bash, 24 Bytes

ping 192.0.0.8 -rqc50|wc

To verify +6 bytes

ping 192.0.0.8 -rqc50|tail -n2

192.0.0.8 is IPv4 dummy address. It send packet for 50 times in one second interval because it would not be reached.

\$\endgroup\$
  • \$\begingroup\$ That's includes in functions that wait for time. And woundn't that print stuff like unreachable dest? \$\endgroup\$ – Felipe Nardi Batista May 9 '17 at 15:33
  • \$\begingroup\$ As expected after all -- this is kind of wait. It dose not print any characters while it is sending 50 packets. wc is WordCount. It is not return until end of stream. In some environments, it shows help because ping -r option is not supported. (ex. s-macke.github.io/jor1k/demos/main.html Try:ping 192.0.0.8 -qc50|wc) \$\endgroup\$ – okud May 9 '17 at 17:09

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