-
Notifications
You must be signed in to change notification settings - Fork 0
/
11b.ts
70 lines (60 loc) · 1.72 KB
/
11b.ts
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
import "./utils/helpers.ts";
const input = await Deno.readTextFile("11.txt");
interface Monkey {
items: number[];
op: "+" | "*";
val: string;
dividable: number;
throwToIfTrue: number;
throwToIfFalse: number;
inspectCount: number;
}
const monkeys = input
.trim()
.lines()
.split("")
.reduce((av, cv) => {
const items = cv[1].nums().map((val) => val);
const [op, val] = cv[2].words().lastVals(2);
const [dividable] = cv[3].nums();
const [throwToIfTrue] = cv[4].nums();
const [throwToIfFalse] = cv[5].nums();
av.push({
items,
op: op as "+" | "*",
val,
dividable,
throwToIfTrue,
throwToIfFalse,
inspectCount: 0,
});
return av;
}, [] as Monkey[]);
const getScore = () => {
const res = [...monkeys].sort((a, b) => b.inspectCount - a.inspectCount);
return {
insps: res.map((r) => r.inspectCount),
monkeyBusiness: res[0].inspectCount * res[1].inspectCount,
};
};
const commonDiv = monkeys.reduce((av, cv) => cv.dividable * av, 1);
for (let round = 0; round < 10000; round++) {
for (const monkey of monkeys) {
const { items, val, op, dividable, throwToIfFalse, throwToIfTrue } = monkey;
let item: number;
while ((item = items.pop()!)) {
const getVar = (): number => (val === "old" ? item : parseInt(val));
let updItem: number;
if (op === "+") updItem = item + getVar();
else updItem = item * getVar();
const div = dividable;
updItem %= commonDiv;
const wasDiv = updItem % div === 0;
if (wasDiv) {
monkeys[throwToIfTrue].items.push(updItem);
} else monkeys[throwToIfFalse].items.push(updItem);
monkey.inspectCount++;
}
}
}
console.log(getScore().monkeyBusiness);