aboutsummaryrefslogtreecommitdiff
path: root/day3.c
blob: 7179be683037900bbded7a7afdcaf8fb31a7b3c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdlib.h>
#include <stdio.h>

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 coff) {
  int i;
  for(i = -roff; i < rows - roff ; i++)
    free(arr[i] - coff);
  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++;
    }

  free2dWithOffset(somap, 400, 200, 200);
  free2dWithOffset(rsmap, 400, 200, 200);

  printf("Santa only: %d\nRobosanta:  %d\n", sosum, rssum);
  return 0;
}