From 669d55707edad1d504d799282cc6bbc85d5a314d Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 7 Dec 2023 11:54:54 +0200 Subject: [PATCH] AOC2023: Use quick sort in solution --- userspace/aoc2023/day7/main.cpp | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/userspace/aoc2023/day7/main.cpp b/userspace/aoc2023/day7/main.cpp index 6b360231b5..addd9655d0 100644 --- a/userspace/aoc2023/day7/main.cpp +++ b/userspace/aoc2023/day7/main.cpp @@ -1,6 +1,6 @@ #include +#include #include -#include #include #include @@ -116,24 +116,15 @@ i64 puzzle(FILE* fp, bool joker) )); } - // Very slow sorting algorithm - for (size_t i = 0; i < hands.size(); i++) - { - 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; - } + BAN::sort_quick(hands.begin(), hands.end(), + [joker] (const Hand& lhs, const Hand& rhs) { + return hand_score(lhs, joker) < hand_score(rhs, joker); } - } + ); i64 score = 0; for (size_t i = 0; i < hands.size(); i++) - score += (hands.size() - i) * hands[i].bid; + score += (i + 1) * hands[i].bid; return score; }