aboutsummaryrefslogtreecommitdiff
path: root/src/2020/day8/aoc.cpp
blob: 61702150766335e9d20445b52760a15b21e13db9 (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
#include "aoc.h"
#include <vector>

namespace aoc2020 {

static int run(size_t index, std::vector<code> cs, int* accumlator) {
  if (index < cs.size()) {
    auto& code = cs[index];
    if (code.executed == 0) {
      size_t next = index + 1;
      if (code.type == acc) {
        *accumlator += code.value;
      }
      if (code.type == jmp) {
        next = index + code.value;
      }
      code.executed += 1;
      return run(next, cs, accumlator);
    }
    return 1;
  }
  return 0;
}

std::pair<int, int> day8(line_view file) {
  std::vector<code> cs;
  int acc0 = 0;
  per_line(file, [&cs](line_view lv) {
    cs.emplace_back(lv);
    return true;
  });
  run(0, cs, &acc0);

  auto reset = [](code& c) {
    c.type = (code_t)(!(int)c.type);
  };
  int acc1 = 0;
  for(auto& code: cs) {
    if (code.type != acc) {
      reset(code);
      acc1 = 0;
      if (run(0, cs, &acc1) == 0) {
        break;
      }
      reset(code);
    }
  }
  return {acc0, acc1};
}

} // namespace aoc2020