Geeks With Blogs

They call me Big Papa 4 kids and counting

Given the day of the year and the year (both as ints), return the date in “m/d/yyyy” (string) format without using the System.DateTime namespace or any other built-in date/time handler.

One solution (1858 IL characters)

```static string g(int d, int y) { int l; int i = 0; if (d < 0) { i = 13; } l = ((y % 400 == 0) | ((y % 100 != 0) & (y % 4 == 0))) ? 1 : 0; int[] m = new int[13] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; while (i < 13) { if (i > 1) { m[i] = m[i] + l; } if (d <= (m[i])) { return i + "/" + (d - m[i - 1]) + "/" + y; } i++; } return "e"; } ```

Another solution (1814 IL characters):
```public string G(int d, int y) { if (d < 1 || y < 1) return "e"; bool l = y % 4 == 0 && (y % 100 != 0 || y % 400 == 0) ? true : false; d = l ? d - 1 : d; int[] m = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int i = 0; do { if (d > m[i]) d -= m[i]; else break; } while (++i < 12); d = l ? i < 2 ? d + 1 : d : d; return i > 11 ? "e" : String.Format("{0}/{1}/{2}", i + 1, d, y); } ```

Another (1718 IL characters):
```static string r(int d, int y) { int m = 0; bool l = y % 4 == 0 && (y % 100 != 0 || y % 400 == 0) ? true : false; int[] c = { 31, l ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; while (d > c[m < 11 ? m : 11]) { d -= c[m < 11 ? m : 11]; m++; } return d < 1 || y < 1 || m > 11 ? "E" : String.Format("{0}/{1}/{2}", m + 1, d, y); } ```

Winner (1538 IL characters)
```static string D(int d, int y) { bool L = y % 4 == 0 && y % 100 != 0 || y % 400 == 0; if (d > (L ? 366 : 365) || d <= 0 || y <= 0) return null; int x = 0, t = 3; string i = string.Format("3{0}3232332323", L ? "1" : "0"); string s = "JFMAMJJASOND"; while (d > t + 28) { d -= t + 28; t = int.Parse(i[++x].ToString()); } return string.Format("{0}/{1}/{2}", s[x], d, y); } ```

Related Posts on Geeks With Blogs Matching Categories

Comments on this post: Coding Challenge - Date Me