Chaining Programs

26

4

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.

Downgoat

Posted 2015-12-22T01:40:56.403

Reputation: 27 116

Will the sequences p1p2p3... always be from from p1 to pn? – Moose – 2015-12-22T01:56:07.857

@Moose Yes, it will always be from p1 to pn. – Downgoat – 2015-12-22T01:57:07.393

5This is an interesting problem. The program sequence is easy enough; the chaining is harder. – Conor O'Brien – 2015-12-22T01:57:26.620

Is accessing files allowed? – Lynn – 2015-12-22T02:56:30.643

@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. – Downgoat – 2015-12-22T02:57:34.310

@CᴏɴᴏʀO'Bʀɪᴇɴ In some languages it's the other way around – SuperJedi224 – 2016-01-03T18:28:01.713

@SuperJedi224 Such as? – Conor O'Brien – 2016-01-03T18:28:40.633

This would seem to be the case in, for example, befunge. – SuperJedi224 – 2016-01-03T18:30:33.193

Answers

49

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.

Moose

Posted 2015-12-22T01:40:56.403

Reputation: 883

9

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.

Nikolai97

Posted 2015-12-22T01:40:56.403

Reputation: 653

+1 for using a different method (than mine). This is the type of creative answer I was hoping for. – Moose – 2015-12-23T03:19:57.463

+1 for Lua, and choosing a cooler, if more lengthy method than the other answers :P – cat – 2015-12-23T14:05:59.960

Haha thank you, I was quite proud that i managed to do this with a non-golfing and fairly verbose language :) – Nikolai97 – 2015-12-23T17:18:20.497

4

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!

Addison Crump

Posted 2015-12-22T01:40:56.403

Reputation: 10 763

4

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!

Addison Crump

Posted 2015-12-22T01:40:56.403

Reputation: 10 763

4

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.

quintopia

Posted 2015-12-22T01:40:56.403

Reputation: 3 899

2

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.

Conor O'Brien

Posted 2015-12-22T01:40:56.403

Reputation: 36 228

2

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 _%_

daniero

Posted 2015-12-22T01:40:56.403

Reputation: 17 193

1

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.

LegionMammal978

Posted 2015-12-22T01:40:56.403

Reputation: 15 731

0

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();

SuperJedi224

Posted 2015-12-22T01:40:56.403

Reputation: 11 342

0

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/

user59178

Posted 2015-12-22T01:40:56.403

Reputation: 1 007