aboutsummaryrefslogtreecommitdiff
path: root/src/2016/day1/aoc.cpp
blob: 114c2901b6ace0436151c0e911836f9cf9ac8304 (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
#include "aoc.h"

namespace aoc2016 {

instruction parse_day1(const char** pp) {
  instruction i;
  const char* p = *pp;
  i.direction = (*p++) == 'R' ? instruction::right : instruction::left;
  while (*p >= '0' && *p <= '9') {
    i.distance = i.distance * 10 + *p - '0';
    p++;
  }

  *pp = p;
  return i;
}

std::pair<int, int> day1(line_view file) {
  position x{position::north, 0, 0};
  std::vector<instruction> is;

  bool found = false;
  position first{position::north, 0, 0};

  const char* p1 = file.line;
  const char* p2 = file.line + file.length;
  while (p1 < p2) {
    if (*p1 == 'R' || *p1 == 'L') {
      is.emplace_back(parse_day1(&p1));
    } else {
      p1++;
    }
  }
  position n = x.move(is, &first, &found);
  return {x.blocks(n), x.blocks(first)};
}

} // namespace aoc2016