C, 259 231 bajtów
Kod do gry w golfa
#define v a[1][i
i,k,l,x,h,w;main(char*s,char**a){for(;v];w+=2*!x,s=v++],h=x>h?x:h)x=(s==v])*(x+1);h++;s=malloc((x=h++*++w+1)+w);memset(s,32,h*w);for(i=k;v];s[x+1]=s[x]=k=v++],x=k==v]?x-w:(h-1)*w+l++*2+3)s[i*w]=10;printf("%s",s);}
Pełny kod
//Variable Explanations:
//i - increment through argument string, must beinitialized to 0
//k - increment through argument string, must be initialized to 0
//l - record x coordinate in return value, must be initialized to 0
//x - record the actual character position within the return string
//arrheight - the height of the return string
//arrwidth - the width of the return string
//arr - the return string
//argv - the string containing the arguments
#define v argv[1][i
i,k,l,x,arrheight,arrwidth;
main(char*arr,char**argv){
for(;v]; //For Length of input
arrwidth+=2*!x, //increment width by 2 if this char is not the same as the last
arr=v++], //set arr to current char
arrheight=x>arrheight?x:arrheight //see if x is greater than the largest recorded height
)x=(arr==v])*(x+1); //if this character is the same as the last, increment x (using arr to store previous char)
arrheight++; //increment height by one since its 0 indexed
arr=malloc((x=arrheight++*++arrwidth+1)+arrwidth); //create a flattened array widthxheight and set x to be the bottom left position
memset(arr,32,arrheight*arrwidth); //fill array with spaces
for(i=k;v]; //For Length of input
arr[x+1]=arr[x]=k=v++], //set x and x+1 positions to the current character, store current character in i
x=k==v]?x-arrwidth:(arrheight-1)*arrwidth+l++*2+3 //if next char is same as current move vertically, else set x to bottom of next column
)arr[i*arrwidth]=10; //Add new lines to string at end of width
printf("%s",arr); //output string
}
Kompilowany z GCC, bez specjalnych flag
Edytować
Zaoszczędzono 28 bajtów dzięki Adelphusowi. Jego zmiana pozwoliła mi stworzyć definicję. I utworzyłem pętle while w pętlach for, aby zaoszczędzić 2 bajty, zmieniając układ pętli. Rozwiązałem również problem polegający na tym, że kod pękał, gdy ostatni wprowadzony znak nie był singletonem. Kod zawiedzie, jeśli jest tylko jedna unikalna litera, ale powinien działać we wszystkich innych przypadkach.