11
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Is trailing whitespace allowed in the output? \$\endgroup\$ – Arnauld Oct 25 at 15:17
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – FryAmTheEggman Oct 25 at 17:46
  • 1
    \$\begingroup\$ Are 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. \$\endgroup\$ – gastropner Oct 30 at 0:50
  • \$\begingroup\$ Can we output in uppercase (ABC instead of abc)? \$\endgroup\$ – Grimmy Oct 31 at 13:45
6
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Keg! \$\endgroup\$ – Jono 2906 Oct 25 at 22:32
6
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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)

\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
3
\$\begingroup\$

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

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

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

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

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.

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

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.

\$\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.