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

namespace aoc2018 {

struct guard {
  int id = 0;
  struct {
    int month;
    int day;
    int hour;
    int minute;
  } timestamp = {0, 0, 0, 0};
  enum {
    on,
    off,
  } status = off;

  std::vector<int> offtime[60];
  std::vector<int> ontime[60];

  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;
  }

  guard(line_view lv) {
    int* ds[] = {&timestamp.month, &timestamp.day, &timestamp.hour, &timestamp.minute, &id};
    const char* p = lv.line + 6;
    int i{0};
    while (p < lv.line + lv.length) {
      if (*p >= '0' && *p <= '9') {
        get_number(&p, ds[i++]);
        if (i == 4 && *(p + 2) == 'w') {
          status = on;
        }
      }
      p++;
    }
    if (id > 0) {
      status = on;
    }
  };
};

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

} // namespace aoc2018