Encode a program with the fewest distinct characters possible,

12

2

The goal is to write a program that encodes an other program (input) with the fewest character possible.

Scoring

  • The score is equal to the different number of characters needed for the output.
  • Lower score is better.

Rules

  • No target languages with a limited set of commands. (No Brainf**k, Whitespace etc)
    Edit: I mean at least 26 significant characters, A doesn't change the way a brainf**k program operates, so you can't count this character. Same applies to whitespace.
  • The target language must exist at the time where this question is written.
  • You have to include a small explanation how you archive your score.
  • The input program is valid.
  • The encoded program must be a valid program in the same language as the input.
  • The encoded program must do the same job as the original program.
  • Your encoder must work for every valid program in that language.
  • Include some sample input and output.

Notes

  • The encoder might be written in any language, not only in the language it targets.
  • This is not , readable programs are encouraged.
  • The great goal is to see how many different characters are needed to write anything in that language. I disallowed BF etc because there would be no challenge.
  • This was inspired by Print a string in as few distinct characters as possible, you could take it as for that question.

Example

In Java, you can use \uXXXX instead other characters. A valid entry encodes every character from the input this way. This would have a score of 18. (\ 0-9a-f)

Code in Tcl, encodes a Java program:

set res {}
foreach char [split [read stdin] {}] {
    append res [format \\u%04x [scan $char %c]]
}
puts $res

Johannes Kuhn

Posted 2013-05-14T23:32:58.420

Reputation: 7 122

sorry for the vote to close... thought this was a duplicate of http://codegolf.stackexchange.com/questions/6974/ but that one's python-specific.

– boothby – 2013-05-15T00:12:53.290

2With you Java score and your encoder: I don't speak much Tcl, but doesn't gets read a single line of input only? And you missed the u in your encoder (but on the other hand you don't need the space and thus the score remains the same). – Howard – 2013-05-15T04:40:20.120

Maybe you should add a rule along the lines encoded program should (under reasonable constraints) perform the same task as the original version. It is never stated and thus I could transform anything to any (constant) program. – Howard – 2013-05-15T04:43:17.397

Do you rule out a simple conversion to binary, converting the digits to characters and removing the resulting spaces? – Graham – 2013-05-15T09:21:48.337

@Graham In which language would that result be a valid program? – Howard – 2013-05-15T09:50:12.777

@Howard I guess I should have read rule 5 more carefully if that means it should run as is in its encoded form :( – Graham – 2013-05-15T10:02:00.687

possible duplicate of Print a string in as few distinct characters as possible

– Peter Taylor – 2013-05-15T14:16:44.433

4I'm voting to close this because it has a broken victory condition patched over by a very unclear ban on languages. If Unary wasn't banned, it'd score 1. If brainfuck wasn't banned, it'd score 7 (note that the language itself naturally has 8 commands + 248 comment characters). So where's the line between naturally having too few commands, and having enough to be legitimate. – None – 2016-12-28T11:10:23.863

1Even languages like Python have a limited set of commands. You can't "make a new one", you simply create a new function or something, but the reserved words stay the same. The definition of "command" is unclear. The question could possibly be fixed by not limiting languages (just don't upvote trivial answers), and requiring that the set of characters used must be a proper subset of those in the language. – mbomb007 – 2016-12-28T14:28:54.203

so a cat program submitted to this would have a score of 2^30? – Rohan Jhunjhunwala – 2016-12-28T16:55:57.520

Is CJam allowed? It has only 26 variables that can be redefined, which could contain blocks (commands). It's not limited to the original set of commands, but the maximum number of commands is still finite. – jimmy23013 – 2016-12-28T18:51:35.220

And how are subroutine labels in Whitespace, which are not significantly different from C functions, not new commands? – jimmy23013 – 2016-12-28T18:55:38.260

I hope my edit solves your concerns. – Johannes Kuhn – 2016-12-28T22:16:35.303

"at least 26 significant characters" seems to still allow a Beatnik-based answer with a score of 2... or even a Lenguage-based answer with a score of 1! (Neither language truly ignores any characters: Beatnik splits a program into chunks of consecutive alphabetical characters and executes commands based on their Scrabble scores, and a Lenguage program encodes a brainfuck program in its length disregarding any other information.) – Unrelated String – 2019-05-28T23:23:37.110

7Don't all programming languages have a "limited set of commands"? Where do you draw the line? – jpjacobs – 2014-09-15T10:53:08.027

try to make a new command in brainfuck. – Johannes Kuhn – 2014-09-16T15:59:39.707

Answers

10

GolfScript / GolfScript, score 4

"'',,"\{"'"\","*"',+"}%"''+~"

The encoder itself is a GolfScript program which takes the original code on STDIN and transforms it into a sequence of characters ',+~. This output itself is valid GolfScript code which performs the same operations as the original version.

The basic method consists of an encoding of the code as a string (using chars ',+, see below) and then evaluate this string using the eval command ~.

If one concatenates any string together with an array of numbers in GolfScript the numbers are converted to code points and the result is a string itself. Thus the string encoding simply builds a list of numbers (from the code points of the input code) and then concatenates all those with an empty string.

Example:

The input code

"hi"
p

is translated into (note: line breaks and comments added for readability)

# initialize an empty array []
'',,

# append number 34 (length of string ',,, ... ,,,')
',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,',+

# ... other numbers
',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,',+
',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,',+
',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,',+
',,,,,,,,,,',+
',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,',+

# result is array [34 104 105 34 10 112]

# append empt string, i.e. convert array to string (results in "\"hi\"\np")
''+

# evaluate
~

Howard

Posted 2013-05-14T23:32:58.420

Reputation: 23 109

11

Python -> Python, 8 distinct characters

def minimal_python(input_code):
    """Convert Python code to minimal Python code."""

    # Create a list of the ordinal numbers of <input_code>'s characters.
    # '%' signs have to be treated specially and are represented with -1.
    ords = []
    num_escaped_chars = 0
    for char in input_code:
        if char == '%':
            ords.append(-1)
        else:
            ords.append(ord(char))
            num_escaped_chars += 1

    modulo_sign_escape = '%' * 2**num_escaped_chars
    def formatters():
        num_escaped_chars_so_far = 0
        for o in ords:
            if o == -1:
                yield modulo_sign_escape
            else:
                yield '%' * 2**num_escaped_chars_so_far + 'c'
                num_escaped_chars_so_far += 1
    format_str = "'" + ''.join(formatters()) + "'"

    values_str = ''.join('%' + '-~'*o + '0' for o in ords if o != -1)

    return 'exec' + format_str + values_str

This uses modulo formatting to rebuild the input string. For example, print 1 results in this program:

exec'%c%%c%%%%c%%%%%%%%c%%%%%%%%%%%%%%%%c%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%c%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%c'%-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~0%-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~0%-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~0%-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~0%-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~0%-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~0%-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~0

In theory you can encode any program like this, but the resulting program will always have more than 2n characters, where n is the number of characters in the input, not including % symbols.

flornquake

Posted 2013-05-14T23:32:58.420

Reputation: 1 467

The lower bound of the resulting program is 2**n+3*n-1 + 6 characters, including all input characters (if you assume every character is a NUL byte). This is taken from OEIS A132074, plus 6 for exec''. If you assume characters must be printable ASCII, then the lower bound is higher. https://repl.it/EHEN

– mbomb007 – 2016-10-26T19:09:36.633

6

CJam -> CJam, score: 3

CJam is newer than the question so it's not eligible to win.

q{[{_'(>{'(-')*''\+}{is'c+T}?}%{"+"T}*'~]}:T~

It uses ')~. ') is the character ), and each extra ) increments it by one. ~ can evaluate a character or a string. The complete program is evaluated after concatenating all the characters by evaluating +. And an integer of the character value and a number-to-character operation is evaluated for each character less than ).

Examples

XX{_2$+}I*]N*

is translated into:

'))))))))))))))))))))))))))))))))))))))))))))))))'))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~'))))))))))')))~')))))))))))'))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~')))~')))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))')))~'))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))')))~'))))))))))))))))))))))))))))))))))))))')))~'))')))~~

and

q{[{_'(>{'(-')*''\+}{is'c+T}?}%{"+"T}*'~]}:T~

is translated into:

')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))')))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~')))~'))))))))))))'))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~')))~'))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))')))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~')))~'))))))))))))'))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~')))~')))))')))~')))))))))))')))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~')))~')')))~'))')))~')))))))))))')))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~')))~')))))))))))')))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~')))~'))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))')))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))')))~'))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))')))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))'))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~')))~')))')))~')))))))))))'))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~')))~'))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~'))')))~')))))))))))')))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~')))~'))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))')))~')))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~'))))))))))))))))))')))~'))))))))))))))))))))))))))))))))))))))))))))')))~'))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))')))~~

jimmy23013

Posted 2013-05-14T23:32:58.420

Reputation: 34 042

4

APL (score: 10)

'⍎⎕AV[',(⊃,/{'(⍴',(⍵/'⍬'),')'}¨⎕AV⍳⍞),']'

Characters used in encoding: ⍎⎕AV[(⍴⍬)]

The APL program to encode must restrict itself to the APL charset and not use any extra Unicode.

Encoding for the program 42 (which outputs the answer to Life, the Universe, and Everything):

⍎⎕AV[(⍴⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬)(⍴⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬⍬)]

How it works: is the empty list, ⍬⍬⍬⍬ is therefore a list of four empty lists, ⍴⍬⍬⍬⍬ is the length of the list of four empty lists, which is four. Dyadic is reshape, so the length-of-list-of-lists instructions must be wrapped in parenthesis, giving the final encoding of character number four as (⍴⍬⍬⍬⍬).

These numbers are then looked up in ⎕AV which is the character set, and executes the resulting string.

(At first sight it looks like the score could be brought to 9 by using ⎕UCS instead of ⎕AV and saving [], but that doesn't work, because (⍴⍬)(⍴⍬) is a list of lists of scalars, i.e [[1], [1]] instead of the [1, 1] that would be required, and working around that would require interspersing the encoded values with commas, bringing the score back up to 10.)

marinus

Posted 2013-05-14T23:32:58.420

Reputation: 30 224

2

RProgN, 7 Distinct Characters, Noncompeting

RProgN is newer than this question.

' do.Lc

Convert the program to 'oooo...' L c 'ooooo...' L c 'oo...' L c . . . do, where each 'oo...' represents the character code in o's, surrounded by apostrifies to make a string out of them. L c then turns them into a number constant. After all the strings are placed on the stack, a chain of .'s equal to the amount of characters in the encoded string minus one rebuild the encoded string, and do executes it.

Encoder, also written in RProgN.

'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' L c 'ooooooooooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooo' L c 'oooooooooooooooooooooooooooooooooooooooooooooo' L c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . do

Try it online!

ATaco

Posted 2013-05-14T23:32:58.420

Reputation: 7 898

1

Ruby -> Ruby, 8

#Convert a number from 0 to 255 to the form "111+11+11+1+1+1"
def byte_to_ones(byte)
  output = []
  ones = [111,11,1]
  ones.each do |one|
    while byte>=one
      byte-=one
      output<<one
    end
  end
  output.join('+')
end

bytes = $<.each_byte.map { |byte| byte_to_ones(byte) }

puts "eval''<<#{bytes.join('<<')}"

Sample:

puts "Hello, world!" => eval''<<111+1<<111+1+1+1+1+1+1<<111+1+1+1+1+1<<111+1+1+1+1<<11+11+1+1+1+1+1+1+1+1+1+1<<11+11+11+1<<11+11+11+11+11+11+1+1+1+1+1+1<<11+11+11+11+11+11+11+11+11+1+1<<11+11+11+11+11+11+11+11+11+1+1+1+1+1+1+1+1+1<<11+11+11+11+11+11+11+11+11+1+1+1+1+1+1+1+1+1<<111<<11+11+11+11<<11+11+1+1+1+1+1+1+1+1+1+1<<111+1+1+1+1+1+1+1+1<<111<<111+1+1+1<<11+11+11+11+11+11+11+11+11+1+1+1+1+1+1+1+1+1<<11+11+11+11+11+11+11+11+11+1<<11+11+11<<11+11+11+1<<1+1+1+1+1+1+1+1+1+1

Implements the Ruby solution given in the linked problem and replaces I/O with eval.

histocrat

Posted 2013-05-14T23:32:58.420

Reputation: 20 600

It was already linked in the question body; I wasn't using your comment. It's non-trivial to port answers over from there since you need to reverse-engineer them. – histocrat – 2013-05-15T15:34:59.850

1I missed the reference to that question in the question body. I still don't think it's very interesting to just transform answers from it, but I withdraw my previous comment with apologies. – Peter Taylor – 2013-05-15T16:04:27.780