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

namespace aoc2016 {

size_t position_at(int t, int slot, size_t p) { return t > 0 ? (t % slot + p) % slot : p; }

struct disc {
  int slots;
  size_t p;
};

static bool same(size_t* ps, int x) {
  for (int i = 0; i < x; i++) {
    if (ps[i] != ps[x]) {
      return false;
    }
  }
  return true;
}

int first_catch(disc ds[7], int x) {
  int t{0};
  while (true) {
    size_t ps[7] = {0, 0, 0, 0, 0, 0, 0};
    for (size_t i = 0; i < 7; i++) {
      ps[i] = position_at(t + i + 1, ds[i].slots, ds[i].p);
    }
    if (same(ps, x)) {
      return t;
    }
    t++;
  }
}

std::pair<int64_t, int64_t> day15(line_view) {
  disc ds[7] = {{17, 1}, {7, 0}, {19, 2}, {5, 0}, {3, 0}, {13, 5}, {11, 0}};
  return {first_catch(ds, 5), first_catch(ds, 6)};
}
} // namespace aoc2016