From 3d44c7c416e2fc7b4b62877b5ac7a5fd424e1a30 Mon Sep 17 00:00:00 2001 From: Stedd Date: Sun, 1 Dec 2024 17:28:18 +0100 Subject: [PATCH] Using the filestream to run the parser loop directly Also removed col2 variable and calculating occurrence_map during parsing. --- days/day01.cpp | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/days/day01.cpp b/days/day01.cpp index 6916900..a5f7b90 100644 --- a/days/day01.cpp +++ b/days/day01.cpp @@ -153,7 +153,6 @@ Once again consider your left and right lists. What is their similarity score? void day01::Part2() { std::vector col1; - std::vector col2; std::unordered_map occurrence_map; int sum = 0; @@ -168,21 +167,14 @@ void day01::Part2() return; } - std::string delimiter = " "; - auto delimiterLength = delimiter.length(); - std::string line; - while (std::getline(input, line)) + int num1, num2; + while (input >> num1 >> num2) { - auto delimiterPos = line.find(delimiter); - col1.emplace_back(std::stoi(line.substr(0, delimiterPos))); - col2.emplace_back(std::stoi(line.substr(delimiterPos + delimiterLength, line.length()))); + col1.emplace_back(num1); + occurrence_map[num2]++; } - for (auto number: col2) - { - occurrence_map[number]++; - } for (auto number: col1) { sum += number * occurrence_map[number];