summary refs log tree commit diff
path: root/22.21
diff options
context:
space:
mode:
Diffstat (limited to '22.21')
-rw-r--r--22.21/main.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/22.21/main.py b/22.21/main.py
new file mode 100644
index 0000000..2ac7c15
--- /dev/null
+++ b/22.21/main.py
@@ -0,0 +1,24 @@
+from sys import stdin
+
+monkeys = {}
+
+for line in stdin:
+	out = line[:4]
+	parts = line[6:].split()
+	if len(parts) == 1: parts = int(parts[0])
+	monkeys[out] = parts
+
+def get(k):
+	m = monkeys[k]
+	if type(m) != int:
+		a = get(m[0])
+		b = get(m[2])
+		op = m[1]
+		if op == '+': m = a + b
+		if op == '-': m = a - b
+		if op == '*': m = a * b
+		if op == '/': m = a // b
+		monkeys[k] = m
+	return m
+
+print(get('root'))