From e3098c6651fd2ffa488b74e1c93349d8ef65df9b Mon Sep 17 00:00:00 2001 From: Feiko Wielsma Date: Thu, 4 Dec 2025 12:54:27 +0000 Subject: [PATCH] D3P2 --- day3/main.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++----- day3/test_input | 4 +-- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/day3/main.cpp b/day3/main.cpp index c2a523d..aab3028 100644 --- a/day3/main.cpp +++ b/day3/main.cpp @@ -76,15 +76,77 @@ auto countJoltages(const std::vector>& banks) { return totalJoltage; } +auto calculateJoltageP2(const std::vector& bank) -> long long{ + const int TO_TAKE = 12; + std::vector takenNumbers{}; + + auto curIt = bank.begin(); + auto rightIt = bank.end(); + + auto leftBound = bank.begin(); + + while(takenNumbers.size() != TO_TAKE){ + + // First find biggest number with that is not in the last 12 + while(curIt != (rightIt +1 - TO_TAKE + takenNumbers.size())){ + if(*curIt > *leftBound ){ + leftBound = curIt; + } + curIt++; + } + takenNumbers.push_back(*leftBound); + + curIt = leftBound+1; + leftBound = curIt; + } + + long long intRepr = std::ranges::fold_left(takenNumbers, 0, [](long long x, long long y) -> long long{ + return (x*10) + y; + }); + return intRepr; +} + +auto calculateJoltageP2_Optimized(const std::vector& bank) -> long long { + const int TO_TAKE = 12; + + if (bank.size() < TO_TAKE) { return 0; } + + long long result = 0; + auto current_start = bank.begin(); + + for (int i = 0; i < TO_TAKE; ++i) { + + + int items_needed_after_this = (TO_TAKE - 1) - i; + auto search_end = bank.end() - items_needed_after_this; + + auto max_it = std::max_element(current_start, search_end); + + result = (result * 10) + *max_it; + + current_start = max_it + 1; + } + + return result; +} + +auto countJoltagesP2(const std::vector>& banks) { + long totalJoltage{}; + for(const auto& bank : banks){ + totalJoltage += calculateJoltageP2_Optimized(bank); + } + return totalJoltage; +} + auto main() -> int { auto testCase = parseBanks("test_input"); if(testCase) { - auto testResult = countJoltages(*testCase); - // auto testResultP2 = countRepeats(*testCase); - std::println("P1 Testcase result: {}", testResult); - // std::println("P2 Testcase result: {}", testResultP2); + //auto testResult = countJoltages(*testCase); + auto testResultP2 = countJoltagesP2(*testCase); + //std::println("P1 Testcase result: {}", testResult); + std::println("P2 Testcase result: {}", testResultP2); } else{ std::print("{}\n", testCase.error()); @@ -93,10 +155,10 @@ auto main() -> int { auto realPuzzle = parseBanks("puzzle_input"); if(realPuzzle) { - auto realResult = countJoltages(*realPuzzle); - // auto realResultP2 = countRepeats(*realRanges); - std::println("P1 Real result: {}", realResult); - // std::println("P2 Real result: {}", realResultP2); + //auto realResult = countJoltages(*realPuzzle); + auto realResultP2 = countJoltagesP2(*realPuzzle); + //std::println("P1 Real result: {}", realResult); + std::println("P2 Real result: {}", realResultP2); } else{ std::print("{}\n", realPuzzle.error()); diff --git a/day3/test_input b/day3/test_input index 6b1df6a..a7353bc 100644 --- a/day3/test_input +++ b/day3/test_input @@ -1,4 +1,4 @@ +818181911112111 987654321111111 811111111111119 -234234234234278 -818181911112111 \ No newline at end of file +234234234234278 \ No newline at end of file