Writing code for three tasks When you run your program first, it should print an integer sequence.  The
second run should print the 'Mary had a little lamb' song. The third run should
produce an ASCII image file.

Scoring

Your score will be the sum of your bytes of code and any file content at the
start of each of the 3 runs. Lowest score wins.

For example, myprog is 1500 bytes and does not change its size, but writes a 4
bytes file each run so it knows what to do on the next run. The score would be
1500 + (1500+4) + (1500+4) = 4508.

Output

The first output is this sequence of 133 integers, separated by single spaces:

0 89 98 179 188 197 269 278 287 296 359 368 377 386 395 449 458 467 476 485
494 539 548 557 566 575 584 593 629 638 647 656 665 674 683 692 719 728
737 746 755 764 773 782 791 809 818 827 836 845 854 863 872 881 890 908
917 926 935 944 953 962 971 980 1079 1088 1097 1169 1178 1187 1196 1259
1268 1277 1286 1295 1349 1358 1367 1376 1385 1394 1439 1448 1457 1466 1475
1484 1493 1529 1538 1547 1556 1565 1574 1583 1592 1619 1628 1637 1646 1655
1664 1673 1682 1691 1709 1718 1727 1736 1745 1754 1763 1772 1781 1790 1808
1817 1826 1835 1844 1853 1862 1871 1880 1907 1916 1925 1934 1943 1952 1961
1970


The second output is this text exactly (including capitals, punctuation, and
white space - the inconsistent punctuation is intentional):

Mary had a little lamb,
Little lamb, little lamb,
Mary had a little lamb,
Its fleece was white as snow

And everywhere that Mary went,
Mary went, Mary went,
Everywhere that Mary went
The lamb was sure to go

It followed her to school one day
School one day, school one day
It followed her to school one day
Which was against the rules.

It made the children laugh and play,
Laugh and play, laugh and play,
It made the children laugh and play
To see a lamb at school


The third output is this image in PPM ASCII(P3) format left in a file called
blue.ppm. This is the image in PNG format:



The PPM file should look something like this:

P3 50 50 255
255 255 255 255 255 255 255 255 255 255 255 255 ...
...
255 255 255


There is a copy of the full PPM file here.

Example run (on Linux)

# ls
myprog
# interpreter myprog
0 89 98 179 188 197 269 278 287 296 359 ...
# interpreter myprog
Mary had a little lamb,
Little ...
# interpreter myprog
# ls
blue.ppm
myprog
# cat blue.ppm
P3 50 50 255
255 255 255 255 255 ...


Rules


Your code may modify itself.
Running the program a fourth time is undefined behaviour.
You may write to a file in the current directory, but the size of this file
will add to your score at the start of each run. The final PPM file is
excluded from this.
The first two outputs are to stdout (or similar). The last output is written
to a file, with nothing sent to stdout.
The first two outputs may optionally have a trailing white space character (e.g. space or line feed).
You may not use compression or image libraries. You may use your own
compression or image functions but this code must be included in your program.
The standard loopholes are prohibited.
Ruby, 1356 1268 * 3 = 3804 bytes

if File.file?(?b)
s='P3
50 50
255
'
d=e=0
[153,44,6,1,42,1,6,1,42,1,6,1,42,1,6,1,42,1,6,1,42,1,6,1,42,1,4,40,5,1,4,1,1,1,36,1,5,1,4,1,1,1,36,1,5,1,4,1,1,1,36,1,5,1,4,1,1,1,36,1,5,1,4,1,1,34,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,8,31,5,1,4,1,8,1,25,1,9,1,4,1,8,1,25,1,9,1,4,1,8,1,25,1,9,1,4,1,8,1,25,1,9,1,4,1,8,1,25,1,9,1,4,1,8,1,25,1,9,1,4,1,8,1,25,1,9,1,4,1,8,37,4,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,36,63].map{|n|n.times{s+=[" 255 255 255"," 0 0 255"][d];s+=$/if (e+=1)%50==0};d=1-d}
File.write 'blue.ppm',s
elsif File.file?(?a)
puts [["","Mary had a ","little lamb",?,,?,,"Its fleece was white as snow"],["And ","everywhere that ","Mary went",?,,'',"The lamb was sure to go"],["","It followed her to ","school one day",'','',"Which was against the rules."],["","It made the children ","laugh and play",?,,'',"To see a lamb at school"]].map{|a,b,c,d,e,f|[a+b+c+d,c.capitalize+', '+c+d,b.capitalize+c+e,f]*$/}*"

"
`touch b`
else
1971.times{|i|$&gt;&lt;&lt;"#{i} "if i.to_s.chars.map(&amp;:to_i).reduce(:+)%17&lt;1}
`touch a`
end


To keep track of the invocation, I'm creating an empty file a on the first run and an empty file b on the second run.


The first program uses a certain property of the numbers.
The second program uses a bunch of reusable parts of the verses, but that currently only saves 100 bytes over naively printing the entire thing.
The third program currently uses just a simple run-length encoding. I'll look into actually encoding the path of the line later.