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

namespace aoc2020 {

static void run(size_t index, std::vector<code>& cs, int* accumlator) {
  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;
    run(next, cs, accumlator);
  }
}

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

  run(0, cs, &accumlator);
  return accumlator;
}

} // namespace aoc2020