diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-02-08 15:57:49 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-02-08 15:57:49 +0800 |
commit | f16c630fad1481f7097b4c514739e15b2cddb334 (patch) | |
tree | 619f99381d584395bc824743dbbf9a47dd226253 /src/2017/day12/aoc.cpp | |
parent | 6700ffdab424d6f238431efbd6094224f14c8379 (diff) | |
download | advent-of-code-f16c630fad1481f7097b4c514739e15b2cddb334.tar.gz advent-of-code-f16c630fad1481f7097b4c514739e15b2cddb334.zip |
2017 day12
Diffstat (limited to 'src/2017/day12/aoc.cpp')
-rw-r--r-- | src/2017/day12/aoc.cpp | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/src/2017/day12/aoc.cpp b/src/2017/day12/aoc.cpp index 30f0501..0323e71 100644 --- a/src/2017/day12/aoc.cpp +++ b/src/2017/day12/aoc.cpp @@ -2,5 +2,71 @@ namespace aoc2017 { -std::pair<int64_t, int64_t> day12(line_view) { return {0, 0}; } +static void get_number(const char** pp, int* d) { + const char* p = *pp; + while (*p >= '0' && *p <= '9') { + *d = *d * 10 + *p - '0'; + p++; + } + *pp = p; +} + +struct record12 { + int ns[10] = {0}; + bool connected = false; + + void print() const noexcept { + for (auto n : ns) { + printf("%d ", n); + } + printf("\n"); + } + + record12(line_view lv) { + const char* p = lv.line; + int i{0}; + while (p < lv.line + lv.length) { + if (*p >= '0' && *p <= '9') { + get_number(&p, &ns[i++]); + } + p++; + } + } +}; + +void mark_connected(size_t i, std::vector<record12>& rs, int* total) { + auto& r = rs[i]; + if (!r.connected) { + *total += 1; + r.connected = true; + for (int x = 1; x < 10; x++) { + if (r.ns[x] > 0) { + mark_connected(r.ns[x], rs, total); + } + } + } +} + +std::pair<int64_t, int64_t> day12(line_view file) { + std::vector<record12> rs; + per_line(file, [&rs](line_view lv) { + rs.emplace_back(lv); + return true; + }); + + int total{0}; + mark_connected(0, rs, &total); + int t0 = total; + int t1 = 1; + + for (size_t i = 1; i < rs.size() && total < (int)rs.size(); i++) { + int tx = total; + mark_connected(i, rs, &total); + if (total > tx) { + t1 += 1; + } + } + + return {t0, t1}; +} } // namespace aoc2017 |