4
\$\begingroup\$

Your challenge is to implement a cyclic Program Sequence Generator (PSG) in a language of your choice.

Given a language, L, a PSG in L is a program in L that outputs a PSG in L. Some properties of a PSG are:

  • Repeated execution of a PSG in L generates a sequence of programs in L.
  • A PSG is said to be cyclic if the sequence it generates is cyclic.
  • The period of a cyclic PSG, P, is written T(P) and is the period of the cycle in the sequence it generates. The period of a non-cyclic PSG is written the same way and is infinite.

A trivial example of a PSG is a quine; any quine is a cyclic PSG with period 1.

To make this challenge interesting, the period of your PSG must be at least 5.

Scoring is a bit different than typical code golf. Informally, your score is the sum of the length of all programs in one cycle of the sequence generated by your PSG divided by the period of your PSG. Formally: let P be your PSG, pₙ be the nth term in your PSG's sequence, and l(pₙ) be the length in bytes of pₙ. Then your score is:

score formula

For a non-cyclic PSG, since the limit of l(pₙ) as n goes to infinity is infinity, the score of any non-cyclic PSGs diverges to infinity.

The PSG with the lowest score wins

\$\endgroup\$
  • \$\begingroup\$ Oh, you never said, are we trying to get a HIGH score or a LOW score? I'm assuming high. \$\endgroup\$ – Magic Octopus Urn Feb 2 '18 at 19:57
  • \$\begingroup\$ Golfers beware: there is an error in my formal score formula because it assumes a sequence cycles back to , when in reality it could only start being cyclic for some larger value of n. Unfortunately I don't have time to fix it immediately, but use the intuition. from the informal definition for now. \$\endgroup\$ – Vaelus Feb 2 '18 at 19:59
  • \$\begingroup\$ The correct formula would have m go to infinity instead of T(P) \$\endgroup\$ – Vaelus Feb 2 '18 at 20:14
  • \$\begingroup\$ @Vaelus the limit is the same in either case, one is just easier to compute. \$\endgroup\$ – kamoroso94 Feb 2 '18 at 22:56
  • \$\begingroup\$ It's the same for non-cyclic PSGs but not, for instance, for a program that outputs a quine but isn't one itself. \$\endgroup\$ – Vaelus Feb 2 '18 at 23:03
4
\$\begingroup\$

JavaScript (ES6), score 50 49 48 47 37

Saved 10 points thanks to @alephalpha

(f=x=>alert(`(f=${f})(${++x%5})`))(0)

Period is 5, each program is 37 bytes long. The 0 is changed to 1, 2, 3, 4, then back to 0.

\$\endgroup\$
  • \$\begingroup\$ (f=x=>alert(`(f=${f})(${++x%5})`))(0) \$\endgroup\$ – alephalpha Mar 9 '18 at 17:52
  • \$\begingroup\$ @alephalpha D'oh, I should've known there was a better alternative to .replace()... Thanks! \$\endgroup\$ – ETHproductions Mar 13 '18 at 1:24
4
\$\begingroup\$

JavaScript (ES6), score 77 74 66

The period of this PSG is 5. Each iteration of the sequence removes one asterisk in the block comment until it reaches 2, then goes back to 6.

f=_=>/**/`f=${f}`.replace('**/','*/').replace('/*\/','/******/')

Let's look at some partial sums to see what the limit approaches.

m     1     2     3     4     5
score 64    66    66.33 66.25 66

Edit: thanks to @ETHproductions for pointing out that I should put the comment first. Not only is the code much shorter, it appears neater!

\$\endgroup\$
  • \$\begingroup\$ I like the clever use of otherwise unnecessary string escapes. \$\endgroup\$ – Vaelus Feb 3 '18 at 0:31
  • 1
    \$\begingroup\$ Would the backslashes be necessary if you moved the comment to the front? \$\endgroup\$ – ETHproductions Feb 3 '18 at 0:34
  • \$\begingroup\$ The length of your PSG seems to be 75, meaning the score comes out to 77. Am I missing something? \$\endgroup\$ – Vaelus Feb 3 '18 at 1:09
  • \$\begingroup\$ @Vaelus Sorry, when I looked at the length of the code (I didn't count manually), it was ignoring the `\`s. I counted them correctly this time though. \$\endgroup\$ – kamoroso94 Feb 3 '18 at 1:18
  • 1
    \$\begingroup\$ I believe you can actually remove the braces and the return and it will still work :-) \$\endgroup\$ – ETHproductions Feb 3 '18 at 3:26
2
\$\begingroup\$

Python 2, score 119 89

s='s=%r;c=0;s=s[:7]+`c+1-c/4*5`+s[8:];print s%%s';c=0;s=s[:7]+`c+1-c/4*5`+s[8:];print s%s

Try it online!

The period of my PSG is 5. Each program is 89 bytes in length. Therefore, my score is 89.

-30 bytes thanks to ovs.

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

Pari/GP, score 38

(f=(x)->print1("(f="f")("x++%5")"))(0)

Period is 5.

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ I didn’t notice the trailing newlines. Interestingly, your first version actually had a score of 38 too, since it settles into a cycle where each program has a trailing newline. \$\endgroup\$ – Vaelus Mar 10 '18 at 22:00

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.