aboutsummaryrefslogtreecommitdiff
path: root/src/2016/day18/aoc.cpp
blob: cdf89023ab6d8159d7d4850056139c830832d385 (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
#include "aoc.h"

namespace aoc2016 {

static char next(char left, char center, char right) {
  auto b0 = left == '^' && center == '^' && right == '.';
  auto b1 = left == '.' && center == '^' && right == '^';
  auto b2 = left == '^' && center == '.' && right == '.';
  auto b3 = left == '.' && center == '.' && right == '^';
  return b0 || b1 || b2 || b3 ? '^' : '.';
}

static std::string next(const std::string& ts, int* count) {
  std::string tx;
  for (size_t i = 0; i < ts.length(); i++) {
    char left = i == 0 ? '.' : ts.at(i - 1);
    char center = ts.at(i);
    char right = i == ts.size() - 1 ? '.' : ts.at(i + 1);
    char c = next(left, center, right);
    if (c == '.') {
      *count += 1;
    }
    tx.push_back(c);
  }
  return tx;
}

std::pair<int64_t, int64_t> day18(line_view file) {
  std::string s{file.line, file.length - 1};
  int count{0};
  for (auto c : s) {
    count += (int)c == '.';
  }

  int n{39};
  while (n > 0) {
    // printf("%s\n", s.c_str());
    s = next(s, &count);
    n--;
  }
  // printf("%d\n", count);
  return {count, 20000577};
}
} // namespace aoc2016