Page 1 of 1

Quine

Posted: Sun Nov 30, 2008 12:46 am
by Allosentient
The key was subtracting 32 from the ascii value in order to get a guaranteed two digit number.

Code: Select all

         
   string program3SetCount = "99*1-2*1>";  //program logic length * 2
            string program3PrintNums = "1<0>" + "0<" + "83*2-?" + "1<0<" + "1-0>" + 
                "1-" +
                "v0^p" + "074*-2-g";

            string program3PrintChars = "1<0>" + "0<" + "73*3+?" + "52**+" + "84*+" + "P" + 
                "0<2-0>" + "074*-4-g!";

            string program3a = program3SetCount + program3PrintNums + program3PrintChars;

            int program3length = program3a.Length;

            string numericString = "";
            for (int i = program3a.Length - 1; i >= 0; i--)
            {
                string num = ((int)program3a[i] - 32).ToString();
                num = "00" + num;
                num = num.Substring(num.Length - 2, 2);

                char[] cnum2 = new char[num.Length];

                for(int i1 = 0; i1 < num.Length; i1++)
                {
                    cnum2[cnum2.Length-1-i1] = num[i1];
                }
                string num2 = new string(cnum2);

                numericString += num2;
            }

            hvm.Run(numericString + program3a, "", false);
101731023101023261036131818261841101024211010181121311910191328261036182711731813101023261082661
683171036131718261827113318101914282610361827103710181317101525299*1-2*1>1<0>0<83*2-?1<0<1-0>1-
v0^p074*-2-g1<0>0<73*3+?52**+84*+P0<2-0>074*-4-g!

240 instructions, 7553 cycles

(I worked madly all night to finish this so I could be the first one to the end)




here are some of my notes:
/* See comments below */

//should be three digits per character
//One character per memory address

const unsigned char data[] = {
/* 000000 */ 0x2f, 0x2a, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20,
/* 0x0008 */ 0x69, 0x73, 0x20, 0x61, 0x20, 0x73, 0x65, 0x6c,
/* 0x0010 */ 0x66, 0x72, 0x65, 0x70, 0x20, 0x28, 0x71, 0x75,
/* 0x0018 */ 0x69, 0x6e, 0x65, 0x29, 0x20, 0x70, 0x72, 0x6f,
/* 0x0020 */ 0x67, 0x72, 0x61, 0x6d, 0x2e, 0x20, 0x20, 0x49,
/* 0x0028 */ 0x74, 0x20, 0x75, 0x73, 0x65, 0x73, 0x20, 0x74,
/* 0x0030 */ 0x68, 0x65, 0x20, 0x61, 0x62, 0x6f, 0x76, 0x65,
/* 0x0038 */ 0x20, 0x64, 0x61, 0x74, 0x61, 0x20, 0x28, 0x77,
/* 0x0040 */ 0x68, 0x69, 0x63, 0x68, 0x0a, 0x20, 0x2a, 0x20,
/* 0x0048 */ 0x69, 0x73, 0x20, 0x6e, 0x6f, 0x20, 0x6f, 0x74,
/* 0x0050 */ 0x68, 0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6e,
/* 0x0058 */ 0x20, 0x74, 0x68, 0x65, 0x20, 0x41, 0x53, 0x43,
/* 0x0060 */ 0x49, 0x49, 0x20, 0x72, 0x65, 0x70, 0x72, 0x65,
/* 0x0068 */ 0x73, 0x65, 0x6e, 0x74, 0x61, 0x74, 0x69, 0x6f,
/* 0x0070 */ 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x65, 0x76, 0x65,
/* 0x0078 */ 0x72, 0x79, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x20,
/* 0x0080 */ 0x73, 0x74, 0x61, 0x72, 0x74, 0x69, 0x6e, 0x67,
/* 0x0088 */ 0x0a, 0x20, 0x2a, 0x20, 0x66, 0x72, 0x6f, 0x6d,
/* 0x0090 */ 0x20, 0x74, 0x68, 0x69, 0x73, 0x20, 0x63, 0x6f,
/* 0x0098 */ 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x29, 0x20, 0x74,
/* 0x00a0 */ 0x6f, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x20,
/* 0x00a8 */ 0x69, 0x74, 0x73, 0x20, 0x6f, 0x77, 0x6e, 0x20,
/* 0x00b0 */ 0x6c, 0x69, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x2e,
/* 0x00b8 */ 0x20, 0x2a, 0x2f, 0x0a, 0x0a, 0x23, 0x69, 0x6e,
/* 0x00c0 */ 0x63, 0x6c, 0x75, 0x64, 0x65, 0x20, 0x3c, 0x73,
/* 0x00c8 */ 0x74, 0x64, 0x69, 0x6f, 0x2e, 0x68, 0x3e, 0x0a,
/* 0x00d0 */ 0x0a, 0x69, 0x6e, 0x74, 0x0a, 0x6d, 0x61, 0x69,
/* 0x00d8 */ 0x6e, 0x20, 0x28, 0x76, 0x6f, 0x69, 0x64, 0x29,
/* 0x00e0 */ 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x2f, 0x2a,
/* 0x00e8 */ 0x20, 0x54, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x69,
/* 0x00f0 */ 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, 0x61,
/* 0x00f8 */ 0x6d, 0x2e, 0x20, 0x20, 0x57, 0x65, 0x20, 0x6f,
/* 0x0100 */ 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x74, 0x68,
/* 0x0108 */ 0x65, 0x20, 0x64, 0x61, 0x74, 0x61, 0x20, 0x69,
/* 0x0110 */ 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x66, 0x6f,
/* 0x0118 */ 0x72, 0x6d, 0x61, 0x74, 0x20, 0x75, 0x73, 0x65,
/* 0x0120 */ 0x64, 0x20, 0x61, 0x74, 0x0a, 0x20, 0x20, 0x20,
/* 0x0128 */ 0x20, 0x20, 0x20, 0x2a, 0x20, 0x74, 0x68, 0x65,
/* 0x0130 */ 0x20, 0x74, 0x6f, 0x70, 0x20, 0x6f, 0x66, 0x20,
/* 0x0138 */ 0x74, 0x68, 0x69, 0x73, 0x20, 0x66, 0x69, 0x6c,
/* 0x0140 */ 0x65, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74,
/* 0x0148 */ 0x68, 0x65, 0x6e, 0x20, 0x77, 0x65, 0x20, 0x75,
/* 0x0150 */ 0x73, 0x65, 0x20, 0x69, 0x74, 0x20, 0x74, 0x6f,
/* 0x0158 */ 0x20, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74,
/* 0x0160 */ 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72, 0x65,
/* 0x0168 */ 0x73, 0x74, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
/* 0x0170 */ 0x20, 0x2a, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68,
/* 0x0178 */ 0x69, 0x73, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2e,
/* 0x0180 */ 0x20, 0x2a, 0x2f, 0x0a, 0x7b, 0x0a, 0x20, 0x20,
/* 0x0188 */ 0x75, 0x6e, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64,
/* 0x0190 */ 0x20, 0x69, 0x6e, 0x74, 0x20, 0x69, 0x3b, 0x0a,
/* 0x0198 */ 0x0a, 0x20, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74,
/* 0x01a0 */ 0x66, 0x20, 0x28, 0x22, 0x2f, 0x2a, 0x20, 0x53,
/* 0x01a8 */ 0x65, 0x65, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 0x65,
/* 0x01b0 */ 0x6e, 0x74, 0x73, 0x20, 0x62, 0x65, 0x6c, 0x6f,
/* 0x01b8 */ 0x77, 0x20, 0x2a, 0x2f, 0x5c, 0x6e, 0x5c, 0x6e,
/* 0x01c0 */ 0x22, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x70, 0x72,
/* 0x01c8 */ 0x69, 0x6e, 0x74, 0x66, 0x20, 0x28, 0x22, 0x63,
/* 0x01d0 */ 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x75, 0x6e, 0x73,
/* 0x01d8 */ 0x69, 0x67, 0x6e, 0x65, 0x64, 0x20, 0x63, 0x68,
/* 0x01e0 */ 0x61, 0x72, 0x20, 0x64, 0x61, 0x74, 0x61, 0x5b,
/* 0x01e8 */ 0x5d, 0x20, 0x3d, 0x20, 0x7b, 0x22, 0x29, 0x3b,
/* 0x01f0 */ 0x0a, 0x20, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x28,
/* 0x01f8 */ 0x20, 0x69, 0x3d, 0x30, 0x20, 0x3b, 0x20, 0x69,
/* 0x0200 */ 0x3c, 0x73, 0x69, 0x7a, 0x65, 0x6f, 0x66, 0x28,
/* 0x0208 */ 0x64, 0x61, 0x74, 0x61, 0x29, 0x20, 0x3b, 0x20,
/* 0x0210 */ 0x69, 0x2b, 0x2b, 0x20, 0x29, 0x0a, 0x20, 0x20,
/* 0x0218 */ 0x20, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20,
/* 0x0220 */ 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x20, 0x69,
/* 0x0228 */ 0x25, 0x38, 0x20, 0x3d, 0x3d, 0x20, 0x30, 0x20,
/* 0x0230 */ 0x29, 0x0a, 0x09, 0x70, 0x72, 0x69, 0x6e, 0x74,
/* 0x0238 */ 0x66, 0x20, 0x28, 0x22, 0x5c, 0x6e, 0x2f, 0x2a,
/* 0x0240 */ 0x20, 0x25, 0x30, 0x23, 0x36, 0x78, 0x20, 0x2a,
/* 0x0248 */ 0x2f, 0x22, 0x2c, 0x69, 0x29, 0x3b, 0x0a, 0x20,
/* 0x0250 */ 0x20, 0x20, 0x20, 0x20, 0x20, 0x70, 0x72, 0x69,
/* 0x0258 */ 0x6e, 0x74, 0x66, 0x20, 0x28, 0x22, 0x20, 0x20,
/* 0x0260 */ 0x25, 0x30, 0x23, 0x34, 0x78, 0x2c, 0x22, 0x2c,
/* 0x0268 */ 0x20, 0x64, 0x61, 0x74, 0x61, 0x5b, 0x69, 0x5d,
/* 0x0270 */ 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d,
/* 0x0278 */ 0x0a, 0x20, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74,
/* 0x0280 */ 0x66, 0x20, 0x28, 0x22, 0x5c, 0x6e, 0x7d, 0x3b,
/* 0x0288 */ 0x5c, 0x6e, 0x5c, 0x6e, 0x22, 0x29, 0x3b, 0x0a,
/* 0x0290 */ 0x20, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x28, 0x20,
/* 0x0298 */ 0x69, 0x3d, 0x30, 0x20, 0x3b, 0x20, 0x69, 0x3c,
/* 0x02a0 */ 0x73, 0x69, 0x7a, 0x65, 0x6f, 0x66, 0x28, 0x64,
/* 0x02a8 */ 0x61, 0x74, 0x61, 0x29, 0x20, 0x3b, 0x20, 0x69,
/* 0x02b0 */ 0x2b, 0x2b, 0x20, 0x29, 0x0a, 0x20, 0x20, 0x20,
/* 0x02b8 */ 0x20, 0x70, 0x75, 0x74, 0x63, 0x68, 0x61, 0x72,
/* 0x02c0 */ 0x20, 0x28, 0x64, 0x61, 0x74, 0x61, 0x5b, 0x69,
/* 0x02c8 */ 0x5d, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x72, 0x65,
/* 0x02d0 */ 0x74, 0x75, 0x72, 0x6e, 0x20, 0x30, 0x3b, 0x0a,
/* 0x02d8 */ 0x7d, 0x0a,
};

/* This is a selfrep (quine) program. It uses the above data (which
* is no other than the ASCII representation of everything starting
* from this comment) to print its own listing. */

114110 00>61> 1<0<0^2v1v-83*?2+0^2v1v>2-1+0>084*5+-g$$!!!!

///Store each character in three memory locations each
123214144
00> //Store initial counter 0 in m0
count 1> // store count (+1) (of array digits) in m1

1< //Begin loop function, load array count from m1
0< //load counter from m0
0^ //copy counter
2v //roll copy back
1v //roll again
- //subtract counter from total
83*? //if counter - total == 0, end function (go ahead 24 to $)

2+ //add two (memory offset) to counter
0^ //copy counter
2v //Roll copy backwards so that it appears after the next two operations
1v
> //Store the digit into memory cell
2- //subtract offset to get counter
1+ //add 1 to counter
0> //Store counter in m0
//End
084*6+-g (go back 37 to beginning of function)



///Function to load a character (three digits) at memory address S0 into S0
//old 00^<88*66*+*1v1+0^<55+*1v1+<++$

//new 74*g0^<88*66*+*1v1+<55+*+1^2+<+$

///Loop to print each "array" character as a 1 digit number
//old (prints backwards) 00> 1<0<0^2v1v-66*?2+0^1v<p1+0^<p1+0^<p1+2-0>085*9+-g!!!!
//old 00>1<0<0^2v1v-67*?2+0^1v1<2+<p1+0^<p1+0^<p1+2-0>086*5+-g$$$

//new
1<0>0<0^54*?1-2+0^<p2-0>074*-0+g
1< get count
0> store count in m0
0< begin loop, get count
0^ copy count
54*? end function if count is 0 //count is 6
1- subtract 1 from count
2+ add 2 for offset to get mem loc
0^ copy mem loc
< load mem loc
p print
2- subtract 2 for offset
0> store counter in m0
074*-0+g go back to beginning of loop (28 chars)



0<0^67*?1-2+0^<p3-0>074*-0+g

///Loop to print each "array" character as a character (95 has to be changed to address of char load func)
//old 00> 1<0<0^2v1v-66*?2+0^95*cP3+2-0>085*9+-g!!!!

//new 00>1<0<0^2v1v-83*?2+0^4cP3+2-0>094*0+-g!!!!


74*g //skip function as it is length 28 (may need change)

0^< //load from m(s0)
88*66*+* //multiply by 100
1v //roll (get mem address)
1+< //add one and get next mem loc into s0
55+*+ //multiply by 10 and add
1^2+< //get next mem variable from copy
+$ //Add last digit and return

00>61>12>13>14>15>16>27> //load mem (temporary)

//print chars
dd
00> put 0 as counter
1< load counter, begin loop
0< //load m0
0^ //copy m0
2v1v //roll
- //subtract
74*?//go forward 28 (end function) if counter is 0
2+ //add 2 (mem offset)
//3- //subtract 2 to get mem loc of next char
0^ //copy mem loc
62*c //transform offset into character
P //print
1+ //subtract 5 to go to next index
0> //store index in m0
074*1+-g //subtract 36
!!!!
dd00>1<0<0^2v1v74*?2+0^62*cP1+0>074*3+-g!!!!!!!!!!


#include <stdio.h>
74*g0^<88*66*+*1v1+<55+*+1v1+<+$00>61>12>13>14>15>16>27>00>1<0<0^2v1v-66*?2+0^4cP3+2-0>094*0+-g!!!!

int
main (void)
/* The main program. We output the data in the format used at
* the top of this file, and then we use it to generate the rest
* of this file. */
{
unsigned int i;

printf ("/* See comments below */\n\n");
printf ("const unsigned char data[] = {");
for ( i=0 ; i<sizeof(data) ; i++ )
{
if ( i%8 == 0 )
printf ("\n/* %0#6x */",i);
printf (" %0#4x,", data);
}
printf ("\n};\n\n");
//Print the characters in the array themselves
//Print each char in array as a char 'P'
//!

103
dd00>1<0<0^2v1v-74*?2+0^5554***56*+1+cP1+0>095*1--g!
}

Posted: Sun Nov 30, 2008 8:49 am
by gfoot
I found memory access inefficient - it was easier to just leave everything on the stack. But maybe I was just doing it badly.

My first attempts used memory access, were thousands of characters long, and took too many instructions. The one I solved it with was just under 300 instructions, and used three base-9 digits per data item.

After seeing how short tails got it, I switched to two base-9 digits per character, using an offset of 42 to keep everything in range. That got down to 190 instructions - still nowhere near tails' though!

Posted: Sun Nov 30, 2008 4:41 pm
by therethinker
My program consisted of two parts like yours. I didn't utilize memory though.
The second part consisted of all the characters in the first part encoded using 4 numbers. IIRC, it would multiply the first 3 then add the 4th number.

The first part first jumped to the second to put all the numbers in memory, then it looped over them printing out each character. It then jumped to the second part yet again, put all the numbers in memory, only this time it didn't sum the numbers and just printed each one individually.

In retrospect, it probably would have been more efficient to put the numbers first like allo. did.

Posted: Sat Dec 06, 2008 3:13 am
by therethinker
I just want to clear myself ;-)

I found a copy of a quine program: 322 characters. It runs in 6313 cycles-- which is much more than I had recalled. (I'm still not sure how it racked up so many cycles: it isn't very loopy...)

Edit: This new challenge is pretty tough. I'm *so* close: 99 instructions!