summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-rw-r--r--2022/day11-1.awk58
1 files changed, 58 insertions, 0 deletions
diff --git a/2022/day11-1.awk b/2022/day11-1.awk
new file mode 100644
index 0000000..97a8148
--- /dev/null
+++ b/2022/day11-1.awk
@@ -0,0 +1,58 @@
+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 {
+	for (n = 1; n <= 20; ++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 = int(item/3);
+				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;
+}
NoFlowJune McEnroe 2019-02-22Move IRC formatting reset to C-sJune McEnroe 2019-02-22Disable terminal flow controlJune McEnroe 2019-02-22Bind up and down arrows to scrollJune McEnroe 2019-02-22Remove topic TODOJune McEnroe 2019-02-22Add /znc commandJune McEnroe 2019-02-22Update status line after scrolling and term eventsJune McEnroe 2019-02-22Reorganize input.cJune McEnroe 2019-02-22Fix name of <raw> window in man pageJune McEnroe 2019-02-22Rename global tags with angle bracketsJune McEnroe 2019-02-22Show status window while connectingJune McEnroe 2019-02-22Reorganize UI code for the umpteenth timeJune McEnroe 2019-02-21Replace "view" with "window"June McEnroe 2019-02-21Remove ROT13June McEnroe 2019-02-21Clean up man pageJune McEnroe 2019-01-26Draw UI before connectingJune McEnroe 2019-01-25Avoid unused variable warnings with getyxJune McEnroe 2019-01-25Add GNU/Linux build instructionsJune McEnroe