20
\$\begingroup\$

Your challenge, should you choose to accept it, is to write a program in a language of your choice that, when given a string (limited to printable ASCII) as input, outputs a new program in the same language that outputs that string without using any characters from that string in the code.

But this task is impossible as-is in many languages; for example, if the string to print contains every ASCII character. So instead, your submission will choose a subset of printable ASCII which is guaranteed not to be in the input. Your score will be the size of the set, with lower score being better, and shortest code length (in bytes) as a tiebreaker.

An example solution might be the following:

Python, 64 bytes / score 17

lambda s:"print('\\"+'\\'.join([oct(ord(c))[2:]for c in s])+"')"

Restricting the set print()'\01234567.

Now that's enough dawdling—go program!

Rules / clarifications

  • "Same language" includes any compiler flags used in the submission.
  • The outputted program should be a full program with output on STDOUT.
\$\endgroup\$
11
  • 1
    \$\begingroup\$ @UnrelatedString I don't know what you mean. The printable ASCII thing is just that you can assume the input string will always be printable ASCII, the output can be whatever you want. \$\endgroup\$ Commented Oct 31, 2023 at 1:32
  • 2
    \$\begingroup\$ @Jonah I had never written the word out before and tried to look it up but I didn't find the right spelling, thanks :) \$\endgroup\$ Commented Oct 31, 2023 at 1:44
  • 1
    \$\begingroup\$ Must be a full program, will clarify \$\endgroup\$ Commented Oct 31, 2023 at 3:16
  • 1
    \$\begingroup\$ »Printable« refers to the regex [:print:] class (ASCII range 0x20–0x7E)? Thus, it includes the whitespace 0x20, but not the TAB 0x09? \$\endgroup\$
    – Philippos
    Commented Nov 14, 2023 at 7:33
  • 1
    \$\begingroup\$ @Philippos Yup. \$\endgroup\$ Commented Nov 14, 2023 at 11:47

31 Answers 31

1
2
0
\$\begingroup\$

GolfScript, 24 22 18 17 bytes, score 4

{("1+"*1n@}/'](+'

Try it online!

The restricted set is (]1+. I'm pretty sure this is minimal, but GolfScript is a weird language and I'm no expert on it.

This constructs an a program that looks something like:

1
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+](+

This pushes arbitrary codepoints to the stack with 1 1+1+...1+, using a newline as whitespace because it isn't printable. Then, ] ends a list literal, which turns the whole stack into a list because there was no previous [. Finally, (+ coerces the list of codepoints to a string by adding it to the empty input, and the result is implicitly outputted.

Here's an explanation of the generator code, which I think is probably also as short as it can get:

{("1+"*1n@}/­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌­
{         }/  # ‎⁡For each character of the (implicit) input:
 (            # ‎⁢  Decrement the codepoint.
  "1+"*       # ‎⁣  Repeat "1+" that many times.
       1n     # ‎⁤  Push the number 1, then push a newline.
         @    # ‎⁢⁡  Move the "1+1+..." string to the top of the stack.

'](+'         # Push the string ](+ to the stack.
              # The entire stack is implicitly outputted.
💎

Created with the help of Luminespire.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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