瀏覽代碼

Solve day 11 part 1 and maybe 2

Jonatan Gezelius 3 年之前
父節點
當前提交
30fc55d7d2
共有 4 個文件被更改,包括 342 次插入0 次删除
  1. 127 0
      2022/11/a.py
  2. 133 0
      2022/11/b.py
  3. 27 0
      2022/11/example.txt
  4. 55 0
      2022/11/infile.txt

+ 127 - 0
2022/11/a.py

@@ -0,0 +1,127 @@
+import os, argparse
+
+DEBUG = False
+#DEBUG = True
+
+def debug_print(s):
+    if DEBUG:
+        print(f"DEBUG: {s}")
+
+def solve_task(lines):
+    monkeys = []
+    current_monkey = []
+    for line in lines:
+        if len(line) == 0:
+            monkeys.append(Monkey.from_line(current_monkey))
+            current_monkey = []
+        else:
+            current_monkey.append(line)
+    if(len(current_monkey) != 0):
+        monkeys.append(Monkey.from_line(current_monkey))
+
+    for i, monkey in enumerate(monkeys):
+        print(f"Monkey {i}:")
+        print(f"{monkey}\n")
+
+    game = KeepAwayGame(monkeys)
+    game.do_rounds(20)
+    for i, monkey in enumerate(monkeys):
+        print(f"Monkey {i} inspected items : {monkey.items_inspected} times.")
+    
+    print(f"Monkey business: {game.monkey_business()}")
+
+class KeepAwayGame:
+    def __init__(self, monkeys) -> None:
+        self.monkeys = monkeys
+    
+    def do_rounds(self, n = 1):
+        for round in range(n):
+            debug_print(f"Round {round}")
+            for i, monkey in enumerate(self.monkeys):
+                debug_print(f"Monkey {i}:")
+                monkey.take_round(self.throw)
+            
+    def monkey_business(self):
+        inspections = [m.items_inspected for m in self.monkeys]
+        inspections.sort()
+        return inspections[-1] * inspections[-2]
+
+    def throw(self, recipient, worry_level):
+        self.monkeys[recipient].give(worry_level)
+
+
+class Monkey:
+    def __init__(self, items, operation, test, true_monkey, false_monkey) -> None:
+        self._items = items
+        self._operation = operation
+        self._test = test
+        self._true_monkey = true_monkey
+        self._false_monkey = false_monkey
+        self.items_inspected = 0
+
+    def __str__(self):
+        to_return = f"  Items: {str(self._items)[1:-1]}\n"
+        to_return += f"  Operation: {self._operation}\n"
+        to_return += f"  Test: divisible by {self._test}\n"
+        to_return += f"    If true: throw to monkey {self._true_monkey}\n"
+        to_return += f"    If false: throw to monkey {self._false_monkey}"
+        return to_return
+
+    def give(self, item):
+        self._items.append(item)
+    
+    def take_round(self, throw_function):
+        while len(self._items):
+            item = self._items.pop(0)
+            #debug_print(f"  Monkey inspects an item with a worry level of {item}.")
+            self.items_inspected += 1
+            item = self._operation(item)
+            debug_print(f"    New worry level: {item}.")
+            item = item // 3
+            #debug_print(f"    Monkey gets bored with item. Worry level is divided by 3 to {item}.")
+            if self._test(item):
+                debug_print(f"    Test: true")
+                throw_function(self._true_monkey, item)
+                #debug_print(f"    Item with worry level {item} is thrown to monkey {self._true_monkey}.")
+            else:
+                #debug_print(f"    Test: false")
+                throw_function(self._false_monkey, item)
+                #debug_print(f"    Item with worry level {item} is thrown to monkey {self._false_monkey}.")
+
+    def from_line(lines) -> None:
+        starting_items = [int(i) for i in lines[1].split(':')[1].split(',')]
+        operation = Monkey.create_operation_from_line(lines[2])
+        test = lambda x: x%int(lines[3].split("by")[1]) == 0
+        iftrue = int(lines[4].split("monkey")[1])
+        iffalse = int(lines[5].split("monkey")[1])
+        return Monkey(starting_items, operation, test, iftrue, iffalse)
+    
+    def create_operation_from_line(line):
+        operation = line.split(':')[1].strip()
+        global_dir = {}
+        exec(f"""def func(old):
+                {operation}
+                return new""", global_dir)
+        return global_dir["func"]
+
+def read_lines(filename):
+    lines = []
+    with open(filename) as infile:
+        for raw_line in infile:
+            line = raw_line.rstrip()
+            lines.append(line)
+    return lines
+
+def parse_arguments():
+    parser = argparse.ArgumentParser(description="Script that solves the case",epilog="Have a nice day!")
+    parser.add_argument('filename', nargs='?', default="example.txt", help='Input file')
+    args = parser.parse_args()
+    return args
+
+def main():
+    args = parse_arguments()
+    lines = read_lines(args.filename)
+    solve_task(lines)
+
+if __name__ == "__main__":
+    main()

+ 133 - 0
2022/11/b.py

@@ -0,0 +1,133 @@
+import os, argparse
+
+DEBUG = False
+DEBUG = True
+
+def debug_print(s):
+    if DEBUG:
+        print(f"DEBUG: {s}")
+
+def solve_task(lines):
+    monkeys = []
+    current_monkey = []
+    for line in lines:
+        if len(line) == 0:
+            monkeys.append(Monkey.from_line(current_monkey))
+            current_monkey = []
+        else:
+            current_monkey.append(line)
+    if(len(current_monkey) != 0):
+        monkeys.append(Monkey.from_line(current_monkey))
+
+    for i, monkey in enumerate(monkeys):
+        print(f"Monkey {i}:")
+        print(f"{monkey}\n")
+
+    game = KeepAwayGame(monkeys)
+    game.do_rounds(20)
+    for i, monkey in enumerate(monkeys):
+        print(f"Monkey {i} inspected items : {monkey.items_inspected} times.")
+    
+    print(f"Monkey business: {game.monkey_business()}")
+
+class KeepAwayGame:
+    def __init__(self, monkeys) -> None:
+        self.monkeys = monkeys
+        all_items = []
+        for monkey in self.monkeys:
+            for item in monkey.starting_items:
+                all_items.append(item)
+                index = len(all_items)-1
+                monkey.give(item, index)
+    
+    def do_rounds(self, n = 1):
+        for round in range(n):
+            debug_print(f"Round {round}")
+            for i, monkey in enumerate(self.monkeys):
+                debug_print(f"Monkey {i}:")
+                monkey.take_round(self.throw)
+            
+    def monkey_business(self):
+        inspections = [m.items_inspected for m in self.monkeys]
+        inspections.sort()
+        return inspections[-1] * inspections[-2]
+
+    def throw(self, recipient, worry_level):
+        self.monkeys[recipient].give(worry_level)
+
+
+class Monkey:
+    def __init__(self, starting_items, operation, test, true_monkey, false_monkey) -> None:
+        self._items = []
+        self.starting_items = starting_items
+        self._operation = operation
+        self._test = test
+        self._true_monkey = true_monkey
+        self._false_monkey = false_monkey
+        self.items_inspected = 0
+
+    def __str__(self):
+        to_return = f"  Items: {str(self._items)[1:-1]}\n"
+        to_return += f"  Operation: {self._operation}\n"
+        to_return += f"  Test: divisible by {self._test}\n"
+        to_return += f"    If true: throw to monkey {self._true_monkey}\n"
+        to_return += f"    If false: throw to monkey {self._false_monkey}"
+        return to_return
+
+    def give(self, item):
+        self._items.append(item)
+    
+    def take_round(self, throw_function):
+        while len(self._items):
+            item = self._items.pop(0)
+            #debug_print(f"  Monkey inspects an item with a worry level of {item}.")
+            self.items_inspected += 1
+            item = self._operation(item)
+            debug_print(f"    New worry level: {item}.")
+            #debug_print(f"    Monkey gets bored with item. Worry level is divided by 3 to {item}.")
+            if self._test(item):
+                debug_print(f"    Test: true")
+                throw_function(self._true_monkey, item)
+                #debug_print(f"    Item with worry level {item} is thrown to monkey {self._true_monkey}.")
+            else:
+                #debug_print(f"    Test: false")
+                throw_function(self._false_monkey, item)
+                #debug_print(f"    Item with worry level {item} is thrown to monkey {self._false_monkey}.")
+
+    def from_line(lines) -> None:
+        starting_items = [int(i) for i in lines[1].split(':')[1].split(',')]
+        operation = Monkey.create_operation_from_line(lines[2])
+        test = lambda x: x%int(lines[3].split("by")[1]) == 0
+        iftrue = int(lines[4].split("monkey")[1])
+        iffalse = int(lines[5].split("monkey")[1])
+        return Monkey(starting_items, operation, test, iftrue, iffalse)
+    
+    def create_operation_from_line(line):
+        operation = line.split(':')[1].strip()
+        global_dir = {}
+        exec(f"""def func(old):
+                {operation}
+                return new""", global_dir)
+        return global_dir["func"]
+
+def read_lines(filename):
+    lines = []
+    with open(filename) as infile:
+        for raw_line in infile:
+            line = raw_line.rstrip()
+            lines.append(line)
+    return lines
+
+def parse_arguments():
+    parser = argparse.ArgumentParser(description="Script that solves the case",epilog="Have a nice day!")
+    parser.add_argument('filename', nargs='?', default="example.txt", help='Input file')
+    args = parser.parse_args()
+    return args
+
+def main():
+    args = parse_arguments()
+    lines = read_lines(args.filename)
+    solve_task(lines)
+
+if __name__ == "__main__":
+    main()

+ 27 - 0
2022/11/example.txt

@@ -0,0 +1,27 @@
+Monkey 0:
+  Starting items: 79, 98
+  Operation: new = old * 19
+  Test: divisible by 23
+    If true: throw to monkey 2
+    If false: throw to monkey 3
+
+Monkey 1:
+  Starting items: 54, 65, 75, 74
+  Operation: new = old + 6
+  Test: divisible by 19
+    If true: throw to monkey 2
+    If false: throw to monkey 0
+
+Monkey 2:
+  Starting items: 79, 60, 97
+  Operation: new = old * old
+  Test: divisible by 13
+    If true: throw to monkey 1
+    If false: throw to monkey 3
+
+Monkey 3:
+  Starting items: 74
+  Operation: new = old + 3
+  Test: divisible by 17
+    If true: throw to monkey 0
+    If false: throw to monkey 1

+ 55 - 0
2022/11/infile.txt

@@ -0,0 +1,55 @@
+Monkey 0:
+  Starting items: 72, 64, 51, 57, 93, 97, 68
+  Operation: new = old * 19
+  Test: divisible by 17
+    If true: throw to monkey 4
+    If false: throw to monkey 7
+
+Monkey 1:
+  Starting items: 62
+  Operation: new = old * 11
+  Test: divisible by 3
+    If true: throw to monkey 3
+    If false: throw to monkey 2
+
+Monkey 2:
+  Starting items: 57, 94, 69, 79, 72
+  Operation: new = old + 6
+  Test: divisible by 19
+    If true: throw to monkey 0
+    If false: throw to monkey 4
+
+Monkey 3:
+  Starting items: 80, 64, 92, 93, 64, 56
+  Operation: new = old + 5
+  Test: divisible by 7
+    If true: throw to monkey 2
+    If false: throw to monkey 0
+
+Monkey 4:
+  Starting items: 70, 88, 95, 99, 78, 72, 65, 94
+  Operation: new = old + 7
+  Test: divisible by 2
+    If true: throw to monkey 7
+    If false: throw to monkey 5
+
+Monkey 5:
+  Starting items: 57, 95, 81, 61
+  Operation: new = old * old
+  Test: divisible by 5
+    If true: throw to monkey 1
+    If false: throw to monkey 6
+
+Monkey 6:
+  Starting items: 79, 99
+  Operation: new = old + 2
+  Test: divisible by 11
+    If true: throw to monkey 3
+    If false: throw to monkey 1
+
+Monkey 7:
+  Starting items: 68, 98, 62
+  Operation: new = old + 3
+  Test: divisible by 13
+    If true: throw to monkey 5
+    If false: throw to monkey 6