From 50a873db767f62befc8dc9b7abb96018bdb84d61 Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Fri, 4 Dec 2015 15:56:07 +0000 Subject: Day 3 *really* needed to be C. --- day3.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 day3.c diff --git a/day3.c b/day3.c new file mode 100644 index 0000000..00dca81 --- /dev/null +++ b/day3.c @@ -0,0 +1,84 @@ +#include +#include + +int** make2dWithOffset(int rows, int cols, int roff, int coff) { + int i; + + int** arr = (int**) malloc(rows * sizeof(int*)); + for(i = 0; i < rows ; i++) + arr[i] = (int*) malloc(cols * sizeof(int)) + coff; + + return (arr + roff); +} + +void free2dWithOffset(int** arr, int rows, int roff) { + int i; + for(i = -roff; i < rows - roff ; i++) + free(arr[i]); + free(arr - roff); +} + +int main() { + int** somap = make2dWithOffset(400, 400, 200, 200); + int** rsmap = make2dWithOffset(400, 400, 200, 200); + int ch, i, j, sosum = 0, rssum = 0, odd = 1; + int soeasting = 0, sonorthing = 0, + seasting = 0, snorthing = 0, + rseasting = 0, rsnorthing = 0; + + for(i = -200; i < 200; i++) + for(j = -200; j < 200 ; j++) { + somap[i][j] = 0; + rsmap[i][j] = 0; + } + + somap[0][0] = 1; + rsmap[0][0] = 2; + + while((ch = getchar()) != EOF) { + switch(ch) { + case '<': + soeasting--; + if (odd) + seasting--; + else + rseasting--; + break; + case '>': + soeasting++; + if (odd) + seasting++; + else + rseasting++; + break; + case 'v': + sonorthing--; + if (odd) + snorthing--; + else + rsnorthing--; + break; + case '^': + sonorthing++; + if (odd) + snorthing++; + else + rsnorthing++; + } + odd = !odd; + somap[soeasting][sonorthing]++; + rsmap[seasting][snorthing]++; + rsmap[rseasting][rsnorthing]++; + } + + for(i = -200; i < 200; i++) + for(j = -200; j < 200 ; j++) { + if(somap[i][j] > 0) + sosum++; + if(rsmap[i][j] > 0) + rssum++; + } + + printf("Santa only: %d\nRobosanta: %d\n", sosum, rssum); + return 0; +} -- cgit v1.2.1