Challenge
Your task is to encode an integer as a string of ASCII characters, then successfully decode it after said string has been randomly shuffled.
You will write two programs/functions, which will be referred to as Encoder and Decoder.
Encoder
- Input: an integer \$n\$ in the range \$[0,2^{31}-1]\$.
- Output: a string \$s\$ of ASCII characters (not necessarily printable).
Decoder
- Input: a random permutation \$s'\$ of the string \$s\$.
- Output: the integer \$n\$.
Scoring
Let \$A\$ be the maximum length of \$s\$ across all possible values of \$n\$. If the Encoder acts non-deterministically (which is allowed, see below), then the \$A\$ will be the maximum length of \$s\$ that may occur (possibly \$\infty\$).
Let \$L_E\$ be the length of the Encoder in bytes and \$L_D\$ the length of the Decoder in bytes.
Then your score is \$A\cdot(L_E+L_D)\$.
Victory is awarded to the submission the lowest score.
Time limit
There is a somewhat arbitrary time limit of 1 minute on the execution time of both the Encoder and the Decoder for a single testcase (i.e. a single value of \$n\$).
The goal is to avoid solution that find that brute-force the encoding by enumerating all sequences with certain properties. If your solution does something more clever than that, it will most likely fit the time constraint and will be considered valid. Likewise, if it works on TIO for some randomly selected values of \$n\$ it will be considered valid. Otherwise I will test it on my machine, but note that if your solution is pure brute-force it will almost certainly fail.
Rules
- The Encoder and the Decoder must be written in the same language.
- The Decoder must output the correct integer \$n\$ for every possible permutation \$s'\$ of the string \$s\$ returned by the Encoder.
- The Encoder and the Decoder are not allowed to share information in any way (e.g. by means of global variables or files).
- The output of the Encoder need not be deterministic (that is, the same input \$n\$ may produce different output strings if the Encoder is run multiple times), but the Decoder must always guess the correct integer \$n\$.
- The Encoder and the Decoder may take and return the integer \$n\$ in any convenient way (e.g. if \$n=14\$ it is fine for the input to be
14
,"14"
or[1,4]
). - The Encoder may output the string \$s\$ either by printing it on
stdout
or by returning a string, a list/array of characters or a list/array of integers in the range \$[0,127]\$; note that the Decoder will receive as input a permutation of \$s\$ as returned by the Encoder, so it should accept the string \$s'\$ in the same format as \$s\$. - Standard loopholes are forbidden.
- If possible, explain how your code works and why the score you claim is correct.
Example
Assume \$n=14\$.
- The Encoder receives
14
as input. It may output"qwerty"
.- The Decoder receives a permutation of
"qwerty"
as input, for instance"tweyqr"
. It must output14
(in any convenient format).
The Encoder could have returned [113,119,101,114,116,121]
as well, in which case the Decoder would have received (for instance) [116,119,101,121,113,114]
.
Note that the string returned by the Encoder may also include non-printable ASCII characters (but always in the range [0x00, ..., 0x7F]
).