To avoid while(-1) problems, this only decrements Y when it is nonzero and uses another variable Z to store whether or not Y was decremented in the last loop iteration, so if Y was decremented in the final iteration it is at least as large as X. With a minor addition to the Y >= X block one could check if Y were true which would mean Y > X but I haven't gone and done that. You could also set a variable to 1 right before the Y>=X block and set it to 0 inside the block and later check that variable for an X>Y block.
[ while X (slot 0) is not 0 >> move to Z (slot 2) [ loop sets Z to 0 - ] < move to Y (slot 1) [ if Y is nonzero, set Z to 1 - decrement Y > move to Z + set Z to 1 > move to a slot which will be 0 and terminate the loop [ not really necessary unless the next variable is indeterminate - ] ] slot 3 is 0 and pointed to so loop terminates after 1 iteration < move to X - decrement X ] >> point to Z [ if Z is 1 then Y >= X #If we are here, then Y >= X [ set Z to 0 to terminate loop - ] ]


Will I look at this tomorrow morning and say "wow, that's clever" or "wow, I really needed some sleep"?

Update This will not work if X is positive and Y is negative, or vice versa, it will be determined that the positive one is smaller (because it reaches zero first when decrementing) but if they are both negative it will work as the lesser one will hit zero first when decrementing (it will wraparound first, and thus hit zero first).

Update That didn't work, there was a loop in which it didn't know what variable it was pointing to after the loop, so we need to store X and Y in non-contiguous areas of memory and have a different value after X and another after Y to help us figure out where we're pointing

> point to slot 1 [ - initialize slot 1 to zero ] >> point to slot 3 [ - set slot 3 to zero ] + set slot 3 to 1 > point to slot 4 [ - initialize slot 4 to 0 ] <<<< point to X [ while X >>>>> point to Z [ - set Z to 0 ] <<< point to Y [ if Y is nonzero, set Z to 1 - decrement Y >>> point to Z + Z = 1 < point to slot 4 which is always zero ] loop ends after one iteration < point to either slot 1 or slot 3 [ if we are here then we are pointing to slot 3 << point to slot 1 } < point to X - decrement X ] >>>>> point to Z [ Y >= X, do whatever here < point to slot 4, always zero to terminate the loop ]
And the equivalent C code:
#include <stdio.h> #include <stdlib.h> #define X 0 #define Y 2 #define Z 5 /* slot 0: X slot 1: always zero, let's us know X is before it slot 2: Y slot 3: always one, let's us know Y is before it slot 4: always zero, used to terminate loops slot 5: Z */ int main(int argc, char **argv) { short int vars[5]; short int *p = vars; if(argc != 3) { fprintf(stderr, "Usage: %s X-value Y-value\n", argv[0]); exit(1); } vars[X] = atoi(argv[1]); /* set X */ vars[Y] = atoi(argv[2]); /* set Y */ p++; /* point to slot 1 */ while(*p) { (*p)--; /* initialize slot 1 to zero */ } p++;p++; /* point to slot 3 */ while(*p) { (*p)--; /* set slot 3 to zero */ } (*p)++; /* set slot 3 to one */ p++; /* point to slot 4 */ while(*p) { (*p)--; /* initialize slot 4 to zero */ } p--;p--;p--;p--; /* point to X */ while(*p) /* while X */ { p++;p++;p++;p++;p++; /* point to Z */ while(*p) { (*p)--; /* set Z to 0 */ } p--;p--;p--; /* point to Y */ while(*p) /* if Y is nonzero, set Z to 1 */ { (*p)--; /* decrement Y */ p++;p++;p++; /* point to Z */ (*p)++; /* Z = 1 */ p--; /* point to slot 4 which is always zero */ } /* loop terminates after 1 iteration */ p--; /* point to either slot 1 or slot 3 */ while(*p) { /* if we are here, we are pointing to slot 3 */ p--;p--; /* point to slot 1 */ } p--; /* point to X */ (*p)--; /* decrement X */ } p++;p++;p++;p++;p++; /* point to Z */ while(*p) { printf("Y >= X\n"); p--; /* point to slot 4, always zero */ } return 0; }

In reply to Re: Brainf*ck Question by nardo
in thread Brainf*ck Question by japhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.