From 095ddd091e26ca659bf8dde32da7541c7afa4589 Mon Sep 17 00:00:00 2001 From: Stedd Date: Sun, 1 Dec 2024 15:22:23 +0100 Subject: [PATCH] mapped occurrence of each number in column 2 --- days/day01.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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); }