diff options
Diffstat (limited to '22.3')
-rw-r--r-- | 22.3/main.cpp | 50 |
1 files changed, 34 insertions, 16 deletions
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 <algorithm> #include <iostream> #include <string> +#include <cstdint> 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; } |