summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2022-12-11 23:01:22 -0500
committerJune McEnroe <june@causal.agency>2022-12-11 23:01:22 -0500
commit02fccebe9efcfd63b7af61c78ee9e0e9e6de21e7 (patch)
tree64eab9e9de0c2e70bd4034fc61420113a49f8244
parentSolve day 11 part 1 (diff)
downloadaoc-02fccebe9efcfd63b7af61c78ee9e0e9e6de21e7.tar.gz
aoc-02fccebe9efcfd63b7af61c78ee9e0e9e6de21e7.zip
Solve day 11 part 2
-rw-r--r--2022/day11-2.awk62
1 files changed, 62 insertions, 0 deletions
diff --git a/2022/day11-2.awk b/2022/day11-2.awk
new file mode 100644
index 0000000..27a405d
--- /dev/null
+++ b/2022/day11-2.awk
@@ -0,0 +1,62 @@
+BEGIN {
+	FS = "[ :,]+";
+}
+/^Monkey/ {
+	m = $2;
+}
+/Starting items/ {
+	for (i = 4; i <= NF; ++i) {
+		items[m,len[m]++] = $i;
+	}
+}
+/Operation/ {
+	op[m] = $6;
+	const[m] = $7;
+}
+/Test/ {
+	test[m] = $5;
+}
+/If true/ {
+	true[m] = $7;
+}
+/If false/ {
+	false[m] = $7;
+}
+END {
+	mod = 1;
+	for (m = 0; test[m]; ++m) {
+		mod *= test[m];
+	}
+	for (n = 1; n <= 10000; ++n) {
+		for (m = 0; test[m]; ++m) {
+			for (i = 0; i < len[m]; ++i) {
+				insp[m]++;
+				item = items[m,i];
+				delete items[m,i];
+				if (op[m] == "+") {
+					item += const[m];
+				} else if (op[m] == "*" && const[m] != "old") {
+					item *= const[m];
+				} else {
+					item *= item;
+				}
+				item %= mod;
+				if (item % test[m] == 0) {
+					items[true[m],len[true[m]]++] = item;
+				} else {
+					items[false[m],len[false[m]]++] = item;
+				}
+			}
+			len[m] = 0;
+		}
+	}
+	for (m = 0; test[m]; ++m) {
+		if (insp[m] > max1) {
+			max2 = max1;
+			max1 = insp[m];
+		} else if (insp[m] > max2) {
+			max2 = insp[m];
+		}
+	}
+	print max1 * max2;
+}