aboutsummaryrefslogtreecommitdiff
path: root/src/2017/day13/aoc.h
blob: fabc93d2fe21fe653fe6140a7f67843b790abe84 (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
#pragma once
#include "common.h"
#include <vector>

namespace aoc2017 {

struct scanner {
  int depth = 0;
  int range = 0;

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

  void print() const noexcept { printf("%d %d\n", depth, range); }

  int at_level(int t) const noexcept {
    t %= 2 * (range - 1);
    return t < range ? t : 2 * (range - 1) - t;
  }

  scanner(line_view lv) {
    int* ds[] = {&depth, &range};
    int i{0};
    const char* p = lv.line;
    while (p < lv.line + lv.length) {
      if (*p >= '0' && *p <= '9') {
        get_number(&p, ds[i++]);
      }
      p++;
    }
  }
};

std::pair<int64_t, int64_t> day13(line_view);
} // namespace aoc2017