Program that permutes itself to encode a string (quine-variant) Write a program that prints the following 80-character line:


  This program from codegolf.stackexchange.com permutes itself to encode a string.


then accepts one line of input, then prints its source code with its code points possibly reordered (none added and none deleted). When that code is executed, the same has to happen, except the printed line would be the most recent line of input.

The Perl-style regex ^[A-Za-z0-9. ]{80}$ will match any line of input. You cannot make any additional assumption.

A submission's score is the number of code points in its source code less 94. Lower is better.

The code must not do anything that would be unacceptable in a quine (e.g. file reading). In particular, any submission with a negative score must be cheating somehow, as 93! is less than 6480.

Added 2014-04-21: Your program's entire source code must be well-formed in the character encoding under which you count code points. For example, you cannot use 80 consecutive bytes in the UTF-8 trailing byte range (80..BF) and count each as a single U+FFFD REPLACEMENT CHARACTER (or worse, as not a code point at all).

Additionally, if the encoding allows multiple ways to encode a code point (e.g. SCSU), your program, as well as all programs it directly or indirectly generates, must only use one of them (or at least all must be treated equivalently throughout the code).
GolfScript, 231 162 131

'1àâ4ÿaVo5GùpZBtiXOürsóNîMmWåKHc09JdñúêyzíECäYïhDU ãáIFõ6é8òRìjTv23ønuðLwxfSkôbëAelqý.çèPQ
öûg7'{0@.$[{}/]:&amp;\{@2$,*2$2$?+@@^}/{;65base}:b~{&amp;=}%''+puts'"#{`head -1`}"'~{&amp;?}%)b[94,{)1$1$%@@/}/;]-1%&amp;\[{1$=.@^}/]"'".@+\+\'.~'}.~


How it works

We start by choosing 94 different characters that will get permuted to encode a string. Any 94 characters would work, but we choose the following for golfing purposes:

\n .0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
àáâãäåçèéêëìíîïðñòóôõöøùúûüýÿ


Let's call the array of these characters “&amp;”.

The line of input will always contain 81 characters (including the LF). All of those characters are present in the first 65 characters of “&amp;”. This is the sole reason for choosing characters in the upper 128 bytes.

We replace each character of the string by its index in “&amp;”, so LF becomes 0, space becomes 1, etc.

We consider the 81 obtained numbers the digits of a single base 65 number. Let's call this number “N”.

Now, we enumerate all possible permutations of “&amp;” and retrieve the permutation corresponding to the number from above. This is achieved in the following manner:


Set c = 1 and A = [].
Prepend N % c to A.
Set N = N / c and c = c + 1.
If c &lt; 95, go back to 2.
Set i = 0 and s = "".
Retrieve the charcter &amp;[A[i]], append it to “s” and remove it from “&amp;”.
Set i = i + 1.
If i &lt; 94 go back to 6.


Suppose we have code blocks “E” and “D” that encode and decode a string as explained above.

Now, we need a wrapper for those code blocks that complies with the requirements of the question:

'encoded string'{\.$[{}/]:&amp;; D puts '"#{`head -1`}"'~ E "'".@+\+\'.~'}.~


This does the following:


{…}.~ defines a block, duplicates it and executes the second copy. The first copy will remain on the stack.
\.$ swaps the encoded string with the block and creates a copy of the encoded string, with sorted characters.
[{}/]:&amp;; converts the string from above into an array, saves it in “&amp;” and discards it.
D puts decodes the encoded string and prints the result.
'"#{`head -1`}"'~ reads one line of input by executing head -1 in the shell.
E "'".@+\+ encodes the string and prepends and appends a single quote.
\'.~' swaps the encoded string and the block and appends the string '.~'.
After the block has been executed, GolfScript prints the contents of the stack (encoded string, block, '.~') and exits.


“E” can be defined as follows:

{&amp;?}%        # Replace each character by its index in “&amp;”.
);           # Remove the last integer from the array, since it corresponds to the LF.
65base       # Convert the array to an integer “N” by considering it a base 65 number.
[            #
  94,        # For each integer “c” in 0 … 93:
  {          #
    )        # Increment “c”.
    1$1$%    # Push “N % c”.
    @@/      # Rotate “N % c” below “N” and “c” and divide the first by the latter.
  }/;        # Discard “N”.
]            # Collect the results of “N % c” in an array “A”.
-1%          # Reverse “A”.
&amp;\           # Push “&amp;” and swap it with “A”.
[            #
  {          # For each “j” in “A”:
    1$=.[]+  # Push “&amp;[j] [&amp;[j]]”.
    @^       # Rotate “&amp;” on top of “[&amp;[j]]” and take their symmetric difference.
  }/         #
]            # Collect the charcters into an array.


“D” can be defined as follows:

0&amp;           # Push 0 (initial value of the accumulator “A”) and “&amp;”.
@            # Rotate the encoded string on top of “&amp;”.
{            # For each character “c” of the encoded string:
    @2$,*    # Rotate “A” on top of the stack and multiply it by the length of “&amp;”.
    2$2$?+   # Get the index of “c” in “&amp;” and add it to “A”.
    @@^      # Rotate “A” below “&amp;” and “c” and take their symmetric difference.
}/;          # Discard “&amp;”.
65base       # Convert “A” into the array of its digits in base 65.
{&amp;=}%        # Replace each digit by the corresponding character in “&amp;”.
''+          # Convert the resulting array into a string.


Final golfing:


Replace \.$[{}/]:&amp;;0&amp;@ with 0@.$[{}/]:&amp;\ to save two characters.
Define the function {;65base}:b to save one character.
Remove all whitespace except the trailing LF and the LF in the string.


Example

$ # Create GolfScript file using base64 to avoid encoding issues.
$ base64 &gt; permute.gs -d &lt;&lt;&lt; JzHg4jT/YVZvNUf5cFpCdGlYT/xyc/NO7k1tV+VLSGMwOUpk8frqeXrtRUPkWe9oRFUg4+FJRvU26TjyUuxqVHYyM/hudfBMd3hmU2v0YutBZWxx/S7n6FBRCvb7ZzcnezBALiRbe30vXTomXHtAMiQsKjIkMiQ/K0BAXn0vezs2NWJhc2V9OmJ+eyY9fSUnJytwdXRzJyIje2BoZWFkIC0xYH0iJ357Jj99JSliWzk0LHspMSQxJCVAQC99LztdLTElJlxbezEkPS5AXn0vXSInIi5AK1wrXCcufid9Ln4K
$
$ # Set locale to en_US (or any other where one character is one byte).
$ LANG=en_US
$
$ # Go back and forth between two different strings.
$ # Second and sixth line are user input, not output from the script.
$
$ golfscript permute.gs | tee &gt;(tail -n+2 &gt; tmp.gs) &amp;&amp; golfscript tmp.gs &amp;&amp; rm tmp.gs
This program from codegolf.stackexchange.com permutes itself to encode a string.
Permuting source code code points to encode a string is a certain quine variant.
'18äJoS3sgV9qdçëxm0ÿKMNe5íPî.Htn2ciâIuøbRZéð4AwB7áìUüöôWõèûfñåLàóDrhQlO6
pTaýzòkùYCyFêïãG júEvX'{0@.$[{}/]:&amp;\{@2$,*2$2$?+@@^}/{;65base}:b~{&amp;=}%''+puts'"#{`head -1`}"'~{&amp;?}%)b[94,{)1$1$%@@/}/;]-1%&amp;\[{1$=.@^}/]"'".@+\+\'.~'}.~
Permuting source code code points to encode a string is a certain quine variant.
This program from codegolf.stackexchange.com permutes itself to encode a string.
'1àâ4ÿaVo5GùpZBtiXOürsóNîMmWåKHc09JdñúêyzíECäYïhDU ãáIFõ6é8òRìjTv23ønuðLwxfSkôbëAelqý.çèPQ
öûg7'{0@.$[{}/]:&amp;\{@2$,*2$2$?+@@^}/{;65base}:b~{&amp;=}%''+puts'"#{`head -1`}"'~{&amp;?}%)b[94,{)1$1$%@@/}/;]-1%&amp;\[{1$=.@^}/]"'".@+\+\'.~'}.~
$
$ # Sort all characters from the original source code and hash them.
$ fold -1 permute.gs | sort | md5sum
b5d978c81df5354fcda8662cf89a9784  -
$
$ # Sort all characters from the second output (modified source code) and hash them.
$ golfscript permute.gs | tail -n+2 | fold -1 | sort | md5sum
Permuting source code code points to encode a string is a certain quine variant.
b5d978c81df5354fcda8662cf89a9784  -
$
$ # The hashes match, so the characters of the modified source code are a permutation
$ # of the character of the original one.