summary refs log tree commit diff
path: root/22.3
diff options
context:
space:
mode:
authordzwdz2022-12-03 07:21:54 +0100
committerdzwdz2022-12-03 07:21:54 +0100
commit1407bd20736c7e378d1286e7334de69011702e89 (patch)
tree262671de431156abefaf1e14b549e1df50727e38 /22.3
parent656d6fb7ee07b70d708b9ec922b7d24cf2989617 (diff)
day 3 part 1
Diffstat (limited to '22.3')
-rw-r--r--22.3/main.cpp37
1 files changed, 37 insertions, 0 deletions
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 <iostream>
+#include <string>
+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;
+}