29
\$\begingroup\$

Your challenge is to write N snippets of code such that, when you concatenate the first K ≥ 1 together, they produce the number K. The higher N, the better. Here's the catch: you may not use any character more than once across your snippets.

Rules

  • You may use the same character multiple times in one (and only one) snippet.
  • These snippets must be concatenated in the order they are presented, without skipping any.
  • You must write at least two snippets.
  • All snippets must be in the same language.
  • Remember: snippets do not have to be full programs or functions, nor do they have to function on their own. -1 is a valid snippet in Java, e.g.
  • All resulting concatenations must output the respective K value.
  • The winner is the person with the highest N value. Tie-breaker is shortest overall program length in bytes.

Example

Suppose your snippets were AD, xc, 123, and ;l. Then:

  • AD should produce 1
  • ADxc should produce 2
  • ADxc123 should produce 3
  • and ADxc123;l should produce 4.

This program would have a score of 4.

\$\endgroup\$
  • 4
    \$\begingroup\$ Since they have to be snippets, in a stack-based language, the numbers can just be pushed on the stack, right? \$\endgroup\$ – totallyhuman Oct 31 '17 at 18:58
  • \$\begingroup\$ To add to totallyhuman's question, in a stack-based language is top-of-stack the only value that matters? That is, could the first two snippets in dc be 1 and ` 2`? \$\endgroup\$ – brhfl Oct 31 '17 at 19:26
  • \$\begingroup\$ @totallyhuman I would say no--in a stack based environment, if you had multiple values on the stack, you "produced" more than one value, instead of the requested one integer. \$\endgroup\$ – Conor O'Brien Oct 31 '17 at 22:48
  • \$\begingroup\$ @brhfl See above. \$\endgroup\$ – Conor O'Brien Oct 31 '17 at 22:48
  • \$\begingroup\$ @ConorO'Brien Could just the top of the stack be considered output? 'Cause otherwise, it's practically impossible in a stack-based language with no implicit IO... \$\endgroup\$ – totallyhuman Nov 1 '17 at 2:18

28 Answers 28

10
\$\begingroup\$

Python 3, 1 112 056 snippets, 4 383 854 bytes

This is very similar to @WheatWizard's Python 2 answer. I started working on this shortly before it was posted, but sorting out Python's quirks regarding non-ASCII characters and long lines took some time. I discovered that Python reads lines 8191 bytes at a time, and when those 8191 bytes contain only a part of a multi-byte character, Python throws a SyntaxError.

The first snippet uses an encoding from Fewest (distinct) characters for Turing Completeness.

exec('%c'%(111+1)+'%c'%(111+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+11)+'%c'%(111+1+1+1+1+1)+'%c'%(11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+11)+'%c'%(11+11+11+1+1+1+1+1+1+1)+'%c'%(111)+'%c'%(111+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+11)+'%c'%(11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1)+'%c'%(11+11+11+11+11+11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+1+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+1+1)+'%c'%(111+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1)+'%c'%(11+11+11+11+11+11+11+11+1+1+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1)+'%c'%(11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+1+1+1+1+1+1+1+1)+'%c'%(11+11+11+1+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+1)+'%c'%(11+11+11+11+1+1+1+1+1)+'%c'%(11+11+11+11+1+1+1+1+1+1)+'%c'%(11+11+11+11+11)+'%c'%(11+11+11+11+1+1+1+1)+'%c'%(11+11+11+1+1+1+1+1+1+1++++++++++1))

This monstrosity simply build the following string and executes it.

print(len(open(__file__).read())-1260)

The following snippets are all exactly one character long. The next three characters are \n, \r, and #. All remaining Unicode characters (except surrogates) follow in a specific order, so they align with the 8191-byte boundary.

The following script generates the appropriate programs for input k between 1 and 1112056.

j = 4
s = "exec('%c'%(111+1)+'%c'%(111+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+11)+'%c'%(111+1+1+1+1+1)+'%c'%(11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+11)+'%c'%(11+11+11+1+1+1+1+1+1+1)+'%c'%(111)+'%c'%(111+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+11)+'%c'%(11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1)+'%c'%(11+11+11+11+11+11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+1+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+1+1)+'%c'%(111+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1+1)+'%c'%(11+11+11+11+11+11+11+11+1+1+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+11+11+11+11+11+1)+'%c'%(11+11+11+1+1+1+1+1+1+1)+'%c'%(11+11+11+1+1+1+1+1+1+1+1)+'%c'%(11+11+11+1+1+1+1+1+1+1+1)+'%c'%(11+11+11+11+1)+'%c'%(11+11+11+11+1+1+1+1+1)+'%c'%(11+11+11+11+1+1+1+1+1+1)+'%c'%(11+11+11+11+11)+'%c'%(11+11+11+11+1+1+1+1)+'%c'%(11+11+11+1+1+1+1+1+1+1++++++++++1))"
l = 1
c = \
        [
                None,
                [n for n in range(0x80) if chr(n) not in "\n\r#%'()+1cex"],
                [*range(0x80, 0x800)],
                [*range(0x800, 0xd800), *range(0xe000, 0x10000)],
                [*range(0x10000, 0x110000)]
        ]

k = int(input())
assert k in range(1, 1112057)
s += '\n\r#'[:k - 1]
k -= 4

while j:
                while k > 0 and c[j] and l + j < 8191:
                        s += chr(c[j].pop())
                        l += j
                        k -= 1
                if k < 1:
                        print(end = s)
                        break
                elif c[j] == []:
                        j -= 1
                else:
                        s += chr(c[8191 - l].pop())
                        print(end = s)
                        k -= 1
                        s = ''
                        l = 0
\$\endgroup\$
  • 4
    \$\begingroup\$ Do you ever lose? \$\endgroup\$ – Patrick Roberts Nov 1 '17 at 18:26
  • \$\begingroup\$ I'm confused as to how you have scored more than 256. Are different unicode characters different characters? If so why not use combining diacritics to obtain an infinite score? \$\endgroup\$ – Wheat Wizard Nov 2 '17 at 4:56
  • \$\begingroup\$ @WheatWizard What is a character? \$\endgroup\$ – Dennis Nov 2 '17 at 15:18
  • \$\begingroup\$ It seems that by that definition you can use combining diacritics to get a higher score. \$\endgroup\$ – Wheat Wizard Nov 2 '17 at 16:42
  • \$\begingroup\$ @WheatWizard No, a letter plus a combining diacritic is two Unicode characters. \$\endgroup\$ – Dennis Nov 2 '17 at 17:58
13
\$\begingroup\$

Perl 5, 50,091 151 snippets

First snippet:

use utf8; print length A

2nd through 26th snippets: B through Z

27th through 46nd snippets: a through z, excluding the characters in "length"

47th through 56th snippets: 0 through 9

57th snippet: _

The remaining snippets are the 50,105 individual Unicode characters which Perl regards as "word" characters, excluding the 14 distinct word characters in the initial snippet, in any order.

Well, it was a nice thought, but it turns out that after a certain length Perl gives you an "identifier too long" error. This is the longest combined program I was able to get Perl to digest:

use utf8; print length A012345679BCDEFGHIJKLMNOPQRSTUVWXYZ_abcdjkmoqsvwxyzĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţ

The perldiag manual page says "Future versions of Perl are likely to eliminate these arbitrary limitations" but my Perl 5.18 has not done so.

Explanation:

In non-strict mode, Perl 5 interprets unquoted strings of word characters as "barewords," essentially quoting them for you automatically. They're usually best avoided, but they sure help here!

\$\endgroup\$
  • 4
    \$\begingroup\$ Your a-z snippets will most likely use characters from your first snippet. \$\endgroup\$ – Jonathan Frech Oct 31 '17 at 18:44
  • \$\begingroup\$ Yes indeed, thanks. Fixed. \$\endgroup\$ – Sean Oct 31 '17 at 18:46
  • \$\begingroup\$ I suggest that you make a "showcase"-like answer, because almost all (exo)langs - jelly, pyth, etc - have this behavior \$\endgroup\$ – Rod Oct 31 '17 at 18:49
  • \$\begingroup\$ I don't know what "showcase-like answer" means. \$\endgroup\$ – Sean Oct 31 '17 at 18:52
  • 1
    \$\begingroup\$ @Sean Plenty can be found on esolangs.org, and because this approach does not require a thorough understanding to work, you can learn what you need from the site. In addition, many non-esolangs exhibit this behavior; for example, TI-BASIC's first snippet would be length("length(. \$\endgroup\$ – Khuldraeseth na'Barya Oct 31 '17 at 23:18
10
\$\begingroup\$

Python 2, score 32

for r in range(32):locals()[''.join(map(chr,range(65,66+r)[:26]+range(117,92+r)))]=r+1
print A

With subsequent snippets B, C, D, … Y, Z, u, v, w, x, y, z.

In a twist of dramatic irony, Python 3 supports Unicode identifiers, which would let us get very silly with this trick — but it can’t print without parentheses. I could cram digits into the identifier, too, but I don’t think this approach is very fun to squeeze more out of.

Try it online!

Python 2, score 18, less cheat-y

print 0x10-1&0x1
print 0x10-1&0x12
print 0x10-1&0x123
print 0x10-1&0x1234
print 0x10-1&0x12345
print 0x10-1&0x123456
print 0x10-1&0x1234567
print 0x10-1&0x12345678
print 0x10-1&0x123456789
print 0x10-1&0x123456789A
print 0x10-1&0x123456789Ab
print 0x10-1&0x123456789Abc
print 0x10-1&0x123456789Abcd
print 0x10-1&0x123456789AbcdE
print 0x10-1&0x123456789AbcdEf
print 0x10-1&0x123456789AbcdEf^((()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==()))
print 0x10-1&0x123456789AbcdEf^((()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==()))|[[[]]>[]][[]>[]]
print 0x10-1&0x123456789AbcdEf^((()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==())+(()==()))|[[[]]>[]][[]>[]]<<False**False

Try it online!

\$\endgroup\$
10
\$\begingroup\$

JavaScript (ES6, V8 6.x), 52 50298 119526 119638 119683 128781 snippets, 88 149147 575179 575631 576121 612789 bytes

Farther below is a Stack Snippet that generates the full program, evaluates it, and creates a download link for the file. That snippet will continue to generate better answers as later versions of Unicode are supported by newer versions of JavaScript, which add new valid identifiers to the language.

Using ASCII only

console.log(new Proxy({},{get:(n,{length:e})=>e>>(e/e)}).nn$$00112233445566778899AABBCCDDEEFFGGHHIIJJKKLLMMNNOOQQRRSSTTUUVVWWXXYYZZ__aabbccddffiijjkkmmppqqssuuvvzz)

Explanation

This uses the metaprogramming technique of Proxy to enable a get handler trap on the object and access the property name as a string, returning the identifier's length / 2 as its value.

With the first snippet starting as new Proxy({},{get:(n,{length:e})=>e>>(e/e)}).nn, each additional snippet added increments the string length of the identifier by 2 by making sure to .repeat() the respective code point twice for 2 byte utf-16 characters, and once for 4 byte utf-16 characters.

Identifiers in JavaScript

In the ECMAScript Specification, an IdentifierName is defined with the following grammar:

IdentifierName::
  IdentifierStart
  IdentifierName IdentifierPart

IdentifierStart::
  UnicodeIDStart
  $
  _
  \UnicodeEscapeSequence

IdentifierPart::
  UnicodeIDContinue
  $
  _
  \UnicodeEscapeSequence
  <ZWNJ>
  <ZWJ>

UnicodeIDStart::
  any Unicode code point with the Unicode property “ID_Start”

UnicodeIDContinue::
  any Unicode code point with the Unicode property “ID_Continue”

Generating the answer

Initially using the "ID_Continue" Unicode property, I wrote a Node.js script that generates the full answer. Now it's just a client-side script that uses a naive eval() to test for valid characters, iterating through all the unicode code points instead:

// first snippet
let answer = 'new Proxy({},{get:(n,{length:e})=>e>>(e/e)}).nn'

const used = Array.from(
  answer,
  c => c.codePointAt(0)
).sort(
  (a, b) => a - b
)

// create a O(1) lookup table for used characters in first snippet
const usedSet = Array.from(
  { length: Math.max(...used) + 1 }
)

for (const codePoint of used) {
  usedSet[codePoint] = true
}

// equal to 1 for first snippet
let snippets = eval(answer)
let identifier = ''

for (let codePoint = 0, length = 0x110000; codePoint < length; codePoint++) {
  const character = String.fromCodePoint(codePoint)

  // if unused
  if (usedSet[codePoint] === undefined) {
    // if valid `IdentifierPart`
    try {
      eval(`{let _${character}$}`)
    } catch (error) {
      // console.log(character)
      continue
    }

    // repeat so that `snippet.length === 2`
    identifier += character.repeat(2 / character.length)
    snippets++
  }
}

// number of snippets generated
console.log(`snippets: ${snippets}`)

const program = `console.log(${answer + identifier})`

// output of program to validate with
eval(program)

// download link to check number of bytes used
dl.href = URL.createObjectURL(new Blob([program], { type: 'text/javascript' }))
<a id=dl download=answer.js>Click to Download</a>

Running stat -f%z answer.js yields a byte count of 612802, but we subtract 13 bytes for the console.log( and ) wrapping the actual submission.

Encoding

The source is stored as utf-8, which is reflected in the enormous byte count of the answer. This is done because Node.js can only run source files encoded in utf-8.

JavaScript internally stores strings with utf-16 encoding, so the string "character length" returned in JavaScript is actually just half the number of bytes of the string encoded in utf-16.

\$\endgroup\$
  • \$\begingroup\$ Why not use, say, x instead of $, freeing it up as an extra identifier character? \$\endgroup\$ – Neil Nov 1 '17 at 10:36
  • \$\begingroup\$ @Neil I noticed that a little while ago. I'm currently working on an answer that should be a score of ~119519. Right now I've got it down to just a matter of traversing the encoding properly. \$\endgroup\$ – Patrick Roberts Nov 1 '17 at 10:38
  • \$\begingroup\$ I tried a copy of Spidermonkey JS shell that I happened to have lying around. It only supported 50466 different identifier characters. (Since you use 12 in your initial snippet, that scores you 50455.) \$\endgroup\$ – Neil Nov 1 '17 at 10:48
  • \$\begingroup\$ Well, without doing a major overhaul, it looks like the score will have to be 50297. Writing the answer now. To be clear, there are in fact 128,096 supported identifiers in ES6+ using Unicode 10.0.0 specification, but of those, only the number you mentioned have a string length of 1. Otherwise it's a lot more difficult to get a string character count, and that's what I was hung up on. \$\endgroup\$ – Patrick Roberts Nov 1 '17 at 10:48
  • 1
    \$\begingroup\$ @PatrickRoberts Sorry, my mistake, while reading this I assumed all the text until the ending one was just crossed out text. My eyes must have skipped over that part. \$\endgroup\$ – Conor O'Brien Dec 3 '17 at 17:46
6
\$\begingroup\$

Python 2, score 6 10

+3 thanks to pizzapants184
+1 thanks to WheatWizard

4/4
*2
-~0
+1
|5
^3
&776%6
and 8
if[]else[9][9>9]
.__xor__((""=="").__xor__((""=="")<<(""=="")))

Try it online!

\$\endgroup\$
  • \$\begingroup\$ if you change print int(True) to just 4/4, you can add and 7 for 7, given that you don't have to print the number, just 'produce' it \$\endgroup\$ – pizzapants184 Nov 1 '17 at 0:27
  • \$\begingroup\$ score 9, 47 bytes \$\endgroup\$ – pizzapants184 Nov 1 '17 at 1:09
  • \$\begingroup\$ Adding &2 will increase your score by one because & has higher precedence than ^, but you need to make the number 2 somehow. (You can use [[]==[]][[]<[]]<<[[]==[]][[]<[]]) \$\endgroup\$ – Wheat Wizard Nov 1 '17 at 5:41
  • 1
    \$\begingroup\$ @pizzapants184 Regardless this works for 10. \$\endgroup\$ – Wheat Wizard Nov 1 '17 at 6:05
  • 1
    \$\begingroup\$ @pizzapants184 ord was conflicting with and, but I fixed that \$\endgroup\$ – Rod Nov 1 '17 at 12:28
6
\$\begingroup\$

TI-Basic (83 series, OS version 1.15 or higher), score: 17 18 19 24

(Tiebreaker: 53 bytes.)

You can get a very large score by abusing string length as usual: begin with length("A length("length( (as @Scrooble points out) and keep adding single-token snippets to the end. TI-Basic has over 700 of those, so that actually works pretty well. But here's another approach:

int(log(11
2
3
4
5
6
7
8
9
0
Xmax
Ymax
nMax
abs(Xmin
Ymin
ππ
eee
³
²
₁₀^(₁₀^(X
+e^(⁻e^(Y))))
/√(sub(Z!
°
randrandrand

Note that TI-Basic is tokenized, so (for example) the e^( command does not use any of the characters e, ^, (.

Relies on an undocumented feature of the sub( command: in addition to finding substrings, it can also be used to divide a number by 100.

This works if it's running on a fresh calculator, which lets us assume that X, Y, Z are all zero, that the window variables are set to their standard values, that the calculator is in radian mode, and that rand's first three outputs will be about 0.943, 0.908, 0.146.

\$\endgroup\$
4
\$\begingroup\$

PowerShell, 25 bytes, Score 5

' '.count*2+!""-(-1)|%{5}

Snippet 1: ' '.count outputs 1. It does this by taking the .count of an string, which is one because there is only one string. Try it online!

Snippet 2: *2 outputs 2 because we take the 1 from the previous snippet and multiply it by two Try it online!

Snippet 3: +!"" outputs 3 by adding the Boolean-not of an empty string. This implicitly casts the empty string to $true, which is again implicitly cast to 1, so we're adding one Try it online!

Snippet 4: -(-1) outputs 4 by simply subtracting negative one Try it online!

Snippet 5: |%{5} outputs 5 by taking the previous number into a loop and each iteration of that loop (only one, because there's only one input number) outputs 5 Try it online!

Thanks to Jan for an alternate means of coming up with 4 and a crafty way of coming up with 5.

\$\endgroup\$
  • \$\begingroup\$ How about ' '.count*2+!""-(-1)|%{5} for a score of 5? \$\endgroup\$ – Jan Oct 31 '17 at 23:41
  • \$\begingroup\$ @Jan Oh, right, haha, you don't actually need to use $_ inside |%{ }. Thanks! \$\endgroup\$ – AdmBorkBork Nov 1 '17 at 12:22
4
\$\begingroup\$

C, 10 snippets, 45 bytes

sizeof(char)              // sizeof(char) = 1
<<9/9                     // Multiply by two.
|1                        // 2 or 1 = 3
,4                        // Discard previous expression, return 4.
+!!8                      // Add one.
^3                        // 5 xor 3 = 6
&66                       // 3 and 66 = 2, 5 xor 2 = 7 (& has higher precedence)
??'0xF                    // Trigraph for '^'. 7 xor 15 = 8
-~-2                      // ~-2 = 1, 7 xor 14 = 9
*57%5                     // 1*57%5 = 2, 7 xor 13 = 10

Try it online!

\$\endgroup\$
4
\$\begingroup\$

MATL, Score 8 15, 64 123 bytes

rg             % 1 Random number, make boolean (1)
Q              % 2 Increment
Yq             % 3 Nth prime
a,E]           % 4 any (convert to boolean 1. Do twice: Multiply by 2
T+             % 5 Add True
F~_-           % 6 Subtract negative (not(false)) = 6-(-1)
:sI/           % 7 Range 1:6, sum (21), divide by 3
A8*            % 8 All elements (True). Multiply by 8
d9             % 9 Clear stack, push 9
x10            % 10 Clear stack, push 10
WBPf           % 11 Raise 2^10. Convert to binary [1 0 ... 0], flip [0 0 ... 1]. Find
23ZP           % 12 Push 23, and calculate the distance between 11 and 23
yyyyyyyyyyyyyyyyyyyyyyyyhhhhhhhhhhhhhhhhhhhhhhhhz  % 13. Duplicate and count elements
tttttttttttttvvvvvvvvvvvn  % 14 Duplicate and count elements
OOOOOOOOOOOOOON&           % 15 Add bunch of zeros, output size of stack
  • Got to 12 with help from Luis Mendo! Using 11,23ZP was his idea, along with changing 3 with I for snippet 7.
  • Got to 15 with more help from Luis. OO...N& was his idea.

More to come. I don't know the MATL functions by heart, so I had to go back and forth and back and forth in the documentation... :)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Ooh, 5Yy is nice! I already use Y in 3 to get the second prime :( \$\endgroup\$ – Stewie Griffin Nov 1 '17 at 11:19
  • \$\begingroup\$ I think you can get another one with OOOOOOON&. If you add others, this should probably be the last, because of & \$\endgroup\$ – Luis Mendo Nov 1 '17 at 12:46
  • \$\begingroup\$ Thanks again Luis! Also, why did I need soooo many yyyyyyyy...? I had to double the number, 24 y to get it duplicated 12 times...? \$\endgroup\$ – Stewie Griffin Nov 1 '17 at 13:23
  • \$\begingroup\$ @Stewie I think it is because half the elements you are duplicating are the empty array generated by d. I'm on mobile now, but try X# to debug-print the stack \$\endgroup\$ – Luis Mendo Nov 1 '17 at 15:52
3
\$\begingroup\$

V, score 10

é1
<C-a>
r3
DÉ4
ñóä/5ñ
ddá6
xÁ7
C8<esc>
R9<C-c>
ø.<cr>ÎA0

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Jelly, 253 bytes, score 250

L}“L

Subsequent 1-char snippets:

¡¢£¤¥¦©¬®µ½¿€ÆÇÐÑ×ØŒÞßæçðıȷñ÷øœþ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|~¶°¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾ƁƇƊƑƓƘⱮƝƤƬƲȤɓƈɗƒɠɦƙɱɲƥʠɼʂƭʋȥẠḄḌẸḤỊḲḶṂṆỌṚṢṬỤṾẈỴẒȦḂĊḊĖḞĠḢİĿṀṄȮṖṘṠṪẆẊẎŻạḅḍẹḥịḳḷṃṇọṛṣṭụṿẉỵẓȧḃċḋėḟġḣŀṁṅȯṗṙṡṫẇẋẏż

Note: may also be replaced by \n, they're the same byte in Jelly's codepage.

After you append all those snippets, you can then append the 2-char snippet ”‘.

+1 thanks to Dennis.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Lenguage, 1 112 064 snippets

The nth snippet consists of 23n-1 repetitions of the nth non-surrogate Unicode character, up to and including the 1 112 064th and last non-surrogate character of the current Unicode standard.

Output is in unary (using the null byte as digit) for simplicity. Decimal is possible, but it will make the programs even longer. With unary, we can test the first programs.

For n = 1, we get 4 repetitions of U+0000. This is equivalent to the brainfuck program ., which prints one null byte.

For n = 2, we get 32 repetitions of U+0001, for a total of 36 characters. This is equivalent to the brainfuck program .., which prints two null bytes.

For n = 3, we get 256 repetitions of U+0002, for a total of 292 characters. This is equivalent to the brainfuck program ..., which prints three null bytes.

And so forth, up to n = 1 112 064.

\$\endgroup\$
  • \$\begingroup\$ Can you demonstrate or argue the equivalent in decimal? I'm not sure about the consensus on unary output but I believe it's for languages lacking concept of decimal—at least, last I checked \$\endgroup\$ – Conor O'Brien Nov 1 '17 at 20:59
  • \$\begingroup\$ I believe it's for languages lacking concept of decimal Yes, and since Lenguage doesn't have decimal I/O, I assumed it was fine. Anyway, encoding the brainfuck program '+'*(48 + d) +'.>' for each decimal digit of n would achieve the same score. It's just a lot harder to describe. \$\endgroup\$ – Dennis Nov 1 '17 at 21:12
  • \$\begingroup\$ I see. Same thing just longer. \$\endgroup\$ – Conor O'Brien Nov 1 '17 at 21:16
2
\$\begingroup\$

BASIC (ZX Spectrum), score 244 (new score 247) [is this cheating?]

Snippet 1:

2356 PRINT PEEK (PEEK 23635+256*PEEK 23636+2)+256*PEEK (PEEK 23635+256*PEEK 23636+3)-56-66

Snippet 2: :

Snippet 3: REM

Snippets 4-244: Single-character snippets, using all characters not in snippets 1, 2 and 3.

Explanation

Characters

On Spectrum, PRINT is a single character (code 245). The snippet 1 uses 11 different characters: 2, 3, 5, 6, +, -, *, (, ), PRINT and PEEK What you see as spaces are a part of characters PRINT and PEEK, so the space itself hasn't been used. I decided to give the line number 2356 because these are the only digits present in the code.

Characters 13 and 14 are not allowed. That means there are 243 characters left for snippets 2-244, starting with : and REM to avoid the code that would produce error or do anything else.

How it works

This is why I'm not sure whether this answer is by the book.

23635 is the memory location for 16-bit system variable PROG, stored as LH. (The value is usually 23755. But apart from wrong result if it happens not to be the case, using this number directly, even though it would shorten the code, would cost me extra digit characters.) The value of PROG is the memory location where the program itself is stored. The first two bytes are the line number stored as HL, the following two bytes are the length of the line stored as LH. That's how the program looks up its own line's length, so something appropriate needs to be subtracted

Saving characters

Considering how numbers are stored, the line could have ended with -114 so that snippet 1 would produce 1. But I didn't want to use extra digits, so I instead took away two 2-digit numbers, which then had to add up to 122; 56 and 66 did nicely.

The code is a bit ugly with all nesting and calculating value of PROG (PEEK 23635+256*PEEK 23636) twice. But if I stored it and then used the stored value, it would cost extra characters / snippets - the line could start like

2356 LET p=PEEK 23635+256*PEEK 23636: PRINT PEEK (s+2)...

which would use 4 additional characters: LET, p, = and :.

Maybe I'll engineer this so that all numbers are calculated from numbers that use only 1 digit and gain 3 snippets.

EDIT:

Here is the new snippet 1 (that's how a long single line gets wrap-displayed on the Spectrum, so if you want to test the code , that will help you see that you typed it correctly):

1111 PRINT +PEEK (PEEK (11111+11
111+1111+(1+1+1)*(111-11)+1+1)+(
111+111+11+11+11+1)*PEEK (11111+
11111+1111+(1+1+1)*(111+1-11))+1
+1)+(111+111+11+11+11+1)*PEEK (P
EEK (11111+11111+1111+(1+1+1)*(1
11-11)+1+1)+(111+111+11+11+11+1)
*PEEK (11111+11111+1111+(1+1+1)*
(111+1-11))+1+1+1)-111-111-111-1
11-111-111

I could gain another snippet by avoiding the + and just make do with -. I'm not going to try it, this was enough of an ordeal.

\$\endgroup\$
2
\$\begingroup\$

Klein 011, 9 snippets

Snippet 1

!@1!aaaaaaaaaaaaaaaaaaaaa/a

Try it online!

Snippet 2

2((2|bbb0b2bbbb4bbbbbbbb

Try it online!

Snippet 3


c\*3ccccccccccccccccccccc\ccccccccc3c6cccc9
c\

Try it online!

Snippet 4

ddddddddddddddddddddddd>$d:d:++-$:+:+++$:?:-$-+++

Try it online!

Snippet 5

ee

Try it online!

Snippet 6

fff

Try it online!

Snippet 7

ggggg

Try it online!

Snippet 8

hh

Try it online!

Snippet 9

iiiii

Try it online!

Explanation

This was a really fun challenge for Klein. Klein's unique topology allows for a lot of interesting stuff to be done. As you might notice answers 5-9 are just adding filler (letters do nothing in Klein so I used them as filler) to the code to stretch out the bounding box. This causes the ip to take a different path through earlier parts of the code because of Klein's unique topology.

I'm going to make a full explanation later, but for now here is a easier to understand version of the program with all of the letters replaced with .s.

!@1!...................../.2((2|...0.2....4........
.\*3.....................\.........3.6....9
.\.......................>$.:.:++-$:+:+++$:?:-$-+++.................

Try it online!

\$\endgroup\$
2
\$\begingroup\$

><>, Score: Infinity 1,112,064-6 = 1,112,058

Snippet 1 (6 bytes)

"l4-n;

This snippet outputs the amount of characters after the ; plus one. This can be extended to infinite very large amount of snippets of one character each. A quick google tells me there are 1,112,064 possible Unicode characters, minus the 6 I’ve already used.

Try It Online

\$\endgroup\$
1
\$\begingroup\$

R, score: 79

Credit to Sean's Perl answer for the inspiration; this abuses some quirks of the R interpreter.

First snippet:

nchar(scan(,""))
a

subsequent snippets are the characters in:

bdefgijklmopqtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@$%^&*_+|\[]{}:?><;

Try it online!

The scan function reads data from the file "" which defaults to stdin().

The docs for stdin() indicate that:

When R is reading a script from a file, the file is the ‘console’: this is traditional usage to allow in-line data (see ‘An Introduction to R’ for an example).

Hence, the subsequent data become the file. This can be trivially expanded, and can work with multiple different encodings.

\$\endgroup\$
1
\$\begingroup\$

Pyke, 256 bytes, score 254

Please note that these are hex codes of the actual bytes, separated by spaces, since there the null byte (\x00) is included.

Starting snippet:

6C 22 00

Subsequent 1-char snippets:

01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF

Try it here!

\$\endgroup\$
1
\$\begingroup\$

Java 8, 7 snippets (19 bytes)

1
*2
-~0
>4?4:4
|5
^3
7%7

Just a start, will continue working on it.

Try it here.

Explanation:

Execution precedence is perhaps made clearer when I add parenthesis:

((1*2)-~0)>4?4:4|(5^(37%7))
  • a*b: Multiply a with b
  • ~a: -a-1
  • a>b?x:y: if(a>b){ x }else{ y }
  • a|b: Bitwise-OR a with b
  • a^b: Bitwise-XOR a with b
  • a%b: a modulo-b
\$\endgroup\$
1
\$\begingroup\$

Python 2, 110 snippets

Here is the complete snippet:

print((((len(open(__file__).read())-44.))))

#	 !"$%&'*+,/012356789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^`bcghjkmqsuvwxyz{|}~

Try it online!

The first snippet is

print((((len(open(__file__).read())-44.))))

And then the next 109 snippets are the next 109 bytes.

This is pretty "cheaty" as Lynn puts it. The first snippet opens the file and subtracts 44 from its length, then each other snippet adds 1 to the length of the file without changing the logic of the program as a whole. thus increasing the result of the output by 1.

\$\endgroup\$
1
\$\begingroup\$

dc, score 13, 58 bytes

I3%                   #I is the default input radix, 10, mod 3 yields 1
1+                    #Add one
2^2^2^Z               #2^8=256, Z pushes the number of digits to the stack
6*v                   #Square root of 18 = ~4.24, at precision 0 we get 4
c5                    #Start getting lazy, clear the stack and push 5
_F-E-                 #Subtract negative 15, then subtract 14
ss7                   #Lazy, 'drop' the value by storing it, push 7
SS8                   #Lazy, 'drop' the value by storing it, push 8
d::9                  #Lazy, 'drop' the value by storing it, push 9
;;kA                  #Recall element 9 from nonexistant array ; which is zero, set precision, push A (10)
iB                    #Set input radix to 10, push B (11)
oD                    #Set output radix to 11, push D (13, or 12 in base 11)
 4CCCCCCr/4C/CC/4/    #We have division and the digits 4 and C left, this might not be the optimal way to get us to 13 but it does the job

Try it online! (Note, the TIO version adds an f after each snippet to print the entire stack, showing that each snippet only leaves a single value on the stack; also I forgot the leading space on the last snippet which doesn't matter functionally when they're broken apart by line breaks but does count to my character usage)

Each additional snippet concatenation leaves the desired value and only the desired value on the stack. After hitting 12, I had kind of run out of ways to eat the stack. I tried to use math operations early on since they gobble up the stack and as we get into larger numbers it gets trickier to manage that way. When all is said and done, I have only the digit 0 left to play with and very little in the way of stack-popping, so I think 13 is pretty close to maxed out. I'm sure there are many similar (and likely shorter) ways of accomplishing this in dc, this just kind of fell into place. Note that cygwin handles A-F mixed in with 0-9 differently from most versions of dc, 44C4r/CC 4// works for the final snippet in cygwin.

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

Pyth, 124 snippets

l"x00x01x03x04x05x06x07x08
x0bx0c
x0ex0fx10x11x12x13x14x15x16x17x18x19x1ax1bx1cx1dx1ex1f !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijkmnopqrstuvwxyz{|}~x7f

Try it online!

Unprintable characters where escaped using three printable characters (x..). The first snippet is three bytes long, every snippet after that grows in length by one.

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

Beatnik, 22 bytes, score 20

K A
E
I
L
N
O
R
S
T
U
a
e
i
l
n
o
r
s
t
u

Assuming the word "snippet" allows you to push the value on the stack.

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

Octave, Score 86

nnz n

Followed by:

!$&()*+./0123456789:<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmopqrstuvwxy{|}~

This exploits the fact that Octave treats everything after a function name as a string input. So nnz n returns the number of non-zero elements in the string 'n'. We get to 86 by adding the other printable ASCII-characters. ',"%# doesn't work.

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

APL (Dyalog), score 12

+1 score thanks to @dzaima

First to third snippets:

(~⍴⍬)
+≢'a'
,⍕1

Then each of 234567890.

Try it online!

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

Julia 0.6, 111217

The following script creates the full program:

A = "macro length(a);length(string(a))end;@length a"
B0 = join(([Char(x) for x in Iterators.flatten((0x000001:0x00d7ff, 0x00e000:0x10ffff)) if Base.isvalid(Char(x)) && Char(x) ∉ A && Base.isidentifier("a$(Char(x))") ]));
B = normalize_string(B0, stripmark=true, decompose=true);
B = join(unique(b for b in B))
while (n = search(B, '·')) > 0
    B = B[1:prevind(B,n)]*B[nextind(B,n):end]
end
while (n = search(B, '`')) > 0
    B = B[1:prevind(B,n)]*B[nextind(B,n):end]
end

open("concount.jl", "w") do f
    write(f, A)
    write(f, B)
end

Explanation

The macro

 macro length(a);length(string(a))
 end
 @length a

followed by all distinct unicode characters allowed in identifiers, computed with isidentifier. Some diacritial marks mess up counting so I removed them.

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

TeX, score 61 (possibly 190)

First snippet:

\def\len#1{\expandafter\glen\string#1=}
\def\glen#1{\tlen}
\def\tlen#1{\if#1=\let\tlen\end\number\count1\else\advance\count1by1\fi\tlen}
\count1=-1
\catcode33=11
\catcode34=11
\catcode36=11
\catcode37=11
\catcode'46=11
\catcode40=11
\catcode41=11
\catcode42=11
\catcode43=11
\catcode44=11
\catcode45=11
\catcode46=11
\catcode47=11
\catcode56=11
\catcode57=11
\catcode'72=11
\catcode'73=11
\catcode60=11
\catcode62=11
\catcode63=11
\catcode64=11
\catcode'133=11
\catcode'135=11
\catcode'136=11
\catcode'137=11
\catcode'140=11
\catcode124=11
\catcode126=11
\len\t

Other 60 snippets: in any order, each containing one character out of

!"$%&()*+,-./:;<>?@[]^_`|~89ABCDEFGHIJKLMNOPQRSTUVWXYZhjkqwz

Explanation: \len, which is invoked at the end, converts a control sequence into a string, each character (including \) is one character token of that string; then \glen gobbles token \, then \tlen counts remaining tokens. It therefore outputs the length of that control sequence (not including \).

Characters from other snippets extend the control sequence which is initially just \t. A multi-character control sequence has to be made up of letters only, but they all count as letters because they have all been assigned category code 11 (using ASCII codes so that the characters themselves don't appear in the first snippet).

I decided to use octal codes for some characters which cost me one symbol, ' but saved me two digits, 8 and 9, so I gained one snippet. Possibly could have gained a couple more if I didn't need \expandafter.

Output

Possible alteration: wrap \number\count1 into \message{} so the output doesn't go into .dvi output but instead to console output and .log. It doesn't cost any extra letters.

190

Unfortunately TeX works with ASCII and not Unicode (or does it now?), but my solution could possibly be extended to include 129 more single-character snippets containing the characters with codes 127-255. Maybe even some characters before 32. Character 32 (space) didn't work, otherwise I'd put it into a snippet as well - didn't need it in the first snippet. Of course, each of these additional characters would have to be \catcoded into a letter in the first snippet.

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

Pip, 57 bytes, score = 16

!u
2
RT9
#m
5
(EX3Y8)
7
y
A'<tab>
t
11
+o
-v
PI@`\...`@`..$`
"F"FB:4*4
Ok=k6

Note that <tab> represents a literal tab character (ASCII 9). Try it online!

Each snippet is a full program that outputs the desired number. Most of them work on the principle that the last expression in a Pip program is autoprinted: for example, in snippet 5, the previous code is evaluated, but the only part that matters is the 5. Exceptions to this are:

  • Snippet 3 works fine in isolation as RT9 (square root of 9), but coming after snippet 2 it actually parses as 2RT9 (2nd root of 9)--which, of course, amounts to the same thing.
  • Snippet 6 gets the 8th character (0-indexed) of e^3, which happens to be 6. But it also yanks the number 8 into the y variable. Snippet 8 then outputs the value of y.
  • Snippet 12 takes the 11 from the previous snippet and adds o (preinitialized to 1). Snippet 13 takes that result and subtracts v (preinitialized to -1).
  • Snippet 16 outputs 1 without a newline (Ok=k, where k=k evaluates to the default truthy value of 1), and then autoprints 6.

Other interesting stuff:

  • Snippet 14 uses regex operations to extract 14 from the value of pi.
  • Snippet 15 converts the string "F" from hexadecimal.
\$\endgroup\$
0
\$\begingroup\$

Zsh, score >50 000 (1 112 046?), 16 + Σ(UTF-8 codepoint lengths) bytes

exec echo   $((${#:-$(<$0)}-33))
#

Zsh handles multibyte encodings by default. The base snippet has 18 unique characters, 34 total. Every other codepoint (save for the null byte) can be added onto this. As I write this, my script testing these is on codepoint ~50 000, total file size 150K, going at about 30 snippets/second. Run my full test script yourself here. You may want to adjust it to start at some later codepoints.

Try the first 2000 snippets online!

More snippets are possible by using this technique.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.