C#, 174 172 147 bytes
Saved 25 bytes by "borrowing" some ideas from raznagul's C# answer and merging them with the sum of first n numbers trick!
Saved 2 bytes by using the sum of first n numbers trick for a loss of precision of 185 milliseconds.
class P{static void Main(){for(int i=1;;){System.Console.WriteLine(i++<731?"Not ready yet":"Eat your hot dog");System.Threading.Thread.Sleep(i);}}}
Ungolfed program:
class P
{
static void Main()
{
for (int i=1;;)
{
System.Console.WriteLine( i++ < 731 ? "Not ready yet" : "Eat your hot dog");
System.Threading.Thread.Sleep(i);
}
}
}
Explanation:
Since the total time to wait is hardcoded at 267 seconds, one can consider this number as a telescopic sum of the first n natural numbers, n * (n + 1) / 2
, which must equal 267000 milliseconds.
This is equivalent to n^2 + n - 534000 = 0
.
By solving this second order equation, n1 = 730.2532073142067
, n2 = -n1
. Of course, only the positive solution is accepted and can be approximated as 730.
The total time can be calculated as 730 * (730 + 1) / 2 = 266815 milliseconds
. The imprecision is 185 milliseconds, imperceptible to humans.
The code will now make the main (and only) thread sleeps for 1 millisecond, 2 milliseconds and so on up to 730, so the total sleep period is ~267 seconds.
Update:
The program's logic can be simplified further - basically it needs to continuously display a message and wait a specified time until switching to the second message.
The message can be change by using a ternary operator to check the passing of the specified time (~267 seconds).
The timing aspect is controlled by using an increasing counter and pausing the execution thread.
However, since the counter variable continues increasing indefinitely without any conditions to check its value, one can expect an integer overflow at some point, when the message reverts to Not ready yet
.
A condition can be added to detect and mitigate the issue by assigning a positive value greater than 730 when the overflow occurs - like i=i<1?731:i
inside the for
loop. Sadly, it comes at the cost of 11 additional bytes:
class P{static void Main(){for(int i=1;;i=i<1?731:i){System.Console.Write(i++<731?"\nNot ready yet":"\nEat your hot dog");System.Threading.Thread.Sleep(i);}}}
The key here is using the counter value in milliseconds to greatly delay the moment of overflow.
The time until overflow can be calculated according to the sum(1..n)
formula, where n = the maximum 32-bit signed integer value in C# (and the .NET framework) or 2^31 - 1 = 2147483647:
2 147 483 647 * 2 147 483 648 / 2 = 2,305843008 x 10^18 milliseconds = 2,305843008 x 10^15 seconds = 26 687 997 779 days = ~73 067 755 years
After 73 million years, it might not matter if a glitch in the system appears - the hot dog, the hungry OP and maybe the human race itself are long gone.
Previous version (172 bytes):
namespace System{class P{static void Main(){for(int i=1;i<731;){Console.Write("\nNot ready yet");Threading.Thread.Sleep(i++);}for(;;)Console.Write("\nEat your hot dog");}}}
Ungolfed program:
namespace System
{
class P
{
static void Main()
{
for (int i = 1; i < 731; )
{
Console.Write("\nNot ready yet");
Threading.Thread.Sleep(i++);
}
for ( ; ; )
Console.Write("\nEat your hot dog");
}
}
}
Previous version (174 bytes):
namespace System{class P{static void Main(){for(int i=0;i++<267e3;){Console.Write("\nNot ready yet");Threading.Thread.Sleep(1);}for(;;)Console.Write("\nEat your hot dog");}}}
Ungolfed program:
namespace System
{
class P
{
static void Main()
{
for (int i=0; i++ < 267e3; )
{
Console.Write("\nNot ready yet");
Threading.Thread.Sleep(1);
}
for ( ; ; )
Console.Write("\nEat your hot dog");
}
}
}
Alternatively, the program may display Not ready yet
only once, wait until the specified time is over and then output Eat your hot dog
by overwriting the previous message while being quite a few bytes shorter:
C#, 145 bytes
namespace System{class P{static void Main(){Console.Write("Not ready yet");Threading.Thread.Sleep(267000);Console.Write("\rEat your hot dog");}}}
Ungolfed program:
namespace System
{
class P
{
static void Main()
{
Console.Write("Not ready yet");
Threading.Thread.Sleep(267000);
Console.Write("\rEat your hot dog");
}
}
}
Not ready yet\nNot ready yet\n...
) or can we just output it once and change the output once the 4m 27s is over? \$\endgroup\$