Frans Bergman 2 жил өмнө
parent
commit
3059bbd879
2 өөрчлөгдсөн 110 нэмэгдсэн , 0 устгасан
  1. 55 0
      11.input
  2. 55 0
      11.py

+ 55 - 0
11.input

@@ -0,0 +1,55 @@
+Monkey 0:
+  Starting items: 74, 73, 57, 77, 74
+  Operation: new = old * 11
+  Test: divisible by 19
+    If true: throw to monkey 6
+    If false: throw to monkey 7
+
+Monkey 1:
+  Starting items: 99, 77, 79
+  Operation: new = old + 8
+  Test: divisible by 2
+    If true: throw to monkey 6
+    If false: throw to monkey 0
+
+Monkey 2:
+  Starting items: 64, 67, 50, 96, 89, 82, 82
+  Operation: new = old + 1
+  Test: divisible by 3
+    If true: throw to monkey 5
+    If false: throw to monkey 3
+
+Monkey 3:
+  Starting items: 88
+  Operation: new = old * 7
+  Test: divisible by 17
+    If true: throw to monkey 5
+    If false: throw to monkey 4
+
+Monkey 4:
+  Starting items: 80, 66, 98, 83, 70, 63, 57, 66
+  Operation: new = old + 4
+  Test: divisible by 13
+    If true: throw to monkey 0
+    If false: throw to monkey 1
+
+Monkey 5:
+  Starting items: 81, 93, 90, 61, 62, 64
+  Operation: new = old + 7
+  Test: divisible by 7
+    If true: throw to monkey 1
+    If false: throw to monkey 4
+
+Monkey 6:
+  Starting items: 69, 97, 88, 93
+  Operation: new = old * old
+  Test: divisible by 5
+    If true: throw to monkey 7
+    If false: throw to monkey 2
+
+Monkey 7:
+  Starting items: 59, 80
+  Operation: new = old + 6
+  Test: divisible by 11
+    If true: throw to monkey 2
+    If false: throw to monkey 3

+ 55 - 0
11.py

@@ -0,0 +1,55 @@
+from itertools import zip_longest
+from functools import reduce
+from operator import mul
+
+def grouper(n, iterable, padvalue=None):
+  return zip_longest(*[iter(iterable)]*n, fillvalue=padvalue)
+
+lines = [line.strip() for line in open("11.input")]
+
+def get_monkeys():
+    monkeys = []
+
+    for chunk in grouper(7, lines):
+        items = chunk[1].split(":")
+        items = items[1].strip().split(", ")
+        exp = chunk[2].split("=")[1]
+        monkeys.append({
+            "items": list(map(int, items)),
+            "op": exp,
+            "test": int(chunk[3].split(" ")[-1]),
+            "true": int(chunk[4].split(" ")[-1]),
+            "false": int(chunk[5].split(" ")[-1]),
+            "inspects": 0,
+            })
+    return monkeys
+
+
+def run_part(part, rounds, monkeys):
+    all_prod = reduce(mul, [m["test"] for m in monkeys])
+
+    for _ in range(0, rounds):
+        for monke in monkeys:
+            for item in monke["items"]:
+                old = item
+                new = eval(monke["op"])
+                monke["inspects"] += 1
+                if part == 1:
+                    new = new // 3
+                else:
+                    new %= all_prod
+                if new % monke["test"] == 0:
+                    target = monke["true"]
+                else:
+                    target = monke["false"]
+                monkeys[target]["items"].append(new)
+
+            monke["items"] = []
+
+
+    inspects = [m["inspects"] for m in monkeys]
+    inspects.sort()
+    print(inspects[-1] * inspects[-2])
+
+run_part(1, 20, get_monkeys())
+run_part(2, 10000, get_monkeys())