diff --git a/day11/index.js b/day11/index.js new file mode 100644 index 0000000..14812e8 --- /dev/null +++ b/day11/index.js @@ -0,0 +1,51 @@ +import { readFileSync } from "fs"; + +const input = readFileSync("monkeys.txt", "utf-8"); + +function resolveOperation(num, oper) { + oper = oper.split(" = ")[1]; + if(oper.includes("+")) { + const number = oper.split(" + ")[1]; + num += number.includes("old") ? num : parseInt(number); + } else { + const number = oper.split(" * ")[1]; + num *= number.includes("old") ? num : parseInt(number); + } + return num; +} + +const monkeys = []; + +let monkey = {}; + +input.split('\n').forEach(line => { + if(line.startsWith("Monkey ")) monkey.number = parseInt(line.split(" ")[1][0]); + if(line.startsWith(" Starting items: ")) monkey.items = line.split(": ")[1].split(', ').slice().map(x => parseInt(x)); + if(line.startsWith(" Operation: ")) monkey.operation = line.split(": ")[1]; + if(line.startsWith(" Test: ")) monkey.divisibleBy = parseInt(line.split(" divisible by ")[1]); + if(line.startsWith(" If true: ")) monkey.ifTrue = parseInt(line.split(" throw to monkey ")[1]); + if(line.startsWith(" If false: ")) { + monkey.ifFalse = parseInt(line.split(" throw to monkey ")[1]); + monkey.itemsInspected = 0; + monkeys.push(monkey); + monkey = {}; + } +}); + +for(let i = 0; i < 20; i++) { + monkeys.forEach(m => { + while(m.items.length > 0) { + m.itemsInspected++; + let worry = resolveOperation(m.items[0], m.operation); + worry = Math.floor(worry / 3); + m.items.splice(0, 1); + if(worry % m.divisibleBy === 0) monkeys[m.ifTrue].items.push(worry); + else monkeys[m.ifFalse].items.push(worry); + } + }); +} + +const findMaxArray = monkeys.map(m => m.itemsInspected).sort((a, b) => a - b); +const monkeyBusiness = findMaxArray.pop() * findMaxArray.pop(); + +console.log(monkeyBusiness) \ No newline at end of file diff --git a/day11/monkeys.txt b/day11/monkeys.txt new file mode 100644 index 0000000..adf6e72 --- /dev/null +++ b/day11/monkeys.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 89, 84, 88, 78, 70 + Operation: new = old * 5 + Test: divisible by 7 + If true: throw to monkey 6 + If false: throw to monkey 7 + +Monkey 1: + Starting items: 76, 62, 61, 54, 69, 60, 85 + Operation: new = old + 1 + Test: divisible by 17 + If true: throw to monkey 0 + If false: throw to monkey 6 + +Monkey 2: + Starting items: 83, 89, 53 + Operation: new = old + 8 + Test: divisible by 11 + If true: throw to monkey 5 + If false: throw to monkey 3 + +Monkey 3: + Starting items: 95, 94, 85, 57 + Operation: new = old + 4 + Test: divisible by 13 + If true: throw to monkey 0 + If false: throw to monkey 1 + +Monkey 4: + Starting items: 82, 98 + Operation: new = old + 7 + Test: divisible by 19 + If true: throw to monkey 5 + If false: throw to monkey 2 + +Monkey 5: + Starting items: 69 + Operation: new = old + 2 + Test: divisible by 2 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 6: + Starting items: 82, 70, 58, 87, 59, 99, 92, 65 + Operation: new = old * 11 + Test: divisible by 5 + If true: throw to monkey 7 + If false: throw to monkey 4 + +Monkey 7: + Starting items: 91, 53, 96, 98, 68, 82 + Operation: new = old * old + Test: divisible by 3 + If true: throw to monkey 4 + If false: throw to monkey 2 \ No newline at end of file