25
1
Starry is a funny esoteric programming language in which code consists only of +*.,`'
where the actual command represented by each of those characters is determined by the number of spaces in front of it. That makes it tricky even to golf fixed-output challenges, because different commands can account for vastly different numbers of bytes. In particular, number literals have a unary representation which makes it necessary to build up larger numbers by operating on smaller ones.
Therefore, this challenge is about writing a program which can golf such Starry programs.
How does Starry work?
(A few details are left unspecified on esolangs, so I'm going with the behaviour of the Ruby interpreter.)
Starry is a stack-based language, with a single stack of arbitrary-precision integer values (which is initially empty).
The only meaningful characters are:
+*.,`'
and spaces. All other characters are ignored. Each sequence of spaces followed by one of those non-space characters represents a single instruction. The type of instruction depends on the non-space character and the number of spaces.
The instructions are:
Spaces Symbol Meaning
0 + Invalid opcode.
1 + Duplicate top of stack.
2 + Swap top 2 stack elements.
3 + Rotate top 3 stack elements. That is, send the top stack element
two positions down. [... 1 2 3] becomes [... 3 1 2].
4 + Pop and discard top of stack.
n ≥ 5 + Push n − 5 to stack.
0 mod 5 * Pop y, pop x, push x + y.
1 mod 5 * Pop y, pop x, push x − y.
2 mod 5 * Pop y, pop x, push x * y.
3 mod 5 * Pop y, pop x, push x / y, rounded towards -∞.
4 mod 5 * Pop y, pop x, push x % y. The sign of the result matches the sign of y.
0 mod 2 . Pop a value and print it as a decimal number.
1 mod 2 . Pop a value and print it as an ASCII character. This throws an error
if the value is not in the range [0, 255].
n ` Mark label n.
n ' Pop a value; if non-zero, jump to label n.
Note that the interpreter scans the source code for the labels before execution begins, so it's possible to jump forwards as well as backwards.
Of course, Starry also has input commands (using ,
analogously to .
), but those are irrelevant for this challenge.
The Challenge
Given a string, generate a Starry program which takes no input and prints that string exactly to STDOUT.
You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter.
You may assume that the string is not longer than 128 characters and that it will consist only of printable ASCII characters (code points 0x20 to 0x7E).
Your solution should process any such input in less than 5 minutes on a reasonable desktop machine (there's some leeway to this; if it takes a few minutes more on my laptop I don't mind, but if it takes 15, I'll disqualify it).
Your solution will be tested on a number of different strings listed below. Your score is the total byte count of the corresponding Starry programs. In the case of a tie, the shortest metagolfer wins. That is, don't bother golfing your own code unless there is a tie (which I think will only happen in the case that an optimal solution is possible).
You must not optimise your code towards the specific test cases listed below. Specifically, you shouldn't hardcode hand-crafted solutions for them. Optimising towards classes of strings whose structure is similar to that of the given strings is fine. If I suspect anyone of hardcoding solutions, I reserve the right to replace some or all of the test cases (with strings of comparable structures).
Test Cases
Each line is a separate test case:
Hello, World!
pneumonoultramicroscopicsilicovolcanoconiosis
.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
Hickory, dickory, dock. The mouse ran up the clock. The clock struck 1. The mouse ran down. Hickory, dickory, dock.
36912059868043514648560046917066768694455682545071266675083273015450033938555319356951628735735013250100789433961153496780296165
bVZ48121347GLtpYnt76CZSxTpMDs6791EJE808077eySXldY162424ddTB90707UupwlWGb63618542VhA252989453TXrWgqGm85899uHOAY2oAKE198GOVUttvW63
7MYxoWBNt180CDHS5xBGvU70HHVB17bh8jYzIIiU6n6g98Rose1nOe8Svcg56nax20q30kT3Ttb2jHl5q2Iuf1vPbjPxm9cyKXwxc0OUK8pr13b2n7U9Y7RwQTc26A1I
n9}unwxVa}[rj+5em6K#-H@= p^X/:DS]b*Jv/_x4.a5vT/So2R`yKy=in7-15B=g _BD`Bw=Z`Br;UwwF[{q]cS|&i;Gn4)q=`!G]8"eFP`Mn:zt-#mfCV2AL2^fL"A
Credits for the second test case go to Dennis. Credits for the fourth test case go to Sp3000.
Reference Solution
Here is a really basic reference solution in CJam:
q{S5*\iS*'+S'.}%
You can run it against the entire test suite here. The scores are:
1233
5240
4223
11110
7735
10497
11524
11392
Total: 62954
It's the simplest possible approach: push each character's code point as a literal, and then print it. It makes no use of small differences between consecutive characters, integer printing, repetitive parts of the string etc. I'll leave those things to you.
I believe there is a lot of room for improvement. For reference, the shortest handcrafted "Hello, World!" is only 169 bytes long.
Hooray, someone to battle :-) Of course, now I see that mine has a long way to go... – ETHproductions – 2015-11-17T01:26:21.833