5
\$\begingroup\$

Consider the set of 16-character strings containing only the 95 printable ASCII characters. There are 95164.4×1031 such strings.

Imagine these strings are put into a list and sorted lexicographically (which is the way most languages will sort a list of strings). What is the largest contiguous slice of this list you can find such that every string is a valid program in your programming language of choice whose output is the sole word string?

All output should go to the same place for all programs; use stdout or a similar alternative. An optional trailing newline may be a part of the output, but otherwise nothing besides string should be printed for any of your 16-character programs. There is no input and all programs should run without erroring. The program does not have to terminate.

The list of length 16 strings does not "loop-around" from ~~~~~~~~~~~~~~~~ to                  (16 spaces).

Give your starting string and ending string and explain why every program-string that's in between them lexicographically outputs string.

The submission with the largest slice wins.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Is it my imagination or is this challenge just "find the language that prints a string literal and can throw away the most additional characters"? \$\endgroup\$
    – Sparr
    Commented Dec 6, 2015 at 22:16

6 Answers 6

8
\$\begingroup\$

///, 6634204313747905 > 958

From

string/.~~~~~/0 

to

string/0     /.~

This relies on the program not being required to terminate. I started from a core-range of 958 much like the other solutions, from

string//        

to

string//~~~~~~~~

However, in this case, // is not a comment. In /// everything that's not a slash is just printed (hence this always prints string before doing anything else). A slash then starts a substitution pattern of the form /pattern/replacement/. If only one or two / are found, then the program simply terminates. However, the above range obviously also includes strings with three or more slashes. The trick here is that the pattern to be searched for is empty, and substitutions are performed iteratively until the pattern cannot be found any more. Since the empty string can always be found, this substitution never terminates, and hence the program never gets around to print anything after the initial string.

But since this doesn't rely on // being a comment, we can actually go a bit further! E.g. the previous and next string around this range, respectively, are:

string/.~~~~~~~~
string/0        

These still don't contain a full substitution instruction so they also only print string and then terminate. Let's see how far we can go! I'll focus on the upper end of the range (the lower end works analogously). What if we get another /? The first time this happens is

string/0       /

But that's still not a valid replacement pattern, so all is well. It looks like we need three slashes to break the magic. The first time we get three slashes is at

string/0      //

What now? Well, the pattern to be searched for is 0 (and a bunch of spaces), to be replaced with an empty string. That pattern is never found, so the substitution completes immediately. But luckily, there is nothing to print afterwards in the source code, so the result is still only string. Neat. :)

So we need a valid substitution instruction and more source code afterwards to not print string:

string/0     // 

And this is indeed the first code that doesn't work to spec, as it prints string followed by a space. So the last code that works is

string/0     /.~

We can apply the same argument to the beginning of the range, the last failing code being

string/.~~~~~//~

such that the first code that works is

string/.~~~~~/0 
\$\endgroup\$
3
\$\begingroup\$

pyexpander: 95^8 = 6634204312890625

string$#        

to

string$#~~~~~~~~

Wow, it took forever to find the language I was looking for: one that outputs most source literals untouched and has a rest-of-line comment delimiter. This is still not theoretically optimal, as it's possible that another language like this exists, but with a one-character comment delimiter. However, I couldn't find one.

\$\endgroup\$
1
  • \$\begingroup\$ This is almost 100 times better than the other answers. xD \$\endgroup\$ Commented Feb 5, 2015 at 19:49
2
\$\begingroup\$

GolfScript: 95^7 = 69833729609375

"string"#       

to

"string"#~~~~~~~

Everything from the # is a comment; the literal string is echoed at the end of execution.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think your score is actually a bit higher. The code before your first one is "string""~~~~~~~ and as far as I know and unmatched " acts like a "supercomment" just as an unmatched } does. So I think your earliest valid code is "string""~~~~~~#. \$\endgroup\$ Commented Feb 6, 2015 at 9:21
  • \$\begingroup\$ @MartinBüttner, no, the parser complains "Unterminated string meets end of file". \$\endgroup\$ Commented Feb 6, 2015 at 9:26
2
\$\begingroup\$

PHP, 95^7 = 69833729609375

string<?#

followed by arbitrary characters.

\$\endgroup\$
1
  • \$\begingroup\$ +1 for using my pet language. Golfing in PHP is a real challenge :) \$\endgroup\$
    – user16991
    Commented Feb 6, 2015 at 14:08
1
\$\begingroup\$

Microscript, 95^6=735091890625

"gnirts"ah (padded to 16 chars with trailing spaces) through "gnirts"ah~~~~~~

\$\endgroup\$
0
\$\begingroup\$

Windows Powershell: 95^7 = 69833729609375

"string"#

to

"string"#~~~~~~~

All characters from the # and following are part of a comment.

Same as @PeterTaylor's GolfScript answer

\$\endgroup\$

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.