Compare commits

..

2 Commits

Author SHA1 Message Date
Aelita4 c23561a53a
11_2 2022-12-11 20:13:37 +01:00
Aelita4 c1e13b1a6c
11_1 2022-12-11 18:35:57 +01:00
2 changed files with 110 additions and 0 deletions

55
day11/index.js Normal file
View File

@ -0,0 +1,55 @@
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 = {};
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]);
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]);
monkey.itemsInspected = 0;
monkeys.push(monkey);
monkey = {};
}
});
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 % productOfDivs);
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)

55
day11/monkeys.txt Normal file
View File

@ -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