Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is an challenge in which each answer builds on the previous answer. I recommend sorting the thread by "oldest" in order to be sure about the order in which the posts are made.

The task

The nth program to be submitted must run in n different languages; specifically, all the languages added in previous programs to be submitted, plus one more. The program must output 1 when run in the first language used in answers to this question, 2 when run in the second language, and so on. For example, the first answer could print 1 when run in Python 3, and the second answer could output 1 when run in Python 3 and 2 when run in JavaScript; in this case, the third answer would have to output 1 when run in Python 3, 2 when run in JavaScript, and 3 when run in some other language.

Additional rules

  • Your program must run without erroring out or crashing. Warnings (and other stderr output) are acceptable, but the program must exit normally (e.g. by running off the end of the program, or via a command such as exit that performs normal program termination).
  • Each answer must be no more than 20% or 20 bytes (whichever is larger) longer than the previous answer. (This is to prevent the use of languages like Lenguage spamming up the thread, and to encourage at least a minor amount of golfing.)
  • Using different versions of the same language is allowed (although obviously they'll have to print different numbers, so you'll need to fit a version check into the polyglot). However, you may not use a language feature that returns the language's version number. Repeating the exact same language is, obviously, impossible (as the program would have to deterministically print one of two different numbers).
  • Tricks like excessive comment abuse, despite being banned in some polyglot competitions, are just fine here.
  • You don't have to use the previous answers as a guide to writing your own (you can rewrite the whole program if you like, as long as it complies with the spec); however, basing your answer mostly on a previous answer is allowed and probably the easiest way to make a solution.
  • You cannot submit two answers in a row. Let someone else post in between.
  • As this challenge requires other competitors to post in the same languages you are, you can only use languages with a free implementation (much as though this were a contest).
  • Note that the victory condition (see below) is designed so that breaking the chain (i.e. making it impossible for anyone else to answer after you via the use of a language that is hard to polyglot with further languages) will disqualify you from winning. The aim is to keep going as long as we can, and if you want to win, you'll have to respect that.

Answer format

As all the answers depend on each other, having a consistent answer format is going to be helpful. I recommend formatting your answer something like this (this is an example for the second link in the chain):

2. JavaScript, 40 bytes

(program goes here)

This program prints 1 in Python 3, and 2 in JavaScript.

(if you want to explain the program, the polyglotting techniques, etc., place them here)

Victory condition

Once there have been no new answers for 14 days, the winner will be whoever posted the second newest answer, i.e. the largest polyglot that's been proven not to have broken the chain. Extending the chain after that is still very welcome, though!

share|improve this question
1  
For people who can see deleted posts: the Sandbox post was here. – ais523 8 hours ago
    
1. Maybe you should ban functions getting the version number. 2. Does "freely available online implementation" mean someone must be able to try it online in a browser? – jimmy23013 7 hours ago
3  
Can you remove, replace, or rearrange chars from the previous program (as long as it still works in all languages)? – ETHproductions 7 hours ago
3  
There's no need to copy the previous program, although of course you can use it as a guide; redoing the program from scratch is likely to take longer! There's no need to permalink to answers; sorting by oldest will show all the answers in order already. – ais523 7 hours ago
1  
@ais523 I think what was meant was that should new answers contain try it links with the new code? – BlueEyedBeast 7 hours ago

10 Answers 10

1. Python 3 (8 bytes)

print(1)

This program prints 1 in Python 3.

Starting this off with Python 3 because I know it's good for polyglots and can be taken in a number of different directions (also, I wanted to ensure that the first answer was in a relatively normal language, rather than an absurd esolang that's hard to polyglot with).

share|improve this answer
    
Would ><> be a good choice for the 2nd language (so that we start 2d space)? – Kritixi Lithos 7 hours ago
    
So that means the next answer can't be more than 9 bytes? That's gonna be really hard to come up with another one. – DJMcMayhem 7 hours ago
    
@DJMcMayhem Each answer must be no more than 20% or 20 bytes (whichever is larger) longer – Kritixi Lithos 7 hours ago

4. ><> (29 bytes)

#>>>>>>>>v;n4
print(1)#>3N.i2

This program prints 1 in Python 3, 2 in Vim, 3 in Minkolang v0.15 and 4 in ><>

Try it Online!

Code ran

#             - change direction to left
            4 - add 4 to stack
           n  - print as a number
          ;   - end the program

Yet another 2D language.

Has no effect on Minkolang as it adds characters after the direction changes, gets ignored by Vim for some reason. # is a comment in Python so no change their either.

share|improve this answer

6. SMBF, 45 bytes

#v<++++<;n4
#>3N.
print('1'if 1/2else'5')
#i2

Try it online

This program prints 1 in Python 3, 2 in Vim, 3 in Minkolang v0.15, 4 in ><>, and 5 in Python 2.

SMBF (aka Self-modifying Brainfuck) uses <++++<>.. The pointer is moved left (to the last character of the source code), and the cell is incremented four times then printed.

share|improve this answer
1  
I think you need another + in the code and TIO link. – Riley 7 hours ago
    
@Riley It's been fixed. – mbomb007 6 hours ago

8. Retina, 70 bytes

#v;7||"<+0+0+0+<;n4
#>3N.
#|\w*
#8
#|

#M`
print('1'if 1/2else'5')
#i2

Try it online

This program prints 1 in Python 3, 2 in V, 3 in Minkolang v0.15, 4 in ><>, 5 in Python 2, 6 in SMBF, and 7 in Japt.

Screw creating hyperlinks to interpreters. That's going to take way too much time. It's up to the poster and anyone creating new answers to test it yourself.

share|improve this answer
    
This is 2 bytes too long (50 + 20 is 70, 50 + 20% is 60). – ais523 6 hours ago
    
@ais523 Fixed it. – mbomb007 6 hours ago

9. Perl, 84 bytes

#v;7||"<+0+0+0+<;n4
#>3N.
#|\w*
#8
#|

#M`
print(None and 9or 1/2and 1or 5)
#jd5ki2

There's a literal ESC character between the j and d on the last line, but my browser doesn't want to put it into the input box. (Perhaps someone with a different browser could edit it in?)

This prints 1 in Python 3, 2 in Vim (tested locally, but here's a link for the very similar language V), 3 in Minkolang, 4 in <><, 5 in Python 2, 6 in SMBF, 7 in Japt, 8 in Retina, and 9 in Perl.

Let's get some more exoteric languages in, via abusing arithmetic that works differently in different languages. (None is falsey in Python but truthy in Perl, and and/or chains work the same way in both languages.)

Apart from Python, I also had to change the vim code. Instead of making it into a series of no-ops, I just let it insert junk, then deleted the junk again at the end.

share|improve this answer
2  
Seriously? You're going to make it extremely difficult for anyone else to create answers if you put in a literal ESC. We have to be able to test the code. – mbomb007 4 hours ago
    
You don't have to include the literal ESC in your own answers; I just found it to be the easiest way to write this one. (Additionally, it works fine in Firefox, and in local testing; the only thing blocking me from putting it in the post is that Chromium, which I use for SE, doesn't want to put it into the input box.) – ais523 4 hours ago
    
Also, you can't test locally for Vim. Languages are defined by the interpreter that is used. So it's really V that we've had all this time. – mbomb007 4 hours ago
    
Err, isn't vim an interpreter for Vim? (It works in both vim and V, though.) – ais523 4 hours ago

2. Vim (11 bytes)

print(1)#i2

This program prints 1 in Python 3, and 2 in Vim.

Just to get the ball rolling and to throw my favorite language into the mix early on. :)

It's a very straightforward answer.

print(1)#

just so happens to be a NOP in vim. (lucky for me) Then i2 enters insert mode and inserts a '2'. You can try vim online here

Of course, in python

print(1)

prints '1', and

#2

is a comment.

share|improve this answer
1  
Is this V or Vim? The interpreter you linked to is technically "V". – mbomb007 7 hours ago
    
@mbomb007 Well, V is almost entirely backwards compatible, so the intention was vim. I suppose it technically is V though. Is it too late to change? – DJMcMayhem 7 hours ago
    
Not really, just edit the title in the answers. – mbomb007 6 hours ago
    
Does V have a way to exit Insert Mode? – mbomb007 6 hours ago
    
@mbomb007 A literal ESC character will do it (which is why I had to use one in my submission). – ais523 2 hours ago

3. Minkolang v0.15 (26 bytes)

#>>>>>>>>v
print(1)#>3N.i2

This program prints 1 in Python 3, 2 in Vim, and 3 in Minkolang v0.15

I hope I don't mess things up by introducing a 2d language

Try it online!

Explanation

#                     stops program from moving through time (really does nothing)
 >>>>>>>>             I can't use a space because then the program will move through time
         v            go down
         >            go right
          3N.         Outputs 3 and end program
                      Anything afterward is ignored since program has ended

Vim somehow ignores Minkolang, so that's good

And there really wasn't a problem with Python since it ignores the comments #

Next...

For the next language, I suggest something like ><> since # acts as a reflector (so that the direction will change to left and it will wrap to all the way in the right) so you can add code that can be ignored by other languages

share|improve this answer
    
“move through time” wat? – TùxCräftîñg 7 hours ago
    
@TùxCräftîñg Minkolang has 3 dimensions (2d = normal, the 3rd one is time). TBH, I don't understand it, it just states that in the explanation on the TIO link – Kritixi Lithos 7 hours ago
    
And no. I suggest that someone fix Vim's horrible insert mode with the next answer. – mbomb007 7 hours ago
    
@mbomb007 What exactly are you referring to? – Kritixi Lithos 7 hours ago
    
@mbomb007 4th submission (><>) already posted :) – Kritixi Lithos 7 hours ago

5. Python 2 (35 bytes)

#3N.;n4
print('1'if 1/2else'5')
#i2

This program prints 1 in Python 3, 2 in Vim, 3 in Minkolang v0.15, 4 in ><> and 5 in Python 2.

Try It Online beta!

In Python 2, 1/2 is 0, which is a falsy value, which makes Python print 5. In Python 3, 1/2 is 0.5, which is a truthy value, which makes Python print 1.

share|improve this answer
    
I can confirm it works in Minkolang – Kritixi Lithos 7 hours ago

7. Japt (50 bytes)

#v;7||"<++++<;n4
#>3N.
print('1'if 1/2else'5')
#i2

Man, it was fun to fit Japt in there. Hopefully it didn't kill anyone's plans...

Test it online!

This program prints 1 in Python 3, 2 in V, 3 in Minkolang v0.15, 4 in ><>, 5 in Python 2, and 6 in SMBF.

How this works: #v; takes the char code of v, or 118. Then 7|| returns the logical OR of 7 and the rest of the code, which is wrapped in a string with " to avoid any syntax errors. The result, 7, is automatically sent to STDOUT.

For future polyglotters, " can be changed to ` at no penalty to the Japt program (though I'm not sure about the others).

share|improve this answer

10. Befunge, 95 bytes

#v02^0;7||"<+0+0+0+<;n4
#v0#@00
#>3N.
#|\w*
#8
#|
#M`
print(None and 9or 1/2and 1or 5)
#jd5ki2

There is a literal ESC character between j and d on the last line (grr, @ais523). It is not included in this code. To get the actual code, please go to the Try it online link.

This prints 1 in Python 3, 2 in Vim, 3 in Minkolang, 4 in <><, 5 in Python 2, 6 in SMBF, 7 in Japt, 8 in Retina, 9 in Perl, and 10 in Befunge.

This code shares * with Retina and . with Minkolang and SMBF.

Try it online

Explanation

Actual program

#v02^
    @
    .
    *



    t
    5
#v02^

The last line was written for clarity (Befunge playground is cyclic.)

#

Trampoline, skips v

02^

Push 0 and then 2 in stack and go up.

5t*.@

Push 5, no-op, multiply two elements in stack (2 and 5), print, end program.

share|improve this answer
1  
This makes SMBF print a null byte at the first .. – Pietu1998 2 hours ago
    
@Pietu1998 fixed! – JHM 1 hour ago
    
Hey, it wasn't me who chose to use a language where many of the most important commands are nonprintable characters… (In other news, I was considering Befunge as a strong possibility for adding to this polyglot; it seems to fit in well with the other languages. I like the way you did it, although it may well need modifying to fit in more.) – ais523 38 mins ago
    
@ais523 I agree that modifying this code may be difficult. To alleviate this, I put in some 0s to indicate that those characters can be anything (except the "<+0+0+0+<;n4 part) and parts of the Befunge code may be moved around. And a tip for the next person: most of characters are no-op in Befunge, so adding more lines is not likely going to affect the Befunge code. – JHM 28 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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