-
Notifications
You must be signed in to change notification settings - Fork 5
/
day11.mjs
95 lines (83 loc) · 2.69 KB
/
day11.mjs
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
import { readFileSync } from "node:fs";
function getOperationFunction(input) {
return function (old) {
const string = input.replace(/old/, old);
// Warning: do not use in prod
return eval(string);
};
}
// console.log(getOperationFunction("old * 19")(10));
// console.log(getOperationFunction("old * old")(10));
// console.log(getOperationFunction("old + 3")(10));
function getMonkeys() {
const monkeys = readFileSync("day11.txt", { encoding: "utf-8" }) // read day??.txt content
.replace(/\r/g, "") // remove all \r characters to avoid issues on Windows
.trim() // Remove starting/ending whitespace
.split("\n\n") // Split on newline
.map((lines, monkeyId) => {
const items = lines
.match(/Starting items(?:[:,] (\d+))+/g)[0]
.split(": ")[1]
.split(", ")
.map(Number);
const operation = lines.match(/= ([^\n]+)/)[1];
const divisibleBy = parseInt(lines.match(/divisible by (\d+)/)[1]);
const whenTrueSendTo = parseInt(
lines.match(/If true: throw to monkey (\d)/)[1]
);
const whenFalseSendTo = parseInt(
lines.match(/If false: throw to monkey (\d)/)[1]
);
return {
id: monkeyId,
totalInspectedObjects: 0,
items,
divisibleBy,
operation: getOperationFunction(operation),
sendTo: (item) =>
item % divisibleBy === 0 ? whenTrueSendTo : whenFalseSendTo,
};
});
return monkeys;
}
function part1() {
const monkeys = getMonkeys();
for (let i = 0; i < 20; i++) {
for (const monkey of monkeys) {
let items = monkey.items;
while (items.length) {
let item = items.shift();
monkey.totalInspectedObjects++;
item = monkey.operation(item);
item = Math.floor(item / 3);
const destination = monkey.sendTo(item);
monkeys[destination].items.push(item);
}
}
}
const activity = monkeys.map((m) => m.totalInspectedObjects);
activity.sort((a, b) => b - a);
console.log(activity[0] * activity[1]);
}
function part2() {
const monkeys = getMonkeys();
const divider = monkeys.map((m) => m.divisibleBy).reduce((a, b) => a * b, 1);
for (let i = 0; i < 10000; i++) {
for (const monkey of monkeys) {
let items = monkey.items;
while (items.length) {
let item = items.shift();
monkey.totalInspectedObjects++;
item = monkey.operation(item);
item = item % divider;
const destination = monkey.sendTo(item);
monkeys[destination].items.push(item);
}
}
}
const activity = monkeys.map((m) => m.totalInspectedObjects);
activity.sort((a, b) => b - a);
console.log(activity[0] * activity[1]);
}
part1();
part2();