aboutsummaryrefslogtreecommitdiff
path: root/src/2019/day4/aoc.cpp
blob: 8f15fb522f4910eced8835d9d2db760956b88573 (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
#include "aoc.h"
#include <string.h>
#include <string>
#include <vector>

namespace aoc2019 {

bool smaller(const char* s1, const char* s2) { return std::atoi(s1) < std::atoi(s2); }

bool two_identical(const char* s) {
  const char* n = s + strlen(s);
  while (s < n - 1) {
    if (*s == *(s + 1)) {
      return true;
    }
    s++;
  }
  return false;
}

bool two_identical_plus(const char* s) {
  auto is_right = [s](int i, int j) -> bool {
    bool b1 = s[i] == s[j];
    bool b2 = i > 0 ? s[i] != s[i - 1] : true;
    bool b3 = j < 5 ? s[j] != s[j + 1] : true;
    return b1 && b2 && b3;
  };
  for (int i = 0; i < 5; i++) {
    if (is_right(i, i + 1)) {
      return true;
    }
  }
  return false;
}

typedef bool (*identical_f)(const char*);

void next(size_t i, size_t j, char p[], const char* low, const char* high, int* c1, int* c2) {
  static char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  if (i == strlen(high)) {
    if (smaller(p, high) && smaller(low, p)) {
      *c1 += int(two_identical(p));
      *c2 += int(two_identical_plus(p));
    }
  } else {
    for (size_t x = j; x < ARRAY_SIZE(digits); x++) {
      p[i] = digits[x];
      next(i + 1, x, p, low, high, c1, c2);
    }
  }
}

std::pair<int, int> day4(const char* low, const char* high) {
  char pass[7] = {0};
  int c1{0};
  int c2{0};
  next(0, 0, pass, low, high, &c1, &c2);
  return {c1, c2};
}

} // namespace aoc2019