The result of Kodehoved’s code golf competition has been found, and I got a shared 5th place.
The task was to add two large integers together by representing the integers with arrays of their digits, thus allowing them to become extremely large.
My contribution looks like this (weighing in at 138 characters):
1 2 3 4 5 6 |
public static int[] Mookid8000_Add(int[] a,int[] b){ var r=""; for(int n=a.Length,m=b.Length,s,c=0;n+m+c>0;c=s/10) r=(s=(n>0?a[--n]:0)+(m>0?b[--m]:0)+c)%10+r; return r.Select(t=>t-48).ToArray(); } |
Asger‘s and Lars‘ contribution looks like this (135 characters):
1 2 3 4 5 6 |
public static int[] AsgerHallasOgLarsUdengaard_Add(int[] a,int[] b){ var r=""; for(int c=0,x=a.Length,y=b.Length;x+y>0|c>9;) r=(c=(x>0?a[--x]:0)+(y>0?b[--y]:0)+c/10)%10+r; return r.Select(s=>s-48).ToArray(); } |
And the winners’ (Mads and Peter Sandberg Brun) contribution looks like this (weighing in at an incredibly compact, but almost unreadable, 132 characters :)):
1 2 3 4 5 6 |
public static int[] MadsOgPeterSandbergBrun_Add(int[] a,int[] b){ var c=""; for(int o=a.Length,p=b.Length,s=0;-o-p<(s=s/10+(0<o?a[--o]:0)+(0<p?b[--p]:0));) c=s%10+c; return c.Select(i=>i-48).ToArray(); } |
I usually don’t compete in competitions like this, because I’ve always thought of myself as pretty lame when it comes to solving coding puzzles, but this time it was great fun – especially since I was pretty motivated by my desire to beat Asger (my little sister’s boyfriend), who pushed me several iterations further than I would have gone on my own. He ended up beating me though, but I am still pretty satisfied with my fairly compact and almost readable solution.