From b6a7ce896a8cabd6b070d2ded1e88ef252c39150 Mon Sep 17 00:00:00 2001 From: Stedd Date: Sun, 1 Dec 2024 14:59:55 +0100 Subject: [PATCH] added part 2 info --- days/day01.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/days/day01.cpp b/days/day01.cpp index ebc0e1a..fc43c10 100644 --- a/days/day01.cpp +++ b/days/day01.cpp @@ -109,5 +109,72 @@ void day01::Part1() { sum += std::abs(col1[i] - col2[i]); } - std::cout << sum << '\n'; +} + +/* +--- Part Two --- + +Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different. + +Or are they? + +The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, +but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other +numbers aren't location IDs at all but rather misinterpreted handwriting. + +This time, you'll need to figure out exactly how often each number from the left list appears in the right +list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number +of times that number appears in the right list. + +Here are the same example lists again: + +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 + +For these example lists, here is the process of finding the similarity score: + + The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9. + The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4. + The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0). + The fourth number, 1, also does not appear in the right list. + The fifth number, 3, appears in the right list three times; the similarity score increases by 9. + The last number, 3, appears in the right list three times; the similarity score again increases by 9. + +So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9). + +Once again consider your left and right lists. What is their similarity score? + + */ + +void day01::Part2() +{ + std::vector col1; + std::vector col2; + int sum = 0; + + std::ifstream input("input/day01.txt"); + //std::ifstream input("input/day01short.txt"); + + if (!input) + { + std::cerr << "Failed to open input file." << std::endl; + std::cout << "Current path is " << std::filesystem::current_path() << '\n'; + + return; + } + + std::string delimiter = " "; + auto delimiterLength = delimiter.length(); + + std::string line; + while (std::getline(input, line)) + { + 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()))); + } }