summary refs log tree commit diff
path: root/22.3/main.cpp
diff options
context:
space:
mode:
authordzwdz2022-12-03 07:42:08 +0100
committerdzwdz2022-12-03 07:42:08 +0100
commit7ce14487c506b7086d1cac7e16918551e1859883 (patch)
treeeb0289f217fb2fef072099623fcc430392162a44 /22.3/main.cpp
parent1407bd20736c7e378d1286e7334de69011702e89 (diff)
day 3 part 2
Diffstat (limited to '22.3/main.cpp')
-rw-r--r--22.3/main.cpp50
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;
 }