aboutsummaryrefslogtreecommitdiff
path: root/src/2020/day8/aoc.h
blob: b39b1248039001679e202a1162eb20514b02139a (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
#pragma once
#include "common.h"

namespace aoc2020 {

enum code_t {
  nop,
  jmp,
  acc,
};

struct code {
  code_t type;
  int value;
  int executed = 0;

  void get_number(const char** pp, int* d) {
    const char* p = *pp;
    int sign = *p == '+' ? 1 : -1;
    p += 1;
    *d = 0;
    while (*p >= '0' && *p <= '9') {
      *d = *d * 10 + *p - '0';
      p++;
    }
    *d *= sign;
    *pp = p;
  }

  code(line_view lv) {
    const char* p = lv.line;
    if (*p == 'a')
      type = acc;
    if (*p == 'n')
      type = nop;
    if (*p == 'j')
      type = jmp;
    while (p < lv.line + lv.length) {
      if (*p == '+' || *p == '-') {
        get_number(&p, &value);
      }
      p++;
    }
  }
};

std::pair<int, int> day8(line_view);

} // namespace aoc2020