From b2503f5ebed4da42950b8e02cb03e0b9950560e9 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Thu, 1 Dec 2022 20:08:55 +0100 Subject: day 1 part 2 --- 22.1/main.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to '22.1') diff --git a/22.1/main.cpp b/22.1/main.cpp index 4a33aca..5f7aea6 100644 --- a/22.1/main.cpp +++ b/22.1/main.cpp @@ -1,21 +1,29 @@ #include +#include #include #include using namespace std; class Solution { - long best = -1; + array best = {0}; long curtotal = 0; -public: +public: void addItem(long cals) { curtotal += cals; } void endElf() { - best = max(best, curtotal); + if (best[0] < curtotal) { + best[0] = curtotal; + // i wonder if the compiler is smart enough to skip the second if + // would honestly be pretty terrifying + if (best[0] > best[1]) swap(best[0], best[1]); + if (best[1] > best[2]) swap(best[1], best[2]); + } curtotal = 0; } - long getAnswer() { return best; } + long getPartOne() { return best[2]; } + long getPartTwo() { return best[0] + best[1] + best[2]; } }; @@ -31,5 +39,6 @@ main() } } sol.endElf(); - cout << sol.getAnswer() << endl; + cout << sol.getPartOne() << endl; + cout << sol.getPartTwo() << endl; } -- cgit 1.4.1-2-gfad0