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.
-
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– xnor2020年05月31日 19:37:43 +00:00Commented 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– xnor2020年05月31日 19:44:26 +00:00Commented May 31, 2020 at 19:44
-
7\$\begingroup\$ @Arnauld Here is the new program to get the index. \$\endgroup\$dingledooper– dingledooper2020年05月31日 20:26:26 +00:00Commented 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\$math junkie– math junkie2020年06月01日 00:48:45 +00:00Commented 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– Bubbler2020年06月01日 08:16:29 +00:00Commented Jun 1, 2020 at 8:16
12 Answers 12
Deorst, 1 byte
Commentator, 1 byte
Snails, 1 byte
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.
J, 52 bytes
echo-95x#.31-3 u:(,quote)'echo-95x#.31-3 u:(,quote)'
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
Pyth, (削除) 39 (削除ここまで) (削除) 37 (削除ここまで) 33 bytes
-2 bytes thanks to @Mukundan
hiR95]m-Cd31jN B"hiR95]m-Cd31jN B
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
-
\$\begingroup\$ -2 byes by using ` B` instead of
*2[. Try it online! \$\endgroup\$Mukundan314– Mukundan3142020年06月01日 02:48:39 +00:00Commented Jun 1, 2020 at 2:48 -
\$\begingroup\$ @Mukundan Thanks :) \$\endgroup\$math junkie– math junkie2020年06月01日 02:51:55 +00:00Commented Jun 1, 2020 at 2:51
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)')
-
\$\begingroup\$ -3 bytes using a b-string. \$\endgroup\$dingledooper– dingledooper2020年06月01日 06:34:02 +00:00Commented Jun 1, 2020 at 6:34
-
\$\begingroup\$ @dingledooper Thanks \$\endgroup\$Mukundan314– Mukundan3142020年06月01日 06:38:15 +00:00Commented Jun 1, 2020 at 6:38
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;}}
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.
Gol><>, 15 bytes
"r3lMF`_*+fs-|h
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
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
-
\$\begingroup\$ -3 bytes by using a for loop instead of the reduce \$\endgroup\$Mukundan314– Mukundan3142020年06月01日 06:21:41 +00:00Commented Jun 1, 2020 at 6:21
-
\$\begingroup\$ @Mukundan That’s great, thanks! \$\endgroup\$dingledooper– dingledooper2020年06月01日 06:30:59 +00:00Commented Jun 1, 2020 at 6:30
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);}}
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))
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.
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
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
Stax, 12 bytes
0|?F31-s94*+
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
Perl 5 + -Mbigint -M5.10.0, 71 bytes
$_=q($n+=(95**(70-$-++))*(-31+ord)for"\$_=q($_);eval"=~/./g;say$n);eval
Explore related questions
See similar questions with these tags.