summary refs log tree commit diff homepage
path: root/2022
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--2022/day03.awk17
1 files changed, 15 insertions, 2 deletions
diff --git a/2022/day03.awk b/2022/day03.awk
index 6a4a153..8a48d69 100644
--- a/2022/day03.awk
+++ b/2022/day03.awk
@@ -4,14 +4,27 @@ function common(a, b) {
 		if (index(b, as[i])) return as[i];
 	}
 }
+function common3(a, b, c) {
+	split(a, as, "");
+	for (i in as) {
+		if (index(b, as[i]) && index(c, as[i])) return as[i];
+	}
+}
 function priority(x) {
 	return index("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", x);
 }
 {
 	a = substr($1, 1, length($1)/2);
 	b = substr($1, length($1)/2 + 1);
-	sum += priority(common(a, b));
+	sum1 += priority(common(a, b));
+	g1 = g2;
+	g2 = g3;
+	g3 = $1;
+	if (NR % 3 == 0) {
+		sum2 += priority(common3(g1, g2, g3));
+	}
 }
 END {
-	print sum;
+	print sum1;
+	print sum2;
 }