aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day6/aoc.cpp
blob: 6fd31592f5311378cf983c369f6eb938af0c947f (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
#include "aoc.h"

namespace aoc2022 {

int next4(const char* p) {
  char cs[4] = {0};
  for(int i = 0; i < 4; i++) {
    cs[i] = *(p + i);
  }
  if (cs[0] == cs[1]) return 1;
  if (cs[0] == cs[2]) return 1;
  if (cs[0] == cs[3]) return 1;
  if (cs[1] == cs[2]) return 2;
  if (cs[1] == cs[3]) return 2;
  if (cs[2] == cs[3]) return 3;
  return 0; 
}

int exam(int x, char cs[], int s) {
  if (x == s) {
    return 0;
  }
  for (int i = x + 1; i < s; i++) {
    if (cs[x] == cs[i]) {
      return x + 1;
    }
  }
  return exam(x+1, cs, s);
}

int next14(const char* p) {
  char cs[14] = {0};
  for(int i = 0; i < 14; i++) {
    cs[i] = *(p + i);
  }
  return exam(0, cs, 14);
}

std::pair<int,int> day6(line_view file) {
  const char *p0 = file.line;
  int d0 = next4(p0);
  while (d0 != 0) {
    p0 += d0;
    d0 = next4(p0);
  }

  const char *p1 = file.line;
  int d1 = next14(p1);
  while (d1 != 0) {
    p1 += d1;
    d1 = next14(p1);
  }

  return {p0 - file.line + 4 , p1 - file.line + 14};
}

}