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

namespace aoc2016 {

static void reverse(std::string& s) {
  size_t b = 0;
  size_t e = s.size() - 1;
  while (b < e) {
    std::swap(s.at(b), s.at(e));
    b++;
    e--;
  }
}

static void replace(std::string& s) {
  char s2[] = {'1', '0'};
  for (size_t i = 0; i < s.size(); i++) {
    s.at(i) = s2[(int)s.at(i) - '0'];
  }
}

static std::string take(const std::string& s, size_t n) { return n < s.size() ? std::string{s.data(), n} : s; }

static std::string checksum(const std::string& s) {
  std::string x;
  char s2[] = {'0', '1'};
  for (size_t i = 0; i < s.size() - 1; i += 2) {
    x.push_back(s2[(int)s.at(i) == s.at(i + 1)]);
  }
  return x;
}

std::string checksum(std::string s, size_t max) {
  while (s.length() < max) {
    std::string a{s};
    reverse(a);
    replace(a);
    s.push_back('0');
    s.append(a);
  }
  s = take(s, max);

  auto chk = checksum(s);
  while ((chk.size() & 1) == 0) {
    chk = checksum(chk);
  }
  return chk;
}

std::pair<std::string, std::string> day16(line_view) {
  std::string init{"11110010111001001"};
  return {checksum(init, 272), checksum(init, 35651584)};
}
} // namespace aoc2016