This commit is contained in:
Aelita4 2022-12-11 20:13:37 +01:00
parent c1e13b1a6c
commit c23561a53a
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
1 changed files with 7 additions and 3 deletions

View File

@ -17,12 +17,16 @@ function resolveOperation(num, oper) {
const monkeys = []; const monkeys = [];
let monkey = {}; let monkey = {};
let productOfDivs = 1;
input.split('\n').forEach(line => { input.split('\n').forEach(line => {
if(line.startsWith("Monkey ")) monkey.number = parseInt(line.split(" ")[1][0]); 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(" 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(" 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 true: ")) monkey.ifTrue = parseInt(line.split(" throw to monkey ")[1]);
if(line.startsWith(" If false: ")) { if(line.startsWith(" If false: ")) {
monkey.ifFalse = parseInt(line.split(" throw to monkey ")[1]); 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 => { monkeys.forEach(m => {
while(m.items.length > 0) { while(m.items.length > 0) {
m.itemsInspected++; m.itemsInspected++;
let worry = resolveOperation(m.items[0], m.operation); let worry = resolveOperation(m.items[0], m.operation);
worry = Math.floor(worry / 3); worry = Math.floor(worry % productOfDivs);
m.items.splice(0, 1); m.items.splice(0, 1);
if(worry % m.divisibleBy === 0) monkeys[m.ifTrue].items.push(worry); if(worry % m.divisibleBy === 0) monkeys[m.ifTrue].items.push(worry);
else monkeys[m.ifFalse].items.push(worry); else monkeys[m.ifFalse].items.push(worry);