26
\$\begingroup\$

Challenge

In this challenge, you will be writing the first program, p1, of an infinite sequence of programs, in which running pn outputs/generates program pn+1. When concatenating the first n >= 2 programs, the sequence should output n.

Example

Let's say the first 4 programs are:

p1 p2 p3 p4

If I were to run p1, it should output:

p2

If I were to run p1p2, it should output:

2

If I were to run p1p2p3p4 it should output:

4

If I were to run p4, it should generate the next program in the sequence:

p5

Scoring

Your score is the byte count of the first 10 programs.

\$\endgroup\$
8
  • \$\begingroup\$ Will the sequences p1p2p3... always be from from p1 to pn? \$\endgroup\$
    – Moose
    Dec 22, 2015 at 1:56
  • \$\begingroup\$ @Moose Yes, it will always be from p1 to pn. \$\endgroup\$
    – Downgoat
    Dec 22, 2015 at 1:57
  • 5
    \$\begingroup\$ This is an interesting problem. The program sequence is easy enough; the chaining is harder. \$\endgroup\$ Dec 22, 2015 at 1:57
  • \$\begingroup\$ Is accessing files allowed? \$\endgroup\$
    – Lynn
    Dec 22, 2015 at 2:56
  • \$\begingroup\$ @Mauris Yes but the file's content's & name's byte count must be counted in the total byte count for each program it is used in. \$\endgroup\$
    – Downgoat
    Dec 22, 2015 at 2:57

10 Answers 10

50
\$\begingroup\$

Pyth, 12

p1:

l"1

p2: 1

p3: 1

etc..

p1p2p3:

l"111 

Output: 3

Explanation:

l        length
 "1      string "1"

On first run, this outputs the length of a single character string, 1. This also happens to be a valid Pyth program, outputting 1 again. Therefore, pn+1 is always 1. When the programs are chained, p1 outputs the length of the chained programs, which will be n.

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

Lua, 950 900 bytes

s=io.open(arg[0]):read()if#s<95 then print(s)do return end end print(#s/90) do return end;

Ungolfed:

s=io.open(arg[0]):read'*a'
if #s < 96 then 
    print(s)
    do return end 
end 
print(#s/90) 
do return end;

Explanation:

The first line grabs the entire source of the program. Then we compare the length of the entire program to 1 + the length of one single program. If the size of the current program is smaller than this value, than the source is printed, which is the next program, p2, and we exit. Each iteration is just a quine. When several of these are put together, the the conditional fails, and we print the length of the concatenated program divided by the length of one program, which is the number of concatenated programs, n.

\$\endgroup\$
3
  • \$\begingroup\$ +1 for using a different method (than mine). This is the type of creative answer I was hoping for. \$\endgroup\$
    – Moose
    Dec 23, 2015 at 3:19
  • \$\begingroup\$ +1 for Lua, and choosing a cooler, if more lengthy method than the other answers :P \$\endgroup\$
    – cat
    Dec 23, 2015 at 14:05
  • \$\begingroup\$ Haha thank you, I was quite proud that i managed to do this with a non-golfing and fairly verbose language :) \$\endgroup\$
    – Nikolai97
    Dec 23, 2015 at 17:18
4
\$\begingroup\$

Vitsy, 19 bytes

Not dealing with strings here, but using method tricks.

p1

1ml1-\+N
1

p2

1

p3

1

So on, so forth.

Explanation below:

1ml1-\+N
1m       Execute the first index of lines (the bit with the ones)
  l1-    Get the length minus 1.
     \+  Add them all up.
       N Output as number.

1        Push one to the stack.

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Vitsy, 14 bytes

Similar to the Pyth and Jolf answers, I'm mapping strings. The only difference is that I use the line wrapping features to make sure I always get the right length.

p1

'l3-N

p2

1

Replace 1 with any single number.

p3 and so on match this pattern, and you can do this until Integer.MAX_VALUE, the integer restriction of the language.

Explanation:

'l3-N
'     Wrap around the line until finding another '. Since no ' is found before the
      End of the line, it wraps around.
 l    Get the length of the stack.
  3-  Subtract three.
    N Output as number.

Try it online!

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

Seriously, 15 bytes

First program, 6 bytes (contains an unprintable):

5Ql-.

Hex Dump:

35516c2d2e7f

This program prints 1: Try it online

The rest of the programs are all 1, a valid program that prints itself just like the Pyth answer. The original program prints the length of its source code minus 5 and terminates immediately. 1s appended to the end increase the length of the source code by 1 byte each time, but never get executed.

\$\endgroup\$
2
\$\begingroup\$

Jolf, 14 bytes

Try it here!

a-lq4
a      print
  lq   the length of the source code
 -  4  minus 4

When executed, this prints 1. Thus, p2 = 1. Executing p2 yields 1. So, for all N > 1, pN = 1.

Observe p1p2: a-1q41. This transpiles to:

alert(sub(length("a-lq41"),4));
1;

Since implicit printing is disabled after the first print, this prints 2, as the length of the source code minus 4 is 2. And on and on and on.

\$\endgroup\$
2
\$\begingroup\$

Ruby, 318 bytes

p1:

x=DATA.readlines.size
_="_=%p;puts _%%_"
puts x>0?x+1:_%_
__END__

Each separate program pi outputs this one-line quine: _="_=%p;puts _%%_";puts _%_.

When you add these quines to the end of p1, they end up as lines in the DATA object because they are below the magic __END__.

Here's a test:

$ ruby chain.rb                                    # running p1
_="_=%p;puts _%%_";puts _%_

$ ruby -e '_="_=%p;puts _%%_";puts _%_'            # running p2
_="_=%p;puts _%%_";puts _%_

$ ruby -e '_="_=%p;puts _%%_";puts _%_'            # running p3
_="_=%p;puts _%%_";puts _%_

$ # Concatenating p2 and p3 to p1:
$ ruby -e '_="_=%p;puts _%%_";puts _%_' >> chain.rb
$ ruby -e '_="_=%p;puts _%%_";puts _%_' >> chain.rb

$ ruby chain.rb                                    # running p1p2p3
3

The ten first programs concatenated looks like this (318 bytes):

x=DATA.readlines.size
_="_=%p;puts _%%_"
puts x>0?x+1:_%_
__END__
_="_=%p;puts _%%_";puts _%_
_="_=%p;puts _%%_";puts _%_
_="_=%p;puts _%%_";puts _%_
_="_=%p;puts _%%_";puts _%_
_="_=%p;puts _%%_";puts _%_
_="_=%p;puts _%%_";puts _%_
_="_=%p;puts _%%_";puts _%_
_="_=%p;puts _%%_";puts _%_
_="_=%p;puts _%%_";puts _%_
\$\endgroup\$
1
\$\begingroup\$

C#, 2099 + 7 = 2106 bytes

First program (uses compiler flag /main:A):

class A{static void Main(){int a=System.Reflection.Assembly.GetEntryAssembly().GetTypes().Length;var b=@"class A{0}{{static void Main(){{var a=@""{1}"";System.Console.Write(a,{0}+1,a.Replace(""\"""",""\""\""""));}}}}";System.Console.Write(a>1?"{2}":b,0,b.Replace("\"","\"\""),a);}}

Second program:

class A0{static void Main(){var a=@"class A{0}{{static void Main(){{var a=@""{1}"";System.Console.Write(a,{0}+1,a.Replace(""\"""",""\""\""""));}}}}";System.Console.Write(a,0+1,a.Replace("\"","\"\""));}}

Third program:

class A1{static void Main(){var a=@"class A{0}{{static void Main(){{var a=@""{1}"";System.Console.Write(a,{0}+1,a.Replace(""\"""",""\""\""""));}}}}";System.Console.Write(a,1+1,a.Replace("\"","\"\""));}}

You get the idea.

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

Javascript ES6, score 483 455

Program 1, 77 bytes:

v=1;setTimeout(_=>alert(v>1?v:'a=_=>this.v?v++:alert("a="+a+";a();");a();'));

Program 2 and beyond, 42 bytes each:

a=_=>this.v?v++:alert("a="+a+";a();");a();
\$\endgroup\$
0
\$\begingroup\$

PHP, 1470 bytes

Program 1: 219 bytes:

class O{public$n=1;function __destruct(){echo($n=$this->n)>1?$n:'if(!$o->n++)echo str_replace(chr(9),$a=aWYoISRvLT5uKyspZWNobyBzdHJfcmVwbGFjZShjaHIoOSksJGE9CSxiYXNlNjRfZGVjb2RlKCRhKSk7,base64_decode($a));';}}$o=new O();

progam 2 and beyond 139 bytes:

if(!$o->n++)echo str_replace(chr(9),$a=aWYoISRvLT5uKyspZWNobyBzdHJfcmVwbGFjZShjaHIoOSksJGE9CSxiYXNlNjRfZGVjb2RlKCRhKSk7,base64_decode($a));

use like:

php -r "class O{public$n=1;function __destruct(){echo($n=$this->n)>1?$n:'if(!$o->n++)echo str_replace(chr(9),$a=aWYoISRvLT5uKyspZWNobyBzdHJfcmVwbGFjZShjaHIoOSksJGE9CSxiYXNlNjRfZGVjb2RlKCRhKSk7,base64_decode($a));';}}$o=new O();"

Uses a slightly golfed version the php quine technique detailed here: http://10types.co.uk/the-lab/a-minimal-php-quine/

\$\endgroup\$

Your Answer

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

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