AOC2023: Use quick sort in solution

This commit is contained in:
Bananymous 2023-12-07 11:54:54 +02:00
parent 6caa9b6f95
commit 669d55707e
1 changed files with 6 additions and 15 deletions

View File

@ -1,6 +1,6 @@
#include <BAN/Vector.h> #include <BAN/Vector.h>
#include <BAN/Sort.h>
#include <BAN/String.h> #include <BAN/String.h>
#include <BAN/Swap.h>
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
@ -116,24 +116,15 @@ i64 puzzle(FILE* fp, bool joker)
)); ));
} }
// Very slow sorting algorithm BAN::sort_quick(hands.begin(), hands.end(),
for (size_t i = 0; i < hands.size(); i++) [joker] (const Hand& lhs, const Hand& rhs) {
{ return hand_score(lhs, joker) < hand_score(rhs, joker);
i64 l_score = hand_score(hands[i], joker);
for (size_t j = i + 1; j < hands.size(); j++)
{
i64 r_score = hand_score(hands[j], joker);
if (r_score > l_score)
{
BAN::swap(hands[i], hands[j]);
l_score = r_score;
}
} }
} );
i64 score = 0; i64 score = 0;
for (size_t i = 0; i < hands.size(); i++) for (size_t i = 0; i < hands.size(); i++)
score += (hands.size() - i) * hands[i].bid; score += (i + 1) * hands[i].bid;
return score; return score;
} }