aboutsummaryrefslogtreecommitdiff
path: root/src/2021/day2/aoc.h
blob: 3969caa93ad0c43c063a740fcec45a4c2a6379ed (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
#pragma once

#include "common.h"

namespace aoc2021 {

struct meters {
  int horizontal;
  int depth;

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

  void apply(line_view lv) {
    const char* p = lv.contains(" ");
    int d = get_number(p + 1);
    if (line_view{lv.line, p} == line_view{"forward"}) {
      horizontal += d;
    }
    if (line_view{lv.line, p} == line_view{"down"}) {
      depth += d;
    }
    if (line_view{lv.line, p} == line_view{"up"}) {
      depth -= d;
    }
  }

  void apply(line_view lv, int* aim) {
    const char* p = lv.contains(" ");
    int d = get_number(p + 1);
    if (line_view{lv.line, p} == line_view{"forward"}) {
      horizontal += d;
      depth += *aim * d;
    }
    if (line_view{lv.line, p} == line_view{"down"}) {
      *aim += d;
    }
    if (line_view{lv.line, p} == line_view{"up"}) {
      *aim -= d;
    }
  }
};

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

} // namespace aoc2021