aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day23/aoc.cpp
blob: 1b09a2b3398bbe7c4241c3f4b4f890f404a5b195 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "aoc.h"
#include <map>
#include <set>

namespace aoc2022 {
typedef elf (*elf_move)(elf);
typedef bool (*elf_predicate)(elf, const std::set<elf>&);

static elf nomove(elf e) { return e; }
static elf north(elf e) { return elf{e.x, e.y - 1}; }
static elf south(elf e) { return elf{e.x, e.y + 1}; }
static elf west(elf e) { return elf{e.x - 1, e.y}; }
static elf east(elf e) { return elf{e.x + 1, e.y}; }

bool eight(elf e, const std::set<elf>& elves) {
  elf es[8] = {
      {e.x - 1, e.y - 1}, {e.x, e.y - 1},     {e.x + 1, e.y - 1}, {e.x - 1, e.y},
      {e.x + 1, e.y},     {e.x - 1, e.y + 1}, {e.x, e.y + 1},     {e.x + 1, e.y + 1},
  };
  for (auto& e : es) {
    if (elves.find(e) != elves.end()) {
      return false;
    }
  }
  return true;
}

bool north_three(elf e, const std::set<elf>& elves) {
  elf es[3] = {
      {e.x - 1, e.y - 1},
      {e.x, e.y - 1},
      {e.x + 1, e.y - 1},
  };
  for (auto& e : es) {
    if (elves.find(e) != elves.end()) {
      return false;
    }
  }
  return true;
}

bool south_three(elf e, const std::set<elf>& elves) {
  elf es[3] = {
      {e.x - 1, e.y + 1},
      {e.x, e.y + 1},
      {e.x + 1, e.y + 1},
  };
  for (auto& e : es) {
    if (elves.find(e) != elves.end()) {
      return false;
    }
  }
  return true;
}

bool west_three(elf e, const std::set<elf>& elves) {
  elf es[3] = {
      {e.x - 1, e.y - 1},
      {e.x - 1, e.y},
      {e.x - 1, e.y + 1},
  };
  for (auto& e : es) {
    if (elves.find(e) != elves.end()) {
      return false;
    }
  }
  return true;
}

bool east_three(elf e, const std::set<elf>& elves) {
  elf es[3] = {
      {e.x + 1, e.y - 1},
      {e.x + 1, e.y},
      {e.x + 1, e.y + 1},
  };
  for (auto& e : es) {
    if (elves.find(e) != elves.end()) {
      return false;
    }
  }
  return true;
}

static struct elf_test {
  elf_predicate p;
  elf_move m;
} moves[5] = {{eight, nomove}, {north_three, north}, {south_three, south}, {west_three, west}, {east_three, east}};

bool duplicate(elf e, const std::map<elf, int>& dups) {
  auto it = dups.find(e);
  return it != dups.end() && it->second > 1;
}

std::vector<elf> round(int i, const std::vector<elf>& elfs, const std::set<elf>& elves, size_t* n) {
  std::vector<elf> next;
  *n = 0;

  for (auto& e : elfs) {
    if (moves[0].p(e, elves)) {
      next.emplace_back(moves[0].m(e));
      *n += 1;
    } else {
      bool can_move{false};
      for (int x = 0; x < 4; x++) {
        int index = (i + x) % 4 + 1;
        if (moves[index].p(e, elves)) {
          next.emplace_back(moves[index].m(e));
          can_move = true;
          break;
        }
      }
      if (!can_move) {
        next.emplace_back(e);
      }
    }
  }

  std::map<elf, int> dups;
  for (auto& e : next) {
    auto kv = dups.insert({e, 1});
    if (!kv.second) {
      kv.first->second += 1;
    }
  }

  // check duplicate of next
  for(size_t i = 0; i < next.size(); i++) {
    if (duplicate(next[i], dups)) {
      next[i] = elfs[i]; // stay put
    }
  }
  return next;
}

int count(const std::vector<elf>& elfs, const std::set<elf>& elvs) {
  int minx{INT32_MAX}, miny{INT32_MAX};
  int maxx{INT32_MIN}, maxy{INT32_MIN};

  for (auto& e: elfs) {
    minx = std::min(e.x, minx);
    miny = std::min(e.y, miny);
    maxx = std::max(e.x, maxx);
    maxy = std::max(e.y, maxy);
  }

  int n{0};
  for (int y = miny; y <= maxy; y++) {
    for (int x = minx; x <= maxx; x++) {
      if (elvs.find(elf{x, y}) == elvs.end()) n++;
    }
  }
  return n;
}

std::pair<int, int> day23(line_view file) {
  int height{0};
  std::vector<elf> elfs;

  per_line(file, [&height, &elfs](line_view lv) {
    for (size_t i = 0; i < lv.length; i++) {
      if (*(lv.line + i) == '#') {
        elfs.emplace_back(elf{(int)i, height});
      }
    }
    height++;
    return true;
  });

  std::set<elf> elves{elfs.begin(), elfs.end()};

  size_t n{0};
  int r{0};
  while (r < 10 /*true*/) {
    elfs = round(r++, elfs, elves, &n);
    if (n == elfs.size()) break;
    elves.clear();
    elves = std::set<elf>{elfs.begin(), elfs.end()};
  }

  int n1 = count(elfs, elves);
  return {n1, r};
}
} // namespace aoc2022