Restricted-source polyglot string

12

1

Your goal is to write a program that prints the string abc in two languages. However, while the full code will print abc in one language (we'll call it Language 1), the other one (Language 2) must print abc if (and only if) every other character is skipped starting from the second character. If the full code is run in language 2, it must throw an error. If the skipped code is run in language 1, likewise, it must throw an error.

Rules and Clarification

  • Trailing newlines are allowed.
  • Output must be lowercase.
  • "Run" means compiled for compiled languages.
  • We will define "throwing an error" as outputting nothing to STDOUT and something to STDERR.

Test Cases

Language 1: 12345 -> abc
Language 1: 135 -> error
Language 2: 135 -> abc
Language 2: 12345 -> error

Scoring

The shortest code in bytes wins (the full code, not the skipped code).

Edit: I have clarified the rules based on feedback in the comments. The existing answers should still be valid.

fle

Posted 2019-10-25T14:56:53.490

Reputation: 121

Is trailing whitespace allowed in the output? – Arnauld – 2019-10-25T15:17:49.513

2

What precisely counts as an error? Is outputting nothing to STDOUT and something to STDERR sufficient? Consider this potential submission, where the Foo program that "errors" encounters an error that doesn't cause the program to halt, but it has no other output.

– FryAmTheEggman – 2019-10-25T17:46:05.733

1Are we to interpret "run" as "compiled" for compiled languages? Otherwise the implication would be that both shortened and full programs must compile in both languages, but throw run-time errors upon execution if the wrong length. – gastropner – 2019-10-30T00:50:40.050

Can we output in uppercase (ABC instead of abc)? – Grimmy – 2019-10-31T13:45:43.550

Answers

8

brainfuck, 28 26 bytes

a+b[c-/[/---<]>>- ]<-.+.+.

Try it online!

Got the bf code from here.

Fails in Ink due to Expected target for new thread but saw '.+.+.'.

ink

abc//-<>-]-++

Try it online!

Ink will just print abc and the rest is a comment.

Fails in brainfuck due to mismatched bracket.

79037662

Posted 2019-10-25T14:56:53.490

Reputation: 1 739

6

Keg, 21 19 bytes (19 17 characters)

abc#1 / ╘ " a b c

Try it online!

Simply prints abc and the rest is a comment.

Fails in MathGolf due to an undefined operator overload of /. / can not work on an integer (1) and a string( pushed via the space instruction.)

MathGolf

ac1/╘"abc

Try it online!

Does a bunch of nonsense, then deletes the whole stack. "abc pushes abc and the stack has an implicit output.

Fails in Keg due to the failure of implicit-outputting a floating-point number in its character form.

79037662

Posted 2019-10-25T14:56:53.490

Reputation: 1 739

1Welcome to Keg! – Lyxal – 2019-10-25T22:32:38.377

5

Japt and Pyth, 20 bytes

Both are shortenings of common scripting languages, nice

Japt: "_a_b_c_"v ë2,1 +[] (TIO, error in Pyth)

Pyth: "abc" 21 [ (TIO, error in Japt)

randomdude999

Posted 2019-10-25T14:56:53.490

Reputation: 789

3

PHP and Emoji, 21 chars, 30 bytes

Full:

⛽_a_b_c_➡➡;echo abc;

Skipped:

⛽abc➡eh b;

PHP, Full - Try it online! Prints: abc

Emoji, Skipped - Try it online! Prints: abc

PHP, Skipped - Try it online! Error: PHP Parse error: syntax error, unexpected 'b' (T_STRING)

Emoji, Full - Try it online! Error: IndexError: pop from empty list

Night2

Posted 2019-10-25T14:56:53.490

Reputation: 5 484

3

C++ (gcc) / C (gcc), 87 bytes

Full (C++)

//*
#import<cstdio>
main(){puts("abc"/*//
m_a_i_n_(_)_{_p_u_t_s_(_"_a_b_c_"_/*/


) ; }

Skipped (C)

/*#motcti>mi({us"b"*/main(){puts("abc"//
);}

Full C++, Working

Skipped C++, Error

Full C, Error

Skipped C, Working

gastropner

Posted 2019-10-25T14:56:53.490

Reputation: 3 264

2

JavaScript (V8) and PHP, 29 bytes

Full:

print`abc`;'e_c_h_o_ _a_b_c';

Skipped:

pitac;echo abc;

JavaScript, Full - Try it online! Prints: abc

PHP, Skipped - Try it online! Prints: abc

JavaScript, Skipped - Try it online! Error: SyntaxError: Unexpected identifier

PHP, Full - Try it online! Error: abc: command not found

Night2

Posted 2019-10-25T14:56:53.490

Reputation: 5 484

2

CJam and Gaia, 15 bytes

(CJam only cares about printable ASCII, so Gaia's codepage can be used to encode both programs)

Full:

 'a'b'c'” 'ọ;e<

Works in CJam

Errors in Gaia

Reduced:

 abc”'ọe

Works in Gaia

Errors in CJam

Explanation

This takes some advantage of the fact that ' defines a character literal in both languages.

Full program:

We push 5 characters: a, b, c, , and . ; deletes the and e< leaves the minimum of c and , which is c. The remaining characters (abc) are joined together and output implicitly.

In Gaia, everything up to is a string literal. We then push the char , and then copy (;) the string literal back to the top and attempt to eval it (e). This throws an error because the last character in that string is ', which is improper syntax (a character literal after it is expected).

Reduced program:

(or, more specifically, byte 0xFB) is not defined, so the program terminates with an error.

In Gaia we get the string literal  abc (with leading space). We then push the character and eval it, which is the command for trim. This leaves plain abc on the stack, which is implicitly output.

Business Cat

Posted 2019-10-25T14:56:53.490

Reputation: 8 927

2

Turing Machine Code and Whitespace, 85 bytes

Full

 	 0 _	a	r 1 ; ; ;	;
	
 1 _ b r 2	;	; ; ; ;	; ;
	
 2 _ c * 3	;	; ; ; ;	;	;
	;
 ; ;



Works in Turing Machine Code
Errors in Whitespace: Prelude.!!: index too large

Reduced












Works in Whitespace
Errors in Turing Machine Code: Halted. No rule for state '0' and symbol '_'.

Takes advantage of the fact that Turing Machine Code ignores most white space and Whitespace pretty much ignores everything else.

ouflak

Posted 2019-10-25T14:56:53.490

Reputation: 925

2

Backhand and ><>, 39 bytes

Full

8.cv*c0*~]!]/]:::[@:1[+ooo2o+@o.3.+.o.;

Prints abc in Backhand

Errors in ><>

Skipped

8c*0~!/::@1+o2+o3+o;

Prints abc in ><>

Errors in Backhand

Explanation

Backhand doesn't execute the instructions one by one. In the full version, it goes 3, then the v instruction turns it to two steps. In the skipped version, this has the consequence that only the first instruction overlaps with the full version.

When you run the skipped version on Backhand, it divides by zero. Errors in backhand don't stop the program, so I added a stop command, which makes ><> a bit harder. (It's still a command in ><>, just not a good one.)

><>, on the other hand, does not skip instructions. In the full version, a . means jump. Fish is a 2D language, so it pops two arguments. However, only 1 thing was pushed (because of 8). Thus, something smells fishy....

How I get around some of the commands in ><> is simple. When 0 is executed, it pushes 0, then it immediately removes it. Then it skips the next command, which is a mirror. Finally, it duplicates twice, meaning 3 of the same thing is in the stack, which means @, which cycles the top 3 items in the stack, does absolutely nothing.

Want

Posted 2019-10-25T14:56:53.490

Reputation: 407

Hi! Welcome to CGCC! I hope you enjoy your stay! – Lyxal – 2020-01-20T09:27:35.280

1

An interesting way of doing it, but both ><> and Backhand have the " operator, which should make it a lot easier to push strings. You can do something like this for 29 bytes, though I'm sure that's easily golfable, perhaps by swapping the two languages

– Jo King – 2020-01-21T02:45:58.423

I only found out about backhand having it after I made this. – Want – 2020-01-21T04:53:27.670

1

naz / 7, 54 bytes

6a6a5a5m1a5a1a5a1o5s1a5a1o5s1a5a1o0m3a4a1a2a2a7a4a0a3a

This has to be the most esoteric answer I've submitted so far.

Explanation of the full code (works in naz)

6a6a5a5m1a5a1a5a     # Set the register to a value of 97 ("a")
1o                   # Output once
5s1a5a1o             # Output "b"
5s1a5a1o             # Output "c"
0m3a4a1a2a2a7a4a0a3a # Extra arithmetic to allow for a valid 7 program

Fails in 7: 6 command run with no bar in the frame at /opt/7/7.pl line 605, <> chunk 1.

The reduced code (works in 7)

665515151515151510341227403

Try it online!

Fails in naz: error: attempt to chain number literals

I can't give a very accurate explanation of the reduced code due to my relatively poor understanding of 7. I invite anyone who can make sense of it to improve this post.

sporeball

Posted 2019-10-25T14:56:53.490

Reputation: 461

0

GolfScript / MathGolf, 15 bytes

Here is a golfing language one in pure ASCII. (I guess the best score possible is 15 bytes...)

"abc"#; " a b c

Try it online!

GolfScript explanation

"abc"           # Start a string
     #          # Create a comment (rest is a comment)
      ; " a b c

MathGolf explanation

"abc"           Start a string
     #;         Perform expoentiation between the string and the input (implicitly a string)
                This isn't defined yet, so it throws an error
                Discarding the value won't be executed
        " a b c So does this

MathGolf/GolfScript, 8 bytes

"b";"abc

Try it online!

MathGolf Explanation

"b";     # Push b and then discard the value
    "abc # Push abc and then implicitly print

GolfScript explanation

"b";     # Push b and then discard the value
    "abc # Unterminated string, throws an error

user85052

Posted 2019-10-25T14:56:53.490

Reputation: