-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11_1.js
102 lines (90 loc) · 3.53 KB
/
11_1.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
93
94
95
96
97
98
99
100
101
102
const fileName = process.argv.slice(2)[0] || 'input.txt';
class ItemMove {
constructor(itemWorryLevel, monkeyTo) {
this.itemWorryLevel = itemWorryLevel;
this.monkeyTo = monkeyTo;
}
}
class Monkey {
constructor(itemWorryLevels, operation, divisor, monkeyIfTrue, monkeyIfFalse) {
this.itemWorryLevels = itemWorryLevels;
this.operation = operation;
this.divisor = divisor;
this.monkeyIfTrue = monkeyIfTrue;
this.monkeyIfFalse = monkeyIfFalse;
this.inspectionCount = 0;
}
calculateMove() {
// returns Item Move array. Example: [{itemWorryLevel: 500, monkeyTo: 0}]
const result = this.itemWorryLevels.map(worryLevel => {
const newWorryLevel = parseInt(this.operation(worryLevel) / 3);
this.inspectionCount++;
return new ItemMove(newWorryLevel, newWorryLevel % this.divisor === 0 ? this.monkeyIfTrue : this.monkeyIfFalse);
});
// Reset worry levels as the monkey gives every item away
this.itemWorryLevels = [];
return result;
}
}
const OPERATION_COUNT = 20;
async function solve() {
const lineReader = require('readline').createInterface({
input: require('fs').createReadStream(fileName)
});
const monkeys = [];
let startingItems;
let operation;
let divisor;
let monkeyIfTrue;
for await (const line of lineReader) {
const lineContents = line.replaceAll(' ', '').split(' ');
switch(lineContents[0]) {
case "Starting":
// starting from index 2 because 0 = "Starting", 1 = "items"
startingItems = lineContents.slice(2, lineContents.length).map(numberString => Number(numberString.replace(',', '')));
continue;
case "Operation:":
const operationSign = lineContents[4];
const value = lineContents[5];
if (operationSign === '*') {
if (value === 'old') {
operation = (worryLevel) => worryLevel * worryLevel;
} else {
operation = (worryLevel) => worryLevel * Number(value);
}
} else {
if (value === 'old') {
operation = (worryLevel) => worryLevel + worryLevel;
} else {
operation = (worryLevel) => worryLevel + Number(value);
}
}
continue;
case "Test:":
divisor = Number(lineContents[3]);
continue;
case "If":
if(lineContents[1].startsWith('t')) {
monkeyIfTrue = Number(lineContents[5]);
} else {
monkeys.push(new Monkey(startingItems, operation, divisor, monkeyIfTrue, lineContents[5]));
}
break;
}
}
for (let count = 0; count < OPERATION_COUNT; count++) {
monkeys.forEach(monkey => {
const result = monkey.calculateMove();
for (const move of result) {
monkeys[move.monkeyTo].itemWorryLevels.push(move.itemWorryLevel);
}
});
}
console.log(
monkeys
.sort((a,b) => b.inspectionCount - a.inspectionCount) // highest to the front
.slice(0, 2) // first two monkeys
.reduce((monkeyBusiness, currentMonkey) => monkeyBusiness * currentMonkey.inspectionCount, 1) // calculate "monkey business"
);
}
solve();