D3P2
This commit is contained in:
parent
1fe4ecca8a
commit
e3098c6651
2 changed files with 72 additions and 10 deletions
|
|
@ -76,15 +76,77 @@ auto countJoltages(const std::vector<std::vector<int>>& banks) {
|
|||
return totalJoltage;
|
||||
}
|
||||
|
||||
auto calculateJoltageP2(const std::vector<int>& bank) -> long long{
|
||||
const int TO_TAKE = 12;
|
||||
std::vector<int> 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<int>& 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<std::vector<int>>& 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());
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
818181911112111
|
||||
987654321111111
|
||||
811111111111119
|
||||
234234234234278
|
||||
818181911112111
|
||||
Loading…
Add table
Add a link
Reference in a new issue