12
\$\begingroup\$

Most of us LOST fans out there remember the computer that Desmond had to type the characters "4 8 15 16 23 42" in every 108 minutes or the world would end (or would it?).

The challenge here is to create a program that would do the same thing by requiring that every 108 seconds the input 4 8 15 16 23 42 is entered or it will display the message

Sorry, the world has ended with status code -1

It should warn the user at 100 seconds that they need to enter a number with the message

Enter, Quick!

The program must be able to read input at any time and if it is the correct input it will reset the timer. If incorrect input is given nothing happens.

The program should run indefinitely. So the timeline after the last valid input looks like

From 0 to 99 seconds: no output
At 100 seconds: Enter, Quick!
At 108 seconds: Sorry, the world has ended with status code -1.

This is code golf so the shortest answer (in bytes) that accomplishes this task wins! Good Luck!

\$\endgroup\$
  • \$\begingroup\$ I've edited the question a bit. Feel free to rollback any changes you don't want. \$\endgroup\$ – Martin Ender Sep 17 '15 at 22:33
  • 2
    \$\begingroup\$ There should be bonus points for outputting the correct hieroglyphics. \$\endgroup\$ – curiousdannii Sep 18 '15 at 1:44
  • \$\begingroup\$ After re-reading the challenge, I'm not sure anymore I've interpreted it correctly. What should happen after the "the world has ended"? \$\endgroup\$ – Dennis Sep 18 '15 at 3:22
  • 1
    \$\begingroup\$ As a lost-nerd, I feel obligated to point out that Desmond would be warned about entering the numbers 4 minutes (240 seconds) before the timer runs out. \$\endgroup\$ – DJMcMayhem Sep 18 '15 at 4:15
10
\$\begingroup\$

bash, 160 bytes

I()($s 100&&echo Enter, Quick!&$s 108&&echo Sorry, the world has ended with status code -1&)
i()(read r;[[ $r = '4 8 15 16 23 42' ]]&&pkill $s&&I;i)
s=sleep;I;i

I'm currently uncertain what the expected behavior is after "the world has ended".

Run like this:

bash lost.sh 2>&-

2>&- is required to ignore STDERR, which is allowed by default.

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

Modern-browser JavaScript, 252 247 242 bytes

n=t=>Date.now()+(t?0:1e5)
d=n(i=f=0)
onkeyup=e=>{if("4 8 15 16 23 42".charCodeAt(i%15)==e.keyCode&&++i%15<1)d=n(f=0)}
setInterval('if(n(1)>d&&f<2)d=n(1)+8e3,console.log(f++?"Sorry, the world has ended with status code -1":"Enter, Quick!")',9)

Instructions: run this in the console of a blank tab, click on its document to gain focus and start repeatedly typing the string. As long as you're doing well, you'll get no feedback whatsoever. Refresh and change 1e5 to 1e4 to make things more interesting.

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

Groovy, 244 or 228 bytes

I wrongly remembered Java having a nextLine method that took an argument of how long to wait, so I thought this would be easy. I couldn't find a method that did that, so I implemented this with two threads. It's a bit bulky. Oh well.

t=Thread.start{while(1)try{Thread.sleep(1e5);println "Enter, Quick!";Thread.sleep(8e3);println "Sorry, the world has ended with status code -1";System.exit(-1)}catch(e){}};while(1)if(System.console().readLine()=="4 8 15 16 23 42")t.interrupt()

This assumes the proper behavior for the world ending is for the process to exit with a status code of -1. If the intended behavior is to keep looping and expect an external force to end the world (and by extension, the program), the ;System.exit(-1) portion can be omitted to save 16 bytes. Yay.

I originally wrote this to use the hashCode of the string, but that wound up longer than an exact comparison embedding the string because hashCode is long.

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

APL (Dyalog Unicode), 144 bytesSBCS

As it turns out, both sides are running APL…

:For t:In 100 8
:For s:In⍳t
→{1E3::⍬⋄⍳⍞≡⍕4 8 15 16 23 42}⎕RTL←1
:End
⊃'Enter, Quick!' 'Sorry, the world has ended with status code -1'⌽⍨t=8
:End

Try it online!

:For t:in 100 8 loop twice, once with t(timput) being 100 and then with t as 8:

:For s:In⍳t for s(econds) 1 through and all the ɩndices until t

  ⎕RTL←1 set the Response Time Limit to 1 (second)

  {} apply the following anonymous lambda to that (although this argument is unused)

   1E3:: in the following, if any exception happens:

     return []

    try:

    ⍕4 8 15 16 23 42 stringify the required numbers

    ⍞≡ prompt for input and compare to that (gives 0 or 1)

     the first that many ɩndices ([] or [1]`

   go to that line (1 if [1], continue on next line if [])

:End end of inner loop; proceed with next second of the current timeout

t=3 is this the second timeout (0 or 1)?

 …⌽⍨ rotate the following that many steps:

  'Enter, Quick!' 'Sorry, the world has ended with status code -1' implicitly print appropriate text

 disclose (to print without leading and trailing space)

:End end of outer loop: after warning, loop; after printing "Sorry…", proceed to terminate program

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

C++ (gcc), 395 bytes

Compiling on Linux requires the -pthread switch. MinGW does without.

#import<iostream>
#import<thread>
using namespace std;auto N=chrono::steady_clock::now;auto L=N();int w;int main(){thread A([]{for(;;){auto t=chrono::duration_cast<chrono::seconds>(N()-L).count();t>99&&!w?puts("Enter, Quick!"),w=1:t>107?exit(puts("Sorry, the world has ended with status code -1")),0:0;}}),B([]{for(string s;;s=="4 8 15 16 23 42"?L=N(),w=0:0)getline(cin,s);});A.join();B.join();}

Try it online!

\$\endgroup\$

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.