-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.js
115 lines (99 loc) · 3.74 KB
/
part1.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
103
104
105
106
107
108
109
110
111
112
113
114
115
const { getInputString } = require("../utils/getInputString.js");
const file = getInputString(11, false);
const getMonkeys = (file) => {
const monkeys = []
let currentMonkey;
for(const line of file.split('\n')) {
if(line.startsWith("Monkey")) {
currentMonkey = {
inspected: 0
};
} else if (line.includes("Starting items:")) {
const itemList = line.split(": ")[1];
const items = itemList.split(', ').map(item => parseInt(item));
currentMonkey.items = items;
} else if (line.includes("Operation:")) {
const operation = line.split("old ")[1];
const [operator, value] = operation.split(" ");
currentMonkey.operator = operator;
currentMonkey.operationValue = value === "old" ? value : parseInt(value);
} else if (line.includes("Test:")) {
const divisible = line.split("divisible by ")[1];
currentMonkey.divisible = parseInt(divisible);
} else if (line.includes("If true:")) {
const monkeyTo = line.split("to monkey ")[1];
currentMonkey.ifTrue = parseInt(monkeyTo);
} else if (line.includes("If false:")) {
const monkeyTo = line.split("to monkey ")[1];
currentMonkey.ifFalse = parseInt(monkeyTo);
} else {
monkeys.push(currentMonkey);
}
}
return monkeys;
}
const getNewWorryLevel = (currentWorryLevel, operator, value) => {
if(value === "old") {
value = currentWorryLevel;
}
let newWorryLevel
switch(operator) {
case "+":
newWorryLevel = currentWorryLevel + value
console.log(` Worry level is increased by ${value} to ${newWorryLevel}`)
break;
case "-":
newWorryLevel = currentWorryLevel - value
console.log(` Worry level is descreased by ${value} to ${newWorryLevel}`)
break;
case "*":
newWorryLevel = currentWorryLevel * value
console.log(` Worry level is muliplied by ${value} to ${newWorryLevel}`)
break;
case "/":
newWorryLevel = currentWorryLevel / value
console.log(` Worry level is divided by ${value} to ${newWorryLevel}`)
break;
}
newWorryLevel = Math.floor(newWorryLevel / 3);
console.log(` Monkey gets bored with item. Worry level is divided by 3 to ${newWorryLevel}`);
return newWorryLevel;
}
const doRound = (monkeys) => {
for(let i = 0; i < monkeys.length; i++) {
const monkey = monkeys[i]
console.log(`Monkey ${i}`);
for(let j = 0; j < monkey.items.length; j++) {
monkey.inspected++;
const item = monkey.items[j]
console.log(` Monkey inspects an item with a worry level of ${item}`);
const newWorryLevel = getNewWorryLevel(item, monkey.operator, monkey.operationValue);
if(newWorryLevel % monkey.divisible === 0) {
console.log(` Current worry level is divisible by ${monkey.divisible}`)
console.log(` Item with worry level ${newWorryLevel} is thrown to monkey ${monkey.ifTrue}.`)
monkeys[monkey.ifTrue].items.push(newWorryLevel);
} else {
console.log(` Current worry level is not divisible by ${monkey.divisible}`)
console.log(` Item with worry level ${newWorryLevel} is thrown to monkey ${monkey.ifFalse}.`)
monkeys[monkey.ifFalse].items.push(newWorryLevel);
}
}
monkey.items = []
}
for(let i = 0; i < monkeys.length; i++) {
const monkey = monkeys[i]
console.log(`Monkey ${i}: ${monkey.items.join(', ')}`);
}
console.log();
};
const Run = (rounds) => {
const monkeys = getMonkeys(file);
for(let i = 0; i < rounds; i++){
console.log(`Round ${i + 1}`)
doRound(monkeys);
}
const inspections = monkeys.map(monkey => monkey.inspected);
const topTwoInspections = inspections.sort((x, y) => y - x).slice(0, 2);
console.log(topTwoInspections[0] * topTwoInspections[1])
}
Run(20);