16
\$\begingroup\$

We all know what a quine is. A quine is a program which prints its own source code. And we also know about the printable ASCII characters. Well... this challenge mixes both.

Your mission, should you accept it, is to create an index quine. An index quine is a non-empty program, composed only of the printable ASCII characters, which prints its own index.

The index of a string is its position within an infinite dictionary where all strings composed of exactly the printable ASCII characters appear, sorted by size then ASCII code.

The index is a non-negative whole number, assigning 0 to the empty string then continuing with the space, !, " onward.

For example, *! precedes ** as the ending ! in the first string has lower ASCII code than the ending * in the second, but both are preceded by {, which has a higher ASCII code than the characters of the last two examples, but is shorter.

The printed index should be in base 10, outputted through STDOUT or closest equivalent, with no other output unless your language requires trailing newlines/other characters. Also, a bit like actual quines, do not access your source code directly (as a file, etc.)

Your program should be non-empty, as a reminder to those reading these rules.

If there are multiple programs of shortest length, the tiebreaker is the program with the lower index.

The shortest program following these very rules wins. Have fun.

\$\endgroup\$
15
  • 2
    \$\begingroup\$ I think you mean sorted by size then ascii code, or else some strings don't have finite index. (Actually, maybe I'm just interpreting "then" differently) \$\endgroup\$
    – xnor
    May 31, 2020 at 19:37
  • 1
    \$\begingroup\$ Yeah, I can see it both ways now. I was thinking that to sort, you first compare size, and if those are unequal, then you look at ascii value. \$\endgroup\$
    – xnor
    May 31, 2020 at 19:44
  • 7
    \$\begingroup\$ @Arnauld Here is the new program to get the index. \$\endgroup\$ May 31, 2020 at 20:26
  • 1
    \$\begingroup\$ @Dingus I think the question's definition of index quine is meant to be entirely separate from that of a standard quine. \$\endgroup\$ Jun 1, 2020 at 0:48
  • 3
    \$\begingroup\$ "Quine" on this site has some complications. (TL;DR: a proper quine should have "data" and "code" sections clearly visible.) If you don't want that, you can use self-referential instead. \$\endgroup\$
    – Bubbler
    Jun 1, 2020 at 8:16

12 Answers 12

11
\$\begingroup\$

Deorst, 1 byte

 

Try it online!

Commentator, 1 byte

 

Try it online!

Snails, 1 byte

 

Try it online!


So far, I have found three one-byte solutions, all of which print 1 with a space in the source code.

The first two languages were created by @cairdcoinheringaahing, and the last one by @feersum.

\$\endgroup\$
5
\$\begingroup\$

J, 52 bytes

echo-95x#.31-3 u:(,quote)'echo-95x#.31-3 u:(,quote)'

Try it online!

How it works

echo-95x#.31-3 u:(,quote)'echo-95x#.31-3 u:(,quote)'
                         '...'  NB. a string literal
                 (,quote)       NB. concatenate self with single-quoted self
             3 u:  NB. convert to Unicode codepoints
          31-      NB. subtract from 31 (negation of 1-based index)
     95x#.         NB. convert from base 95 to integer (using extended integer)
    -              NB. negate
echo               NB. print with newline
\$\endgroup\$
4
\$\begingroup\$

Pyth, 39 37 33 bytes

-2 bytes thanks to @Mukundan

hiR95]m-Cd31jN B"hiR95]m-Cd31jN B

Try it online!

hiR95]m-Cd31jN B"...
                "...   ( String literal
               B       ( Wrap in an array and duplicate
            jN         ( Join using `"` (this gives us the full source code)
      m-Cd31           ( For each character, take its codepoint minus 31
     ]                 ( Wrap in an array
 iR95                  ( Map base 95-conversion to that array
h                      ( Take the first (only) element
\$\endgroup\$
2
  • \$\begingroup\$ -2 byes by using ` B` instead of *2[. Try it online! \$\endgroup\$ Jun 1, 2020 at 2:48
  • \$\begingroup\$ @Mukundan Thanks :) \$\endgroup\$ Jun 1, 2020 at 2:51
4
\$\begingroup\$

Python 3.8 (pre-release), 82 66 63 bytes

-3 bytes thanks to @dingledooper

exec(a:='g=0\nfor i in b"exec(a:=%r)"%a:g=g*95+i-31\nprint(g)')

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ -3 bytes using a b-string. \$\endgroup\$ Jun 1, 2020 at 6:34
  • \$\begingroup\$ @dingledooper Thanks \$\endgroup\$ Jun 1, 2020 at 6:38
3
\$\begingroup\$

C# (.NET Core), 471 bytes

using System.Numerics;class A{static void Main(){var s="using System.Numerics;Class A{{static void Main(){{var s={0}{1}{0};System.Console.Write(V(string.Format(s,(char)34,s)));}}static BigInteger V(string s){{BigInteger r=0;for(int i=0;i<s.Length;++i)r+=(s[i]-31)*BigInteger.Pow(95,i);return r;}}}}";System.Console.Write(V(string.Format(s,(char)34,s)));}static BigInteger V(string s){BigInteger r=0;for(int i=0;i<s.Length;++i)r+=(s[i]-31)*BigInteger.Pow(95,i);return r;}}

Try it online!

This is the basic C# quine with the scoring function built in. Is my scoring function a little off? It doesn't quite agree with the Python one linked in the comments.

\$\endgroup\$
3
\$\begingroup\$

Gol><>, 15 bytes

"r3lMF`_*+fs-|h

Try it online!

An extension from my own 6-byte quine.

How it works

"r3lMF`_*+fs-|h
"...             Start string ("), push all characters, and end string(")
 r3              Reverse the stack and push 3 (= 34 - 31)
   lMF       |   Loop (stack length - 1) times...
      `_*+         [... n2 n1] -> [... n2+n1*95]
          fs-      Subtract 31
                 equivalent to "-31 from each except top and base 95 convert"
              h  Print the top number and halt
\$\endgroup\$
3
\$\begingroup\$

Python 2, 68 65 bytes

-3 bytes thanks to @Mukundan

x="g=0\nfor i in'x=%r;exec x'%x:g=95*g+ord(i)-31\nprint g";exec x

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ -3 bytes by using a for loop instead of the reduce \$\endgroup\$ Jun 1, 2020 at 6:21
  • \$\begingroup\$ @Mukundan That’s great, thanks! \$\endgroup\$ Jun 1, 2020 at 6:30
1
\$\begingroup\$

Java (OpenJDK 8), 724 bytes

import java.math.*;class D{public static void main(String[]a0){char q=34;String s="import java.math.*;class D{public static void main(String[]a0){char q=34;String s=;String u=s.substring(0,82)+q+s+q+s.substring(82);BigInteger n=new BigInteger(s.substring(60,61));for(int i=0;i<u.length();)n=n.add(new BigInteger(new Long(95).toString()).pow(i).multiply(new BigInteger(new Long(u.charAt(u.length()-i++-1)-31).toString())));System.out.print(n);}}";String u=s.substring(0,82)+q+s+q+s.substring(82);BigInteger n=new BigInteger(s.substring(60,61));for(int i=0;i<u.length();)n=n.add(new BigInteger(new Long(95).toString()).pow(i).multiply(new BigInteger(new Long(u.charAt(u.length()-i++-1)-31).toString())));System.out.print(n);}}

Try it online!

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

R + GMP, 156 116 bytes

or 126 bytes in R

"->a;t=utf8ToInt(a)-31;sum(as.bigz(95)^(115:0)*c(3,t,3,t))"->a;t=utf8ToInt(a)-31;sum(as.bigz(95)^(115:0)*c(3,t,3,t))

Try it online!

Edit: Big improvement by re-arranging to start immedidately with quoted string, which really simplifies the re-construction of the full program text.

This seems horribly uncompetitive, but I don't seem to be able to do any better, I'm afraid. The enormously-verbose utf8ToInt() function name isn't really helping much.

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

Stax, 34 bytes

1-byte difference with Pyth, since I can't get rid of the prepending quote in Stax.

"c34|Ss+{31-m95|E"c34|Ss+{31-m95|E

Try it online!

Explanation

"..."                 The second half of the program
     c                Copy this operand
      34|S            Surround it with quotes
          s+          Prepend the quoted string with the original string
            {   m     Map: (Since direct - removes items)
             31-      Codepoint - 31 (Stax strings consist of codepoints)
                 95|E Convert from base 95
\$\endgroup\$
0
\$\begingroup\$

Stax, 12 bytes

0|?F31-s94*+

Run and debug it

Note that this uses the |? program source operand, which may be cheating

0   # Push a zero to the stack
|?  # Return program's source as a string
F   # for each character (as an ascii value)...
31- # subtract 31 from the character's value
s   # get the running total
94* # multiply it by 94
+   # add the character's value
\$\endgroup\$
0
\$\begingroup\$

Perl 5 + -Mbigint -M5.10.0, 71 bytes

$_=q($n+=(95**(70-$-++))*(-31+ord)for"\$_=q($_);eval"=~/./g;say$n);eval

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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