aboutsummaryrefslogtreecommitdiff
path: root/src/2017/day6/aoc.cpp
blob: c77e74fb03cb7fdf2fa0a1a64bc4acd5f3b052d8 (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
#include "aoc.h"
#include <set>

namespace aoc2017 {

void next(int* step, int* gap, memory_bank& m, std::set<memory_bank>& ms) {
  if (ms.find(m) == ms.end()) {
    ms.insert(m);
    *step += 1;
    int d{0};
    int i = m.highest(&d);
    // printf("%d %d %d\n", *step, i, d);
    m.distribute(i, d);
    next(step, gap, m, ms);
  }
  auto it = ms.find(m);
  *gap = *step - it->index;
}

std::pair<int, int> day6(line_view file) {
  std::set<memory_bank> ms;
  memory_bank m{file};
  int steps{0};
  int gap{0};
  next(&steps, &gap, m, ms);
  return {steps, gap};
}

} // namespace aoc2017