From 7ce14487c506b7086d1cac7e16918551e1859883 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sat, 3 Dec 2022 07:42:08 +0100 Subject: day 3 part 2 --- 22.3/main.cpp | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) (limited to '22.3') diff --git a/22.3/main.cpp b/22.3/main.cpp index c648d84..9827909 100644 --- a/22.3/main.cpp +++ b/22.3/main.cpp @@ -1,5 +1,7 @@ +#include #include #include +#include using namespace std; int @@ -14,24 +16,40 @@ getPriority(char c) int main() { - long total = 0; - for (string line; getline(cin, line); ) { - bool inFirstHalf[52] = {0}; - if (line.length() % 2 != 0) throw "unexpected input"; - int found = -1; - for (int i = 0; i < line.length(); i++) { - int p = getPriority(line[i]); - if (i < line.length() / 2) { - inFirstHalf[p-1] = true; - } else { - if (inFirstHalf[p-1]) { - found = p; - break; + long total1 = 0; + long total2 = 0; + string line; // don't reallocate + while (!cin.eof()) { + uint8_t masks[52] = {0}; + for (int i = 0; i < 3; i++) { + bool inFirstHalf[52] = {0}; + int found = -1; + + if (!getline(cin, line)) break; + if (line.length() % 2 != 0) throw "unexpected input"; + + for (int j = 0; j < line.length(); j++) { + int p = getPriority(line[j]); + masks[p-1] = masks[p-1] | (1 << i); + if (j < line.length() / 2) { + inFirstHalf[p-1] = true; + } else { + if (inFirstHalf[p-1]) { + if (found != -1 && found != p) + throw "bad input"; + found = p; + } } } + if (found == -1) throw "bad input"; + total1 += found; + } + if (!cin.eof()) { + if (count(begin(masks), end(masks), 7) != 1) + throw "bad input"; + total2 += find(begin(masks), end(masks), 7) - begin(masks) + 1; } - if (found == -1) throw "bad input"; - total += found; } - cout << total << endl; + cout << total1 << endl; + cout << total2 << endl; } -- cgit 1.4.1-2-gfad0