aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day23/aoc.cpp
blob: 1b5a9e63dda9d5c1a2ccb1bbc41024eb56e7a301 (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
#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}};

std::vector<elf> round(int i, const std::vector<elf>& elfs, const std::set<elf>& elves) {
  std::vector<elf> next;
  for (auto& e : elfs) {
    if (moves[0].p(e, elves)) {
      next.emplace_back(moves[0].m(e));
    } else {
      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));
          break;
        }
      }
    }
  }

  // check duplicate of next
  return next;
}

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()};

  return {0, 0};
}
} // namespace aoc2022