Code Golf: Conway’s Game of Life

Mathematica – 179 163 154 151 chars a = {2, 2, 2}; s = Export[“out.txt”, CellularAutomaton[{224, {2, {a, {2, 1, 2}, a}}, {1,1}}, (ReadList[#1, Byte, RecordLists → 2>1] – 46)/ 42, #2]〚#2〛 /. {0 → “.”, 1 → “X”}, “Table”] & Spaces added for readability Invoke with s[“c:\life.txt”, 100] Animation: You can also get a … Read more

Code Golf: Mathematical expression evaluator (that respects PEMDAS)

C (465 characters) #define F for(i=0;P-8;i+=2) #define V t[i #define P V+1] #define S V+2]),K(&L,4),i-=2) #define L V-2] K(double*t,int i){for(*++t=4;*t-8;*++t=V])*++t=V];}M(double*t){int i,p,b; F if(!P)for(p=1,b=i;i+=2,p;)P?P-1||–p||(P=8,M(t+b+2),K(t+b,i-b),i=b):++p; F P-6||(L=pow(L,S;F P-2&&P-7||(L*=(P-7?V+2]:1/S;F P-4&&(L+=(P-5?V+2]:-S; F L=V];}E(char*s,char*r){double t[99];char*e,i=2,z=0;for(;*s;i+=2)V]= strtod(s,&e),P=z=e-s&&z-4&&z-1?s=e,4:*s++&7;P=8;M(t+2);sprintf(r,”%g”,*t);} The first five newlines are required, the rest are there just for readability. I’ve counted the first five newlines as one character each. If … Read more

Code Golf: Number to Words

Ok, I think it’s time for my own implementation in Windows BATCH script (should work on Windows 2000 or later). Here is the code: @echo off set zero_to_nineteen=Zero One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen set twenty_to_ninety=ignore ignore Twenty Thirty Forty Fifty Sixty … Read more