-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.js
92 lines (81 loc) · 2.29 KB
/
11.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {readInput} from '../../../util.js';
const directoryPath = path.dirname(fileURLToPath(import.meta.url));
const input = await readInput(directoryPath);
const monkeys = input
.trim()
.split('\n\n')
.map(monkeyInput => {
const newMonkeyObject = monkeyInput
.split('\n')
.map(currentInput => currentInput.trim());
const items = newMonkeyObject[1]
.split('Starting items: ')[1]
.split(', ')
.map(Number);
const [type, amount] = newMonkeyObject[2]
.split('Operation: new = old ')[1]
.split(' ');
const divisibleBy = Number(
newMonkeyObject[3].split('Test: divisible by ')[1],
);
const divisibleTrue = Number(
newMonkeyObject[4].split('If true: throw to monkey ')[1],
);
const divisibleFalse = Number(
newMonkeyObject[5].split('If false: throw to monkey ')[1],
);
return {
items,
operation: {
type: amount === 'old' ? '**' : type,
amount: Number(amount),
},
test: {
divisible_by: divisibleBy,
true: divisibleTrue,
false: divisibleFalse,
},
inspections: 0,
};
});
// Loop 20 rounds
for (let round = 0; round < 20; round++) {
for (const monkey of monkeys) {
for (const item of monkey.items) {
let newWorryLevel;
switch (monkey.operation.type) {
case '*':
newWorryLevel = item * monkey.operation.amount;
break;
case '+':
newWorryLevel = item + monkey.operation.amount;
break;
case '**':
newWorryLevel = item * item;
break;
default:
throw new Error('Unsupported operation type');
}
// Monkey is bored of item
newWorryLevel = Math.floor(newWorryLevel / 3);
// Give item to other monkey
if (newWorryLevel % monkey.test.divisible_by === 0) {
monkeys[monkey.test.true].items.push(newWorryLevel);
} else {
monkeys[monkey.test.false].items.push(newWorryLevel);
}
// Remove item from current monkey
monkey.items = monkey.items.filter(currentItem => currentItem !== item);
monkey.inspections++;
}
}
}
const twoMonkeysWithMostInspections = monkeys
.sort((a, b) => b.inspections - a.inspections)
.slice(0, 2);
const monkeyBusiness =
twoMonkeysWithMostInspections[0].inspections *
twoMonkeysWithMostInspections[1].inspections;
console.log('Monkey business:', monkeyBusiness);