aboutsummaryrefslogtreecommitdiff
path: root/day6.c
blob: 02d75e508b3b1405cc1ea30887ffbb03a2191b84 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  int i, j,
      rs, cs, re, ce,
      numlights = 0, brightness = 0;
  int** lights; int** lights2;
  char istr[10];

  lights = (int**) malloc(1000*sizeof(int*));
  lights2 = (int**) malloc(1000*sizeof(int*));
  for(i = 0; i < 1000; i++) {
    lights[i] = (int*) malloc(1000*sizeof(int));
    memset(lights[i], 0, 1000);
    lights2[i] = (int*) malloc(1000*sizeof(int));
    memset(lights2[i], 0, 1000);
  }

  while(scanf("%s", istr) != EOF) {
    if(strncmp(istr, "turn", 4) == 0)
      scanf("%s %d,%d through %d,%d", istr, &rs, &cs, &re, &ce);
    else
      scanf("%d,%d through %d,%d", &rs, &cs, &re, &ce);

    if(strncmp(istr, "on", 2) == 0) {
      for(i = rs; i <= re; i++)
        for(j = cs; j <= ce; j++) {
          lights[i][j] = 1;
          lights2[i][j] += 1;
        }
    } else if(strncmp(istr, "off", 3) == 0) {
      for(i = rs; i <= re; i++)
        for(j = cs; j <= ce; j++) {
          lights[i][j] = 0;
          if(lights2[i][j]) lights2[i][j] -= 1;
        }
    } else if(strncmp(istr, "toggle", 6) == 0) {
      for(i = rs; i <= re; i++)
        for(j = cs; j <= ce; j++) {
          lights[i][j] = !lights[i][j];
          lights2[i][j] += 2;
        }
    }
  }

  for(i = 0; i < 1000; i++)
    for(j = 0; j < 1000; j++) {
      if(lights[i][j]) numlights++;
      brightness += lights2[i][j];
    }

  printf("Number of lights that are on: %d\n", numlights);
  printf("Total brightness: %d\n", brightness);

  for(i = 0; i < 1000; i++) {
    free(lights[i]);
    free(lights2[i]);
  }
  free(lights);
  free(lights2);

  return 0;
}