diff --git a/days/day01.cpp b/days/day01.cpp index 233823a..1ddc224 100644 --- a/days/day01.cpp +++ b/days/day01.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -153,6 +154,7 @@ void day01::Part2() { std::vector col1; std::vector col2; + std::unordered_map occurrence_map; int sum = 0; std::ifstream input("input/day01.txt"); @@ -176,4 +178,21 @@ void day01::Part2() col1.emplace_back(std::stoi(line.substr(0, delimiterPos))); col2.emplace_back(std::stoi(line.substr(delimiterPos + delimiterLength, line.length()))); } + + for (auto number: col2) + { + if (occurrence_map.contains(number)) + { + occurrence_map[number]++; + } else + { + occurrence_map[number] = 1; + } + } + + for (auto number: col1) + { + sum += number * occurrence_map[number]; + } + printf("Sum of all occurrence pairs is: %d\n", sum); }