From c23561a53a6e1d0119d542b1d301b4c5f217daae Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Sun, 11 Dec 2022 20:13:37 +0100 Subject: [PATCH] 11_2 --- day11/index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/day11/index.js b/day11/index.js index 14812e8..296c25f 100644 --- a/day11/index.js +++ b/day11/index.js @@ -17,12 +17,16 @@ function resolveOperation(num, oper) { const monkeys = []; let monkey = {}; +let productOfDivs = 1; 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(" Test: ")) { + monkey.divisibleBy = parseInt(line.split(" divisible by ")[1]); + productOfDivs *= 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]); @@ -32,12 +36,12 @@ input.split('\n').forEach(line => { } }); -for(let i = 0; i < 20; i++) { +for(let i = 0; i < 10000; i++) { monkeys.forEach(m => { while(m.items.length > 0) { m.itemsInspected++; let worry = resolveOperation(m.items[0], m.operation); - worry = Math.floor(worry / 3); + worry = Math.floor(worry % productOfDivs); m.items.splice(0, 1); if(worry % m.divisibleBy === 0) monkeys[m.ifTrue].items.push(worry); else monkeys[m.ifFalse].items.push(worry);