aboutsummaryrefslogtreecommitdiff
path: root/day8/day8.c
blob: c318863a20faac068c5f08e89d25c51561fc880f (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
#include <stdio.h>

int readline(char* line) {
  int ch, idx = 0;

  while((ch = getchar()) != '\n' && ch != EOF)
    line[idx++] = (char) ch;

  line[idx] = '\0';
  return idx;
}

int calcdiff1(char* line) {
  int i, code = 0, mem = 0;

  for(i = 0; line[i] != '\0'; i++) {
    switch(line[i]) {
      case '"':
        code += 1;
        break;
      case '\\':
        i += 1;
        switch(line[i]) {
          case '\\':
          case '"':
            code += 2;
            mem += 1;
            break;
          case 'x':
            i += 2;
            code += 4;
            mem += 1;
        }
        break;
      default:
        code += 1;
        mem += 1;
    }
  }

  return code - mem;
}

int calcdiff2(char* line) {
  int i, nl = 2;

  for(i = 0; line[i] != '\0'; i++) {
    switch(line[i]) {
      case '"':
      case '\\':
        nl += 2;
        break;
      default:
        nl += 1;
    }
  }

  return nl - i;
}

int main() {
  char line[127];
  int diff1 = 0, diff2 = 0;

  while(readline(line)) {
    diff1 += calcdiff1(line);
    diff2 += calcdiff2(line);
  }

  printf("Part 1: %d\n", diff1);
  printf("Part 2: %d\n", diff2);
  return 0;
}