Skip to content

Commit cc3b72c

Browse files
author
Mustafa Hanif
committed
solve day 11
1 parent d42c3d7 commit cc3b72c

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

Diff for: 2022-11.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
var fs = require('fs');
2+
const file = fs.readFileSync('input2022-11.txt', 'utf8').trimEnd();
3+
let lines = file.split('\n');
4+
5+
let i = 0;
6+
const monkeys = new Array(8).fill(null).map(() => []);
7+
const activeMonkeys = new Array(8).fill(0);
8+
9+
// reddit told me about * all the divisors, I knew % was involved but I didn't know how to get the divisors
10+
let product = 9699690;
11+
for (let j = 0; j < 10000; j++) {
12+
let i = 0;
13+
while (i < lines.length) {
14+
let line = lines[i];
15+
// console.log(line)
16+
if (line.match(/Monkey ([0-9])+:/m)) {
17+
const monkey = parseInt(line.match(/Monkey ([0-9])+:/m)[1], 10);
18+
// console.log('starting new monkey', monkey);
19+
20+
let itemsString = lines[i + 1];
21+
let items = itemsString.match(/ Starting items: (.*)/m)[1].split(', ').map(Number);
22+
if (j === 0) {
23+
monkeys[monkey] = [...items, ...monkeys[monkey]];
24+
}
25+
26+
let opString = lines[i + 2];
27+
// Operation: new = old * 19
28+
let ops = opString.match(/ Operation: new = old (.*) (.*)/m)[1];
29+
// console.log(ops);
30+
31+
let opValue = opString.match(/ Operation: new = old (.*) (.*)/m)[2];
32+
// console.log(opValue);
33+
34+
let testString = lines[i + 3];
35+
// Test: divisible by
36+
let test = parseInt(testString.match(/ Test: divisible by (.*)/m)[1], 10);
37+
// if (j === 0) {
38+
// product = product * test;
39+
// }
40+
// console.log(test);
41+
42+
let _true = lines[i + 4];
43+
// If true: throw to monkey 2
44+
let trueMonkey = parseInt(_true.match(/ If true: throw to monkey (.*)/m)[1], 10);
45+
46+
// If false: throw to monkey 0
47+
let _false = lines[i + 5];
48+
let falseMonkey = parseInt(_false.match(/ If false: throw to monkey (.*)/m)[1], 10);
49+
50+
// console.log(trueMonkey, falseMonkey, 'working on items', monkeys[monkey]);
51+
const copyItems = [...monkeys[monkey]];
52+
for (let i = 0; i < copyItems.length; i++) {
53+
const item = copyItems[i];
54+
let result = item;
55+
let value = Number.isInteger(parseInt(opValue, 10)) ? parseInt(opValue, 10) : item;
56+
// console.log('perform op', ops, value);
57+
switch (ops) {
58+
case '*':
59+
result = item * value;
60+
break;
61+
case '+':
62+
result = item + value;
63+
break;
64+
case '-':
65+
result = item - value;
66+
break;
67+
case '/':
68+
result = item / value;
69+
break;
70+
default:
71+
break;
72+
}
73+
// console.log('result', result);
74+
// result = Math.floor(result / 3);
75+
// console.log('result', result);
76+
result = result % product;
77+
if (result % test === 0) {
78+
// console.log('true', trueMonkey);
79+
monkeys[trueMonkey].push(result);
80+
monkeys[monkey].splice(monkeys[monkey].indexOf(item), 1);
81+
} else {
82+
// console.log('false', result, test, result % test, (result % test) + test);
83+
monkeys[falseMonkey].push(result);
84+
monkeys[monkey].splice(monkeys[monkey].indexOf(item), 1);
85+
}
86+
activeMonkeys[monkey]++;
87+
};
88+
}
89+
// console.log(monkeys);
90+
i+=7;
91+
}
92+
}
93+
console.log(activeMonkeys)
94+
const sorted = activeMonkeys.sort((a, b) => b - a);
95+
console.log(sorted[0] * sorted[1]);

Diff for: input2022-11.txt

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Monkey 0:
2+
Starting items: 89, 73, 66, 57, 64, 80
3+
Operation: new = old * 3
4+
Test: divisible by 13
5+
If true: throw to monkey 6
6+
If false: throw to monkey 2
7+
8+
Monkey 1:
9+
Starting items: 83, 78, 81, 55, 81, 59, 69
10+
Operation: new = old + 1
11+
Test: divisible by 3
12+
If true: throw to monkey 7
13+
If false: throw to monkey 4
14+
15+
Monkey 2:
16+
Starting items: 76, 91, 58, 85
17+
Operation: new = old * 13
18+
Test: divisible by 7
19+
If true: throw to monkey 1
20+
If false: throw to monkey 4
21+
22+
Monkey 3:
23+
Starting items: 71, 72, 74, 76, 68
24+
Operation: new = old * old
25+
Test: divisible by 2
26+
If true: throw to monkey 6
27+
If false: throw to monkey 0
28+
29+
Monkey 4:
30+
Starting items: 98, 85, 84
31+
Operation: new = old + 7
32+
Test: divisible by 19
33+
If true: throw to monkey 5
34+
If false: throw to monkey 7
35+
36+
Monkey 5:
37+
Starting items: 78
38+
Operation: new = old + 8
39+
Test: divisible by 5
40+
If true: throw to monkey 3
41+
If false: throw to monkey 0
42+
43+
Monkey 6:
44+
Starting items: 86, 70, 60, 88, 88, 78, 74, 83
45+
Operation: new = old + 4
46+
Test: divisible by 11
47+
If true: throw to monkey 1
48+
If false: throw to monkey 2
49+
50+
Monkey 7:
51+
Starting items: 81, 58
52+
Operation: new = old + 5
53+
Test: divisible by 17
54+
If true: throw to monkey 3
55+
If false: throw to monkey 5

0 commit comments

Comments
 (0)