10
\$\begingroup\$

Write two code fragments, which we will call s_zero and s_one.

Program (l, n) consists of l copies of s_zero and s_one corresponding with the digits of n in binary, padded with s_zero on the left.

For example, if s_zero = foo and s_one = bar then
Program (4, 0) = foofoofoofoo
Program (4, 1) = foofoofoobar
Program (4, 2) = foofoobarfoo
etc.

Program (l, n) must print the source of Program (l, (n + 1) mod (2^l)) to standard out. In the example above, foobarfoofoo must print foobarfoobar when executed.

Your score is the sum of the lengths of fragments s_zero and s_one

\$\endgroup\$
  • \$\begingroup\$ Near duplicate to codegolf.stackexchange.com/questions/35974/… - computation done by quining program with 2 different code blocks. \$\endgroup\$ – feersum Jan 26 '15 at 3:53
  • \$\begingroup\$ Can the program read its own source code? \$\endgroup\$ – Doorknob Jan 26 '15 at 3:53
  • 2
    \$\begingroup\$ @feersum I disagree. This one is a lot easier, in particular, you don't need to be able to determine line breaks. Furthermore, I think the task does make a difference, otherwise every generalised quine challenge would be a dupe of the basic quine challenge. \$\endgroup\$ – Martin Ender Jan 26 '15 at 7:29
  • \$\begingroup\$ The advantage of the simpler task is that it encourages competition to create the shockingly short answers seen so far- I hope that justifies this challenge as different! \$\endgroup\$ – QuadmasterXLII Jan 26 '15 at 14:48
  • 1
    \$\begingroup\$ Probably worth noting that s_zero and s_one have to be different. Otherwise I have many solutions with 2*n scores. \$\endgroup\$ – randomra Feb 15 '15 at 11:51
6
\$\begingroup\$

CJam, 29 + 29 = 58 bytes

The 0 code:

0{"_~"]]z(3\+2b(2b2>a\+z~[}_~

The 1 code:

1{"_~"]]z(3\+2b(2b2>a\+z~[}_~

Explanation

0                       " Push the current bit. ";
{"_~"                   " The basic quine structure. ";
    ]                   " Make an array of the current bit, the function and _~.
                          That is the code fragment itself. ";
    ]                   " Make an array of all code fragments in the stack. ";
    z(                  " Extract the array of the bits. ";
    3\+2b(2b2>          " Convert from base 2, decrease by one and convert back,
                          keeping the length. ";
    a\+z                " Put the bits back to the original position. ";
    ~                   " Dump the array of code fragments back into the stack. ";
    [                   " Mark the beginning position of the array of the next code fragment. ";
}_~
\$\endgroup\$
  • 2
    \$\begingroup\$ every single time ... \$\endgroup\$ – Optimizer Jan 26 '15 at 10:47
3
\$\begingroup\$

CJam, 47 + 47 = 94 bytes

The 0 code:

{`"_~"+]:+T2*0+:T)\AsSerS/_,(@2Y$#+2bU@->]zs}_~

The 1 code:

{`"_~"+]:+T2*1+:T)\AsSerS/_,(@2Y$#+2bU@->]zs}_~

Excuse the expletive.

I'm sure I can still shave off a few bytes there. I'll add an explanation once I decide that I can't be bothered to golf it any further.

Test it here.

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

CJam, 45 + 45 = 90 bytes

The 0 code:

{`]W=L0+:L,Ua*L2b)2b+L,W*>\f{X$!s/\s*"_~"}}_~

The 1 code:

{`]W=L1+:L,Ua*L2b)2b+L,W*>\f{X$!s/\s*"_~"}}_~

Explanation soon.

Try it online here

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

GolfScript, 37 + 37 = 74 bytes

0{`".~"+:q;]-2%1\{1$^.@&q@}%\;-1%~}.~
1{`".~"+:q;]-2%1\{1$^.@&q@}%\;-1%~}.~

Not quite as short as user23013's CJam solution, but I figured I'd post this anyway, if only to (marginally) increase the diversity of the languages used.

My solution not directly based on any of the existing solutions (and, indeed, I haven't examined them in detail, as I still don't read CJam very well), but they all feature variants of the same basic quine structure ({".~"}.~ in GolfScript, {"_~"}_~ in CJam). That's not really very surprising, since it seems to be one of the most efficient ways to write a quine with an arbitrary payload in these languages.

There are several parts of this code I don't really like, and I suspect it may be possible to golf this further, but I've spent too much time on this as it is.

\$\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.