diff --git a/python/day11/bsp.txt b/python/day11/bsp.txt new file mode 100644 index 0000000..c04eddb --- /dev/null +++ b/python/day11/bsp.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 \ No newline at end of file diff --git a/python/day11/day11_1.py b/python/day11/day11_1.py new file mode 100644 index 0000000..864e788 --- /dev/null +++ b/python/day11/day11_1.py @@ -0,0 +1,71 @@ +lines = open('input.txt', 'r').read().split("\n\n") +lines = [line.splitlines() for line in lines] + +class Monkey: + def __init__(self, index, items, operator, mul, div, true, false): + self.index = index + self.items = items + self.operator = operator + self.mul = mul + self.div = div + self.true = true + self.false = false + self.activity = 0 + + def business(self): + throwing = [] + at = [] + for idx, item in enumerate(self.items): + worry = self.op(idx) + worry = worry // 3 + target = self.true if self.test(worry) else self.false + throwing.append(worry) + at.append(target) + self.items = [] + return (throwing, at) + def op(self, itemindex): + if self.mul == "old": + mult = int(self.items[itemindex]) + else: + mult = int(self.mul) + if self.operator == "*": + worry = self.items[itemindex] * mult + else: + worry = self.items[itemindex] + mult + self.activity += 1 + return worry + def test(self, worrylevel): + if worrylevel % self.div == 0: + return True + else: + return False + + +monkeys = [] +for l in lines: + index = l[0].split(" ")[1][:-1] + items = [int(i) for i in l[1].split(":")[1].replace(",","").split(" ")[1:]] + operation, mul = l[2].split(" ")[-2:] + divisor = int(l[3].split(" ")[-1]) + true = int(l[4].split(" ")[-1]) + false = int(l[5].split(" ")[-1]) + monkeys.append(Monkey(index, items, operation,mul, divisor, true, false)) + + +for round in range(20): + #print(round) + for monkey in monkeys: + if len(monkey.items) == 0: + continue + else: + items, targets = monkey.business() + for i in range(len(items)): + monkeys[targets[i]].items.append(items[i]) + +active = [] +for m in monkeys: + active.append(m.activity) + +active = sorted(active) + +print(active[-1] * active[-2]) \ No newline at end of file diff --git a/python/day11/day11_2.py b/python/day11/day11_2.py new file mode 100644 index 0000000..26d45ab --- /dev/null +++ b/python/day11/day11_2.py @@ -0,0 +1,77 @@ +lines = open('input.txt', 'r').read().split("\n\n") +lines = [line.splitlines() for line in lines] + +def product(lst): + p = 1 + for i in lst: + p *= i + return p +class Monkey: + def __init__(self, index, items, operator, mul, div, true, false): + self.index = index + self.items = items + self.operator = operator + self.mul = mul + self.div = div + self.true = true + self.false = false + self.activity = 0 + + def business(self): + throwing = [] + at = [] + for idx, item in enumerate(self.items): + worry = self.op(idx) + mod = product([m.div for m in monkeys]) + worry = worry % mod + target = self.true if self.test(worry) else self.false + throwing.append(worry) + at.append(target) + self.items = [] + return (throwing, at) + def op(self, itemindex): + if self.mul == "old": + mult = int(self.items[itemindex]) + else: + mult = int(self.mul) + if self.operator == "*": + worry = self.items[itemindex] * mult + else: + worry = self.items[itemindex] + mult + self.activity += 1 + return worry + def test(self, worrylevel): + if worrylevel % self.div == 0: + return True + else: + return False + + +monkeys = [] +for l in lines: + index = l[0].split(" ")[1][:-1] + items = [int(i) for i in l[1].split(":")[1].replace(",","").split(" ")[1:]] + operation, mul = l[2].split(" ")[-2:] + divisor = int(l[3].split(" ")[-1]) + true = int(l[4].split(" ")[-1]) + false = int(l[5].split(" ")[-1]) + monkeys.append(Monkey(index, items, operation,mul, divisor, true, false)) + + +for round in range(10000): + print(round) + for monkey in monkeys: + if len(monkey.items) == 0: + continue + else: + items, targets = monkey.business() + for i in range(len(items)): + monkeys[targets[i]].items.append(items[i]) + +active = [] +for m in monkeys: + active.append(m.activity) + +active = sorted(active) +print(active) +print(active[-1] * active[-2]) \ No newline at end of file diff --git a/python/day11/input.txt b/python/day11/input.txt new file mode 100644 index 0000000..51ce92c --- /dev/null +++ b/python/day11/input.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 77, 69, 76, 77, 50, 58 + Operation: new = old * 11 + Test: divisible by 5 + If true: throw to monkey 1 + If false: throw to monkey 5 + +Monkey 1: + Starting items: 75, 70, 82, 83, 96, 64, 62 + Operation: new = old + 8 + Test: divisible by 17 + If true: throw to monkey 5 + If false: throw to monkey 6 + +Monkey 2: + Starting items: 53 + Operation: new = old * 3 + Test: divisible by 2 + If true: throw to monkey 0 + If false: throw to monkey 7 + +Monkey 3: + Starting items: 85, 64, 93, 64, 99 + Operation: new = old + 4 + Test: divisible by 7 + If true: throw to monkey 7 + If false: throw to monkey 2 + +Monkey 4: + Starting items: 61, 92, 71 + Operation: new = old * old + Test: divisible by 3 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 5: + Starting items: 79, 73, 50, 90 + Operation: new = old + 2 + Test: divisible by 11 + If true: throw to monkey 4 + If false: throw to monkey 6 + +Monkey 6: + Starting items: 50, 89 + Operation: new = old + 3 + Test: divisible by 13 + If true: throw to monkey 4 + If false: throw to monkey 3 + +Monkey 7: + Starting items: 83, 56, 64, 58, 93, 91, 56, 65 + Operation: new = old + 5 + Test: divisible by 19 + If true: throw to monkey 1 + If false: throw to monkey 0