diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-01-04 22:36:34 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-01-04 22:36:34 +0800 |
commit | b034ad46b9f950e7934935f6f6f4129a78c2ee09 (patch) | |
tree | 108fd122e860ec2872a27ebc82bc7496b4b7f5c3 /src/2022/day23/aoc.cpp | |
parent | 9c1816983af7da0ac5391c5c9010fc0985543353 (diff) | |
download | advent-of-code-b034ad46b9f950e7934935f6f6f4129a78c2ee09.tar.gz advent-of-code-b034ad46b9f950e7934935f6f6f4129a78c2ee09.zip |
2022 day23 elf
Diffstat (limited to 'src/2022/day23/aoc.cpp')
-rw-r--r-- | src/2022/day23/aoc.cpp | 102 |
1 files changed, 100 insertions, 2 deletions
diff --git a/src/2022/day23/aoc.cpp b/src/2022/day23/aoc.cpp index 825e67a..48a2099 100644 --- a/src/2022/day23/aoc.cpp +++ b/src/2022/day23/aoc.cpp @@ -1,9 +1,107 @@ #include "aoc.h" +#include <map> +#include <set> namespace aoc2022 { +typedef elf (*elf_move)(elf); +typedef bool (*elf_predicate)(elf, const std::set<elf>&); -std::pair<int, int> day23(line_view) { - return {0, 0}; +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; +} + +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::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 |