diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-17 10:49:39 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-17 10:49:39 +0800 |
commit | 8a71f9dedc7f74fb31e8c00991c928f1ab44303f (patch) | |
tree | 8e9eb51226b9ba78b47ca5fe9c610c4fcd950c62 /src/2019/day6/aoc.cpp | |
parent | 096c7e2226bfd45a98a4f3aae25f6394cb89cd22 (diff) | |
download | advent-of-code-8a71f9dedc7f74fb31e8c00991c928f1ab44303f.tar.gz advent-of-code-8a71f9dedc7f74fb31e8c00991c928f1ab44303f.zip |
2019 day6 part1
Diffstat (limited to 'src/2019/day6/aoc.cpp')
-rw-r--r-- | src/2019/day6/aoc.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/2019/day6/aoc.cpp b/src/2019/day6/aoc.cpp index f0cbc1b..e0a8cac 100644 --- a/src/2019/day6/aoc.cpp +++ b/src/2019/day6/aoc.cpp @@ -1,5 +1,49 @@ #include "aoc.h" +#include <unordered_map> +#include <vector> namespace aoc2019 { +star* find(std::unordered_map<line_view, star*>& stars, line_view lv) { + auto it = stars.find(lv); + star* s = nullptr; + if (it == stars.end()) { + s = new star{lv}; + stars.insert({lv, s}); + } else { + s = it->second; + } + // std::cout << s->name << std::endl; + return s; } + +void count(star* s, std::vector<star*>& vs, int* total) { + std::cout << s->name << std::endl; + if (s->orbits.size() > 0) { + std::vector<star*> orbits{s->orbits.begin(), s->orbits.end()}; + for (auto& x : orbits) { + vs.push_back(x); + count(x, vs, total); + vs.pop_back(); + } + } + *total += vs.size(); +} + +int day6(line_view file) { + std::unordered_map<line_view, star*> stars; + per_line(file, [&stars](line_view lv) { + const char* p = lv.contains(")"); + star* s1 = find(stars, line_view{lv.line, p}); + star* s2 = find(stars, line_view{p + 1, lv.line + lv.length - 1}); + s1->orbits.insert(s2); + return true; + }); + + int total{0}; + std::vector<star*> vs; + count(stars["COM"], vs, &total); + return total; +} + +} // namespace aoc2019 |