Shortest Konami Code

15

2

The Problem

You must write a program that, when the Konami Code is typed in during runtime, prints the string "+30 lives" and sounds a noise of your choice from the computer's speaker.

Definition

The "Konami Code" is defined as UUDDLRLRBA followed by pressing the enter key.

The Rules

  • You may choose to use the up arrow for U, down for D, left for L, and right for R, as long as your code is consistent with either arrows or letters.
  • Your input may be accepted from a controller or a keyboard, but does not need to support both.
  • Existing answers may continue to use BABA instead of BA, but may also shorten it if they wish to do so. Future answers should all use BA for consistency.

  • Empty input doesn't need to be supported.

Christopher

Posted 2016-12-08T22:53:03.527

Reputation: 3 428

Comments are not for extended discussion; this conversation has been moved to chat.

– Martin Ender – 2018-02-20T08:27:08.990

Answers

13

05AB1E, 33 bytes (Fine on Empty Input)

Dg0Qiq}•ï“뙵yê!•36BQi7ç“+30™‹“J,

Try it online!

05AB1E, 30 26 bytes (Fails on Empty Input)

-4 thanks to Adnan

•ï“뙵yê!•36BQi7ç,“+30™‹“,

Try it online!

•ï“뙵yê!•36B                  # UUDDLRLRBABA in base-214, converted back to base-36.
             Qi                # If implicit input is equal to...
               7ç,             # Print 7 converted to a character to the console (bell sound)...
                  “+30™‹“, # Print +30 lives.

Disclaimer: Bell sound does not play on TryItOnline, maybe ask Dennis if one of his servers somewhere is pinging when you run it.

Magic Octopus Urn

Posted 2016-12-08T22:53:03.527

Reputation: 19 422

You almost won! Just one byte off – Christopher – 2016-12-13T02:32:15.107

2A compressed version of "+30 lives" is “+30™‹“ :) – Adnan – 2016-12-13T08:40:48.343

This always outputs "+30 lives" for me on the online interpreter, even with empty input. – redstarcoder – 2016-12-13T15:12:23.983

2@redstarcoder Only* with empty input, crap, fixing – Magic Octopus Urn – 2016-12-13T15:13:59.073

@carusocomputing can you fix this the BA should only be once (my fault sorry) – Christopher – 2016-12-15T22:35:46.983

7

Python 3,  55  53 bytes

-1 byte thanks to isaacg (use bitwise not to replace -10 with ~9).
-1 byte thanks to CalculatorFeline (it is possible to use a literal BEL byte).

while'UUDDLRLRBA'!=input()[~9:]:1
print('+30 lives7')

where the 7 shown above is a literal byte 0x07 (an unprintable in a code block).

Once enter is pressed the while loop condition is checked. If the last ~9 = 10 characters (at most) do not match the Contra command code then the no-op 1 is executed (replacement of pass for brevity); if they do match then the while loop ends and the print statement is executed, which writes the required text along with an ASCII bell character ("alarm"), 0x07, which produces a sound unless it has been explicitly disabled (or the terminal has no speaker!).

Jonathan Allan

Posted 2016-12-08T22:53:03.527

Reputation: 67 804

can you fix this the BA should only be once (my fault sorry) – Christopher – 2016-12-15T22:36:32.357

Fixed, thanks for the heads up. – Jonathan Allan – 2016-12-16T01:14:37.147

1-10 = ~9, which can save you a byte. – isaacg – 2017-06-19T09:49:47.733

Can't you put a literal \x07 in the string for -1 byte? – CalculatorFeline – 2017-06-19T21:38:33.557

@CalculatorFeline I have no idea, can I? Note people often use \n in strings in Python golfs, why would they not use \x0a? – Jonathan Allan – 2017-06-20T08:11:59.133

*\a and it's because you can't have newlines in strings without triplequotes. That only applies to newlines, BELs are fine. – CalculatorFeline – 2017-06-20T14:59:40.490

@CalculatorFeline Ah yes true about \n. To what does "* \a and" refer? When you said "Can't you..." did you mean to say "You can..."? If so, how is a bell written inside a string in one byte of source? – Jonathan Allan – 2017-06-20T15:16:29.797

...i.e. is this valid?

– Jonathan Allan – 2017-06-20T15:19:20.563

Yes (when running from file). – CalculatorFeline – 2017-06-20T15:21:29.277

7

JavaScript (ES6), 202 200 198 bytes

Tested only on Chrome and Firefox. Uses UDLR rather than the arrow keys. Please make sure that CapsLock is off.

(o=(A=new AudioContext()).createOscillator()).connect(A.destination);s='';document.onkeyup=e=>{(s+=e.key[0]).slice(-11)=='uuddlrlrbaE'&&setTimeout('o.stop()',500,console.log('+30 lives'),o.start())}
Please click inside this area to make sure it gets focus.

Arnauld

Posted 2016-12-08T22:53:03.527

Reputation: 111 334

can you fix this the BA should only be once (my fault sorry) – Christopher – 2016-12-15T22:36:13.650

5

Processing, 161 157 155 bytes

String k="";void keyTyped(){if(match(k+=key,"uuddlrlrba\\n$")!=null){print("+30 lives");new processing.sound.SoundFile(this,"a.mp3").play();}}void draw(){}

The audio file must be saved as sketchName/data/a.mp3. Note: I have only tested this program without the audio file because I am too lazy to download an mp3 file (since only limited extensions are supported from processing.sound.SoundFile).

The draw() function is needed to be there in order for keyTyped to work.

The reason we are using keyTyped is because Processing does not have STDIN, it can only listen for keys being pressed via the sketch being run.

Explanation

String k="";
void keyTyped(){
  if(match(k+=key,"uuddlrlrba\\n$")!=null){
    print("+30 lives");
    new processing.sound.SoundFile(this,"a.mp3").play();
  }
}
void draw(){
}

All of the user's keystrokes are stored as chars inside the String k. The keyTyped is an inbuilt function that is called whenever the user types a key. Simultaneously, we are checking if this String ends with the keystrokes. Then we print +30 lives and play the sound file. And the draw function is there to continuously update keyTyped. After the Konami code is entered, then nothing else will be outputted and no audio will be played.

user41805

Posted 2016-12-08T22:53:03.527

Reputation: 16 320

it seems like you should count the size of your mp3 file – None – 2016-12-13T19:06:47.197

@ricyje Processing doesn't really have any other way of outputting sounds. Also, the size of the .mp3 file doesn't really matter since no matter what it is, Processing will play it as a sound – user41805 – 2016-12-13T19:09:07.297

@KritixiLithos can you fix this the BA should only be once (my fault sorry) – Christopher – 2016-12-15T22:35:58.090

@ChristopherPeart Done! (I'm glad to save 2 more bytes :) – user41805 – 2016-12-16T07:43:30.297

5

Jelly, 33 30 29 27 bytes

ɠ⁻“UUDDLRLRBA”$¿ṛ7Ọ“¡}ʠƈ[ỵ»

Turns out, the append () is not needed, strings are automatically joined together both printed.

ɠ⁻“UUDDLRLRBABA”$¿ṛ7Ọṭ“¡}ʠƈ[ỵ»

Uncompressed

ɠ⁻“UUDDLRLRBABA”$¿ṛ7Ọṭ“+30 lives”

Try it online
If you remove everything after ¿, you can mess with the input and see that it only returns when the input string is correct.

ɠ⁻“UUDDLRLRBABA”$¿ṛ7Ọṭ“+30 lives” - main link
ɠ                                 - takes a line of input
 ⁻“UUDDLRLRBABA”$                 - $ groups it into a monadic !="UUDDLRLRLRBABA"
                 ¿                - while (the inequality, which takes from the getline)
                  ṛ               - take the right argument (to ignore the input string)
                   7Ọ             - chr(7), the bell character
                     ṭ“+30 lives” - append that bell character to the output

Thanks @JonathanAllan for helping me figure out string compression :)

JayDepp

Posted 2016-12-08T22:53:03.527

Reputation: 273

“+30 lives” compressed is “¡}ʠƈ[ỵ» (for future reference here is a post about it, it's linked in the tutorial on the Jelly wiki). – Jonathan Allan – 2016-12-10T07:04:22.183

where is the sound? – tuskiomi – 2016-12-13T18:31:52.760

1

I just tested it with the official interpreter and it does indeed loop until the Konami code is entered

– JayDepp – 2016-12-15T00:46:35.937

@JayDepp can you fix this the BA should only be once (my fault sorry) – Christopher – 2016-12-15T22:36:42.883

I believe that it's not so much automatic string concatenation as an implicit print of the LLC (leading constant chain) 7Ọ followed by an implicit print of the nilad “¡}ʠƈ[ỵ», appearing on stdout as if it were concatenation. As a showcase you can try adding a 2 second sleep between with ɠ⁻“UUDDLRLRBABA”$¿ṛ7Ọ2“+30 lives”œS. – Jonathan Allan – 2016-12-16T01:19:02.740

Also I think the code should really be something like ɠUḣ⁵µ⁻“ABRLRLDDUU”$¿ṛ7Ọ“+30 lives” so that an entry like blahblahUUDDLRLRBA followed by <enter> will also trigger the bell and message. – Jonathan Allan – 2016-12-16T01:39:06.503

@JonathanAllan that's interesting about the printing. At some point I thought about matching the end of the string, but I looked at the other answers and from what I can tell, over half don't do it. I guess that's a result of the vague input specifications. – JayDepp – 2016-12-16T03:18:56.907

4

><>, 132 69 64 62 bytes *><>, 84 bytes

i:1+?\0[
:1+?!\i
BA"a~/"UUDDLRLR
{v!?l/!?=
7/"+30 lives"
o<;!?l

Try it here!

This relys on the bell chime (ascii 7) for the noise at the end (not heard on the online interpreter).

Thanks to @TealPelican for saving another 15 bytes!

Saved two bytes checking for BA instead of BABA.

redstarcoder

Posted 2016-12-08T22:53:03.527

Reputation: 1 771

Does this make a sound? – user41805 – 2016-12-09T17:38:21.993

@KritixiLithos, yes you'll see on the second-last line at the very end is a "7", that's the bell chime. – redstarcoder – 2016-12-09T17:39:15.487

I've refactored your code a little bit because i didn't quite understand the first lines use? Try it here This link is for ><> but works for *><> too (doesn't use the dive mechanic like you have) - it also saved on 19 bytes! :)

– Teal pelican – 2016-12-13T10:45:07.227

@Tealpelican, the first line is so it keeps running, waiting for input! Your linked version doesn't do that ;p. – redstarcoder – 2016-12-13T15:01:59.810

You are right! I missed that! - I've added a line in to accommodate for my error and replaced some redundant reversing and printing Try the new version which I've gotten down to 67 bytes :D

– Teal pelican – 2016-12-13T15:27:57.857

@Tealpelican, almost there! It still exits when it gets the incorrect code, it should loop :p. That's what I had the dive/rise before! It still looks like a serious improvement though. – redstarcoder – 2016-12-13T15:57:35.233

I'm sure a lot of the other answers haven't added these restrictions :/ but yes you are correct. This is a super easy fix (lucky!) V3.0 69 bytes - main change added to line 1 ~ becomes >0[ and on line 4 the ; becomes v to redirect back to the new top code.

– Teal pelican – 2016-12-13T16:25:24.383

@Tealpelican, do they not? Well I'd rather try to keep in the spirit anyways :). It works! I'll make that the answer. Thanks again! – redstarcoder – 2016-12-13T18:01:19.367

3

C 119 -1 - 2 = 116 bytes

Golfed

i;f(){char a[14];a[13]=0;while(strcmp(a,"UUDDLRLRBA"))for(i=0;i<12;a[i]=a[i+++1]);a[i]=getch();puts("+30 Lives\a");}  

Ungolfed

#include<stdlib.h>
#include<conio.h>

i;
f()
{
    char a[14];
    a[13]=0;
    while(strcmp(a,"UUDDLRLRBA"))
    {
        for(i=0;i<12;a[i]=a[i+++1]);
        a[i]=getch();
        puts(a); //This is to print every step in the runtime
    }
    puts("+30 Lives\a"); // '\a' is the acsii char that makes sound when passed to STDOUT
}

main()
{
    f();
}

Mukul Kumar

Posted 2016-12-08T22:53:03.527

Reputation: 2 585

s/UUDDLRLR BABA/UUDDLRLRBABA for -1 byte and the strcmp will be looking for the correct string! – redstarcoder – 2016-12-09T18:43:34.680

@Christopher please never make edits to people's code on this Stack Exchange site. your edit should never have been approved. – cat – 2016-12-10T01:22:48.507

Mukul, you can remove the space yourself. – cat – 2016-12-10T01:23:56.213

@cat that okay? – Mukul Kumar – 2016-12-10T02:09:32.727

1@MukulKumar Of course :) it's important that the answerer edit their own code. – cat – 2016-12-10T02:14:15.880

1@MukulKumar can you fix this the BA should only be once (my fault sorry) – Christopher – 2016-12-15T22:37:52.643

@Christopher fixed – Mukul Kumar – 2016-12-16T03:31:40.670

3

GNU sed, 32 25 + 1 = 33 26 bytes

+1 byte for -n flag.

/UUDDLRLRBA$/a+30 Lives\a

Try it online!

Explanation

Every time enter is pressed ($), the program checks if the preceding 10 keystrokes were the code. If they were, the a command queues the text +30 Lives and the bell sound (\a) to be printed at the end of the current cycle (which, in this case, is immediately).

Jordan

Posted 2016-12-08T22:53:03.527

Reputation: 5 001

can you fix this the BA should only be once (my fault sorry) – Christopher – 2016-12-15T22:36:48.307

This already has the BA just once. – Jordan – 2016-12-15T23:29:20.890

Oh wow didn't notice – Christopher – 2016-12-16T00:23:30.270

3

Mathematica, 50 bytes

While[x=!=UUDDLRLRBA,x=Input[]];Beep[];"+30 lives"

Initially, x is just a symbol and is thus not identical (=!=) to the symbol UUDDLRLRBA, so x=Input[] is evaluated. This will open a dialog box with the cursor already in the input field, so the user can immediately start typing on the keyboard.

If Enter is pressed or OK is clicked without typing anything, then InputField[] will return Null, which is not identical to UUDDLRLRBA, so the loop continues and another dialog box will be opened.

If the user clicks Cancel or otherwise exits the dialog box, then InputField will return $Canceled, which is also not identical to UUDDLRLRBA, so the loop will continue.

The user can type in the dialog box to their heart's desire. When they hit Enter, their input is interpreted as a Wolfram language expression (possibly just boxes). If that expression is anything other than the symbol UUDDLRLRBA, the loop will continue.

"Must loop until the Konami code is entered" is a little vague, but I believe this satisfies it. Once the While loop is completed, Beep[];"+30 lives".

ngenisis

Posted 2016-12-08T22:53:03.527

Reputation: 4 600

can you fix this the BA should only be once (my fault sorry) – Christopher – 2016-12-15T22:37:43.423

1UUDDLRLRB? Where's A? – CalculatorFeline – 2017-06-20T15:01:41.780

@CalculatorFeline Fixed – ngenisis – 2017-06-20T16:20:33.353

3

Stacked, 41 bytes

['+30 lives'BEL+out]prompt'UUDDLRLRBA'=if

Reads input from keyboard.

['+30 lives'BEL+out]prompt'UUDDLRLRBA'=if
[                  ]prompt'UUDDLRLRBA'=if  if the input is the desired string,
 '+30 lives'BEL+out                           output '+30 lives' and the BEL character. 

Conor O'Brien

Posted 2016-12-08T22:53:03.527

Reputation: 36 228

3

Petit Computer BASIC, 91 bytes

@L
WAIT 1S$=S$+CHR$(B)*!!B
B=BTRIG()IF B<1024GOTO@L
IF"xxxxxxxxxx"==S$THEN BEEP?"+30 lives

Finally a solution that actually uses controller input!

I used Petit Computer rather than the newer version (SmileBASIC) because it has access to the Start button, and BTRIG() is shorter than BUTTON().

I've replaced the data string with x's, it should contain characters with ascii codes 1,1,2,2,4,8,4,8,32,16

Boring version, 46 bytes

INPUT S$IF"UUDDLRLRBA"==S$THEN BEEP?"+30 lives

12Me21

Posted 2016-12-08T22:53:03.527

Reputation: 6 110

I would recommend using SmileBASIC 2 for the name of Petit Computer entries, and where necessary specify SmileBASIC as SmileBASIC 3. – snail_ – 2017-02-08T04:33:16.233

Nice job using controller! – Christopher – 2017-02-08T11:00:13.423

3

AutoHotkey, 63 bytes

Input,K,L10
if(K="UUDDLRLRBA"){
MsgBox,"+30 lives" 
SoundBeep
}

After running this script, it will check if the next 10 keys are the Konami code and if it is, it will show a message box saying "+30 lives" and it should play a beep (I don't have speakers now to test).

Kodos Johnson

Posted 2016-12-08T22:53:03.527

Reputation: 776

Cool! AutoHotkey is a interesting program. – Christopher – 2017-03-04T12:55:18.303

2

Powershell, 89 86 Bytes

if((Read-Host)-eq"UUDDLRLRBABA"){"+30 Lives";[System.Media.SystemSounds]::Beep.Play()}

enter submits the string to the Read-Host, so it should.. work?

colsw

Posted 2016-12-08T22:53:03.527

Reputation: 3 195

2I think you just need to check for "UUDDLRLRBABA" not "UUDDLRLR BABA" as there's no " " in the Contra code. That'd also save you a byte. I think assuming enter because they have to press enter to send the input is valid. – redstarcoder – 2016-12-09T18:02:23.980

I does work by the way – Christopher – 2016-12-09T22:28:05.397

1@ChristopherPeart Please don't make edits to others' code even if you are the OP, or if you are saving them a byte. – cat – 2016-12-10T01:26:43.907

Ok I will do that. But It was my fault – Christopher – 2016-12-10T03:08:21.063

@ConnorLSW can you fix this the BA should only be once (my fault sorry) – Christopher – 2016-12-15T22:37:35.690

2

Wonder, 50 bytes

ol"•";f\@[="UUDDLRLRBA"rl0?ol"+30 lives•"?f0];f0

Replace with the character of code \x07 (the BEL control char). Takes input through STDIN.

Mama Fun Roll

Posted 2016-12-08T22:53:03.527

Reputation: 7 234

can you fix this the BA should only be once (my fault sorry) – Christopher – 2016-12-15T22:37:04.803

Well I fixed. Thanks for the notice! – Mama Fun Roll – 2016-12-16T01:58:15.853

2

C# (the boring version), 80 bytes

void n(){while(Console.ReadLine()!="UUDDLRLRBA"){}Console.Write("+30 Lives\a");}

C# (The interesting one), 202 bytes

void n(){t();Console.Write("+30 Lives\a");}void t(){var k="UUDDLRLRBA";var s="";while(s!=k){s+=Console.ReadKey().Key.ToString();s=k.StartsWith(s)?s:"";}if(Console.ReadKey().Key!=ConsoleKey.Enter){t();}}

I think this works, sadly online testers do not support the existence of a console input, as such I will have to go on the tests I have done

Can most likely be golfed hugely - I'm not great at this! :)

Ungolfed:

void n()
{
    t();
    Console.WriteLine("+30 Lives\a");
}

void t()
{
    var k = "UUDDLRLRBA";
    var s = "";
    while(s != k)
    {
        s += Console.ReadKey().Key.ToString();
        s = k.StartsWith(s) ? s : "";
    }
    if(Console.ReadKey().Key != ConsoleKey.Enter)
    {
        t();
    }
}

Alfie Goodacre

Posted 2016-12-08T22:53:03.527

Reputation: 321

Hmm, does C# have a way to read until a newline is detected? That'd save you from looking for the enter key. Look at how it's implemented in Python here.

– redstarcoder – 2016-12-13T15:14:43.033

There's Console.Readline but I thought that might be aganist the spirit of the challenge! If that's fine I can make this much shorter – Alfie Goodacre – 2016-12-13T15:15:11.483

@redstarcoder added a Console.ReadLine() version – Alfie Goodacre – 2016-12-13T15:20:25.967

@AlfieGoodacre can you fix this the BA should only be once (my fault sorry) – Christopher – 2016-12-15T22:38:05.730

@ChristopherPeart It originally was but I had to change it ;) – Alfie Goodacre – 2016-12-15T23:45:38.647

1

flex, 37 + 5 = 42 bytes

%%
UUDDLRLRBA puts("+30 lives\a");

The code itself is 37 bytes, compile it with the "-main" option which adds 5 bytes. Naturally, you have to compile the resulting C file with your favorite C compiler, but I don't think that step should count toward the byte count.

I could save a byte by using a literal BEL character instead of \a, but I'd rather be able to read my own code.

BenGoldberg

Posted 2016-12-08T22:53:03.527

Reputation: 389

4You really don't need to be able to read the code. This is code golf – Christopher – 2016-12-16T14:02:51.693

Personally, I think I should get a bonus for using flex, due to how rarely it gets used here. :) – BenGoldberg – 2016-12-17T18:15:01.507

1I believe you can reduce the penalty to 3 bytes via giving -ll as an option to the C compiler rather than -main as an option to flex. – None – 2016-12-17T18:15:20.117

1

C, 87 85 81 bytes

s[13];f(){for(;strcmp(s,"UUDDLRLRBA\n");fgets(s,12,stdin));puts("+30 Lives!\a");}

MD XF

Posted 2016-12-08T22:53:03.527

Reputation: 11 605

1

shortC, 52 bytes

s[13];AO;Ms,"UUDDLRLRBA\n");Ys,12,N));J"+30 Lives!\a

Explanation:

s[13];                                                // declare array with 13 members
      A                                               // main function
       O;Ms,"UUDDLRLRBA\n");                          // for loop while input is not equal to the Konami Code
                            Ys,12,N));                // read 12 characters of input into array
                                      J"+30 Lives!\a  // print the string

Try it online!

MD XF

Posted 2016-12-08T22:53:03.527

Reputation: 11 605

1

C, 114 bytes

#define X "UUDDLRLRBA"
main(i,j){for(i=j=0;!j;)(getchar()==X[i++])&&((j=1?!X[i]|puts("+30 lives\a"):0)|1)||(i=0);}

user12707

Posted 2016-12-08T22:53:03.527

Reputation: 41