Skip to content

Instantly share code, notes, and snippets.

@DavidBuchanan314
Last active August 9, 2023 23:12
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidBuchanan314/2280bff6e579e4c4ec9cd925a9ed9fea to your computer and use it in GitHub Desktop.
Save DavidBuchanan314/2280bff6e579e4c4ec9cd925a9ed9fea to your computer and use it in GitHub Desktop.
Yet another reformatting of my tiny game of life implementation. Valid ANSI C with no (default) compiler warnings or UB
#define F\
for(i=l;i\
<l*4; i++)
main(){int
i,j,w=512,
n,l=w*w,o[
]={~w,-w,-
w+1,-1,1,w
-1,w,w+1},
b[l*5];F b
[i]=rand();for(puts("YUV4MPEG\
2 W512 H512 C444");puts("FRAM\
E");){F{for(n=j=8;j;n-=1&b[i+o
[--j]]);b[i+l]=(n^5&&!b[i]|n^6
)-1;}F putchar(b[i]=b[i+l]);}}
/* gcc life.c;./a.out|mpv - */
@ilyakurdyukov
Copy link

ilyakurdyukov commented Oct 4, 2020

o[]={~w,-w,-w+1,-1,1,w-1,w,w+1},
...
for(n=j=8;j;n-=1&b[i+o[--j]]);
b[i+l]=(n^5&&!b[i]|n^6)-1;

Can be shortened to (24 bytes less):

for(n=j=9;j--;n-=1&b[i+j/3*w+j%3+~w]);
b[i+l]=(n^6&&!b[i]|n^5)-1;

And ffplay may be used as an alternative to mpv.

Nice work, though.

@DavidBuchanan314
Copy link
Author

Nice one, thanks!

@ilyakurdyukov
Copy link

ilyakurdyukov commented Oct 5, 2020

Also compiling with gcc -ansi gives this warning: ISO C90 forbids variable length array ‘b’ and Clang warns about variable length array used. Because you're using a variable in an expression, even if it's computable at compile time. Thus, the compiler can think of it as a dynamically sized array, which is not ANSI C. So consider using b[5<<18] instead of b[l*5]. But another issue is that allocating 5 MB on the stack is a bad practice in terms of portability, embedded systems may get a stack overflow.

I remember that I also ran into this when doing the entries for the IOCCC this year. You also have other style warnings, due to lack of main() return type and parentheses for n^5. Which might be considered a nonessential compiler complaints, but the IOCCC judges appreciate it when the code doesn't issue warnings with -Wall -Wextra -pedantic and even -Weveryting (Clang only).

I don't see you among the previous IOCCC winners, but I had a feeling that you could be. You can certainly win using your skills. So good luck if you want to enter the next competition. I have win in this year contest, but publication of a contest entries is still delayed for unknown reasons (planned to mid-August according to the site).

@DavidBuchanan314
Copy link
Author

Thank you for the advice. I haven't entered IOCCC before, but I definitely plan to. It's something I've wanted to do for a long time, so I'm humbled to be talking to a winner :)

@ilyakurdyukov
Copy link

Forgive me for this little criticism, this is actually the attention to detail and the experience of writing IOCCC entries, after months of polishing the code (rewriting it little by little every day, making it smaller and smaller), now I can easily see where improvements can be made.

I think you can still use this interesting way of displaying graphics using a video player in your next entries, even if you have published this work. But with something different from the Conway's game of life.

@ilyakurdyukov
Copy link

I'm used these 24 bytes to deal with any warnings that GCC and Clang can see. Even for Clang's -Weverything, but honestly, I think -Weverything are too excessive, I didn't bother about it in my works, only care about -Wall -Wextra -pedantic.

          #define F\
          for(i=l;i\
          <l*4; i++)
          int main()
          {int i,j,w
                    =512,n,l=w
                    *w; static
                    int b[ 5<<
                    18];F b[i]
                    =rand();f\
or(puts("YUV4MPEG2 W512 H512 \
C444");;){puts("FRAME");F{for(
n=j=9;j--;n-=1&b[i+j/3*w+j%3+~
w]){}b[i+l]=(n^6&&(!b[i]||n^5)
)-1;}F putchar(b[i]=b[i+l]);}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment