-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.js
102 lines (83 loc) · 2.64 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
93
94
95
96
97
98
99
100
101
102
const { cloneDeep } = require("lodash");
const input = `Monkey 0:
Starting items: 79, 98
Operation: new = old * 19
Test: divisible by 23
If true: throw to monkey 2
If false: throw to monkey 3
Monkey 1:
Starting items: 54, 65, 75, 74
Operation: new = old + 6
Test: divisible by 19
If true: throw to monkey 2
If false: throw to monkey 0
Monkey 2:
Starting items: 79, 60, 97
Operation: new = old * old
Test: divisible by 13
If true: throw to monkey 1
If false: throw to monkey 3
Monkey 3:
Starting items: 74
Operation: new = old + 3
Test: divisible by 17
If true: throw to monkey 0
If false: throw to monkey 1`;
const monkeys = input.split("\n\n").map((monkey) => {
const [, startingItemsRow, operationRow, testRow, trueRow, falseRow] = monkey
.split("\n")
.map((s) => s.split(": ")[1]);
const items = startingItemsRow.split(", ").map(Number);
const operation = operationRow.split(" = ")[1];
const testDivisibleBy = Number(testRow.split(" ").reverse()[0]);
const ifTrue = Number(trueRow.split(" ").reverse()[0]);
const ifFalse = Number(falseRow.split(" ").reverse()[0]);
return { items, operation, testDivisibleBy, ifTrue, ifFalse };
});
const modAll = monkeys.map(m => m.testDivisibleBy).reduce((s, v) => s * v, 1);
const partOne = (monkeys) => {
const doRoundForMonkey = (monkey) => {
monkey.items.forEach((old) => {
let worryLevel = Math.floor(eval(monkey.operation) / 3);
monkeys[
worryLevel % monkey.testDivisibleBy === 0
? monkey.ifTrue
: monkey.ifFalse
].items.push(worryLevel);
});
monkey.items = [];
};
const inspectedItems = Array.from(monkeys).fill(0);
for (let round = 0; round < 20; round++) {
monkeys.forEach((monkey, idx) => {
inspectedItems[idx] += monkey.items.length;
doRoundForMonkey(monkey);
});
}
const [a, b] = inspectedItems.sort((a, b) => a - b).reverse();
console.log("Part 1", a * b);
};
partOne(cloneDeep(monkeys));
const partTwo = (monkeys) => {
const doRoundForMonkey = (monkey) => {
monkey.items.forEach((old) => {
let worryLevel = eval(monkey.operation) % modAll;
monkeys[
worryLevel % monkey.testDivisibleBy === 0
? monkey.ifTrue
: monkey.ifFalse
].items.push(worryLevel);
});
monkey.items = [];
};
const inspectedItems = Array.from(monkeys).fill(0);
for (let round = 0; round < 10_000; round++) {
monkeys.forEach((monkey, idx) => {
inspectedItems[idx] += monkey.items.length;
doRoundForMonkey(monkey);
});
}
const [a, b] = inspectedItems.sort((a, b) => a - b).reverse();
console.log("Part 2", a * b);
};
partTwo(cloneDeep(monkeys));