aboutsummaryrefslogtreecommitdiff
path: root/src/2015/day5/aoc.cpp
blob: aa0213f8a6ae4568c460c059e9f111198f840f1a (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
#include "aoc.h"
#include <algorithm>

namespace aoc2015 {

int count_vowels(line_view lv, const char* vowels) {
  ascii_count ac{lv};
  int total = 0;
  per_char(vowels, [&ac, &total](char c) {
    total += ac[c];
    return true;
  });
  return total;
}

bool is_nice(line_view lv, int repeated) {
  const char* p = lv.line;
  while (p + repeated <= lv.line + lv.length) {
    if (is_repeated(p, p + repeated)) {
      return true;
    } else {
      p++;
    }
  }
  return false;
}

bool is_nice(line_view lv, const char* disallowed[], size_t size) {
  return not std::any_of(disallowed, disallowed + size, [&lv](const char* s) -> bool { return lv.contains(s); });
}

int day5(line_view file) {
  int total = 0;
  const char* subs[] = {"ab", "cd", "pq", "xy"};
  per_line(file, [&subs, &total](line_view lv) {
    auto b1 = count_vowels(lv, "aeiou") >= 3;
    auto b2 = is_nice(lv, 2);
    auto b3 = is_nice(lv, subs, ARRAY_SIZE(subs));
    if (b1 && b2 && b3) {
      total += 1;
    }
    return true;
  });
  return total;
}

} // namespace aoc2015