From 1407bd20736c7e378d1286e7334de69011702e89 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sat, 3 Dec 2022 07:21:54 +0100 Subject: day 3 part 1 --- 22.3/main.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 22.3/main.cpp diff --git a/22.3/main.cpp b/22.3/main.cpp new file mode 100644 index 0000000..c648d84 --- /dev/null +++ b/22.3/main.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; + +int +getPriority(char c) +{ + // [1;52] + if ('a' <= c && c <= 'z') return c - 'a' + 1; + if ('A' <= c && c <= 'Z') return c - 'A' + 27; + throw "unexpected input"; +} + +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; + } + } + } + if (found == -1) throw "bad input"; + total += found; + } + cout << total << endl; +} -- cgit 1.4.1-2-gfad0