diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-07 22:53:41 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-07 22:53:41 +0800 |
commit | 83781c309420cd52c869190055cc47c971122eae (patch) | |
tree | 29af4e3ce916bce6905775f4979b83a03134f231 /src/2016/day4/aoc.cpp | |
parent | 0b12c5c0ebe57f12c35b53ede39674b5f08f2459 (diff) | |
download | advent-of-code-83781c309420cd52c869190055cc47c971122eae.tar.gz advent-of-code-83781c309420cd52c869190055cc47c971122eae.zip |
2016 day4
Diffstat (limited to 'src/2016/day4/aoc.cpp')
-rw-r--r-- | src/2016/day4/aoc.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/2016/day4/aoc.cpp b/src/2016/day4/aoc.cpp index 55f4247..895725d 100644 --- a/src/2016/day4/aoc.cpp +++ b/src/2016/day4/aoc.cpp @@ -1,5 +1,77 @@ #include "aoc.h" +#include <algorithm> +#include <string.h> +#include <vector> namespace aoc2016 { +struct ccount { + char c; + int count; + + bool operator<(const ccount& other) const noexcept { + return count > other.count ? true : (count < other.count ? false : c < other.c); + }; +}; + +unsigned char shift(unsigned char c, int times) { + if (c == '-') { + return ' '; + } + times %= 26; + c += times; + if (c > 'z') { + c = 'a' + c - 'z' - 1; + } + return c; } + +bool is_real_room(line_view lv, int* id, char names[]) { + const char* p = lv.line; + while (!(*p >= '0' && *p <= '9')) { + p++; + } + const char* p0 = p; + + ascii_count ac{{lv.line, p}}; + std::vector<ccount> cc; + for (char i = 'a'; i <= 'z'; i++) { + cc.push_back({i, ac.count[int(i)]}); + } + std::sort(cc.begin(), cc.end()); + + while (*p >= '0' && *p <= '9') { + *id = *id * 10 + *p - '0'; + p++; + } + int x{0}; + for (const char* p1 = lv.line; p1 != p0; p1++) { + names[x++] = shift(*p1, *id); + } + for (int i = 0; i < 5; i++) { + if (*(p + i + 1) != cc[i].c) { + return false; + } + } + return true; +} + +std::pair<int, int> day4(line_view file) { + int total{0}; + int pole_id{0}; + per_line(file, [&total, &pole_id](line_view lv) { + int id{0}; + char names[100] = {0}; + if (is_real_room(lv, &id, names)) { + // printf("%d %s\n", id, names); + if (strstr(names, "northpole")) { + pole_id = id; + } + total += id; + } + return true; + }); + return {total, pole_id}; +} + +} // namespace aoc2016 |