summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2022-12-04 18:00:16 -0500
committerJune McEnroe <june@causal.agency>2022-12-04 18:00:16 -0500
commit61c244e84dea782f17c2e7377c405e0ac4198cfc (patch)
treea0749a37290e623017777b34cc3a26a0d3c0b403
parentSolve day 2 part 2 (diff)
downloadaoc-61c244e84dea782f17c2e7377c405e0ac4198cfc.tar.gz
aoc-61c244e84dea782f17c2e7377c405e0ac4198cfc.zip
Solve day 3 part 1
-rw-r--r--2022/day03.awk17
1 files changed, 17 insertions, 0 deletions
diff --git a/2022/day03.awk b/2022/day03.awk
new file mode 100644
index 0000000..6a4a153
--- /dev/null
+++ b/2022/day03.awk
@@ -0,0 +1,17 @@
+function common(a, b) {
+	split(a, as, "");
+	for (i in as) {
+		if (index(b, 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));
+}
+END {
+	print sum;
+}