9
\$\begingroup\$

Write a program with the following properties:

  • it must be a quine, i.e. running it outputs precisely the program itself;

  • it must take no input;

  • for at least one integer k, it must be the case that deleting the kth character of the program results in a new program that takes no input and outputs precisely the integer k.

Scoring

  • Let n be the length of the program in characters.

  • Let r be the number of integers k for which deleting the kth character gives output k (i.e. the number of "right answers").

  • Let w = n - r (i.e. the number of "wrong answers").

Then your score is

w + n/r.

Your goal is to make the score as low as possible.

Further rules

The quine (without deletions) should not error.

Integers may be output in any reasonable consistent form, and may optionally be followed by one newline, but to count as a "right answer", nothing else should be output and the program should not error. For other choices of deleted character ("wrong answers"), the behavior of the program can be arbitrary.

Deleting a character means removing it and concatenating the remaining strings. So deleting the 3rd character of codegolf gives coegolf.

Optionally, "kth character" can be interpreted as 0-based, so that the possible "right" outputs are 0,1,...,n-1 (as opposed to the default of 1,2,...,n). However, the same convention must be used consistently for all k when computing the score.

Besides the usual information, the entry title should contain the score and should say if it is 0-based. Ideally your description should also list the "right" ks.

Lowest score in each language wins!

\$\endgroup\$
3
  • \$\begingroup\$ @Ted I meant n-r. Corrected now. Many thanks! \$\endgroup\$ Commented Aug 15 at 2:27
  • \$\begingroup\$ I find the definition of w ambiguous. I expect you mean w to be (# of "wrong answers"), which is n - r, however as written it looks like w = n - r x (# of "wrong answers")... Also, it would be worth adding that k is in 1..n immediately. It's specified in the "further rules", but not at the point where k is defined. It's obvious in retrospect -- otherwise w is infinite -- but... still? \$\endgroup\$ Commented Aug 15 at 13:06
  • \$\begingroup\$ @MatthieuM. I've addressed the first issue I hope. For the second one I don't see the problem. "Deleting the kth character" for k>n could only really be interpreted as making no change, in which case the program would still need to be a quine. w cannot be infinite because it is the difference of two finite numbers. \$\endgroup\$ Commented Aug 15 at 18:41

2 Answers 2

6
\$\begingroup\$

PowerShell, 2 bytes, Score 1, 1-based

21

Try it online!

"Right" ks: 1, 2 ('all of them')
n = 2, r = 2, w = 0
Score = 0 + 2/2 = 1

A valid PowerShell can consist of just a string or integer value, which will just be sent to the output.


For the sake of completeness:

PowerShell, 2 bytes, Score 1, 0-based

10

Try it online!

"Right" ks: 0, 1
Same idea as above.

\$\endgroup\$
2
  • 5
    \$\begingroup\$ Note that both versions are 'improper' quines, which are disallowed by default. \$\endgroup\$ Commented Aug 15 at 14:23
  • \$\begingroup\$ I think I agree that this should be disallowed as an entry as per @Dingus comment. But it is still good to have it here to get the ball rolling. I suspect it might not be too difficult to get R=2 by basing a program around this... \$\endgroup\$ Commented Aug 15 at 23:20
2
\$\begingroup\$

Haskell, 166.51 score, 0-based

n = 488, r = 323, w = 165

f=let c=toEnum 34;d=succ c;v i x y l|l==[]=y++[c]++x++[c]|l!!0==d&&l!!1==d=show(i+164)|l!!0==d=v(i+2)x(y++[l!!1])$drop 2 l|otherwise=show(i+163);s x=v 0 x[]x in s"#f#=#l#e#t# #c#=#t#o#E#n#u#m# #3#4#;#d#=#s#u#c#c# #c#;#v# #i# #x# #y# #l#|#l#=#=#[#]#=#y#+#+#[#c#]#+#+#x#+#+#[#c#]#|#l#!#!#0#=#=#d#&#&#l#!#!#1#=#=#d#=#s#h#o#w#(#i#+#1#6#4#)#|#l#!#!#0#=#=#d#=#v#(#i#+#2#)#x#(#y#+#+#[#l#!#!#1#]#)#$#d#r#o#p# #2# #l#|#o#t#h#e#r#w#i#s#e#=#s#h#o#w#(#i#+#1#6#3#)#;#s# #x#=#v# #0# #x#[#]#x# #i#n# #s"

Is an f :: String which is its own source code normally, or a base-10 integer if the (0-indexed) 163th to 485th character is removed (i.e. any part of the string literal except the last character).

Explanation

v is a recursive function that detects if there are two #s consecutively or if there is two non-#s consecutively. # is interleaved in the string literal containing the rest of the program.

Annotated source:

f = let c = toEnum 34 -- "
        d = succ c    -- #
        v i x y l | l == [] = y ++ [c] ++ x ++ [c]
                  | l !! 0 == d && l !! 1 == d = show (i + 164)
                  | l !! 0 == d = v (i + 2) x (y ++ [l !! 1]) $ drop 2 l
                  | otherwise = show (i + 163)
        s x = v 0 x [] x
    in s "..."
\$\endgroup\$
1
  • \$\begingroup\$ Impressive solution! \$\endgroup\$ Commented Oct 28 at 14:11

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.