-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11_2.js
58 lines (54 loc) · 1.71 KB
/
day11_2.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
console.log('\n\n\n\n###################################');
import { readLines } from 'https://deno.land/[email protected]/io/bufio.ts';
import { range } from 'https://deno.land/x/[email protected]/range.mjs';
import { slidingWindows } from 'https://deno.land/[email protected]/collections/mod.ts';
import count from 'https://deno.land/x/[email protected]/src/collection/count.ts';
import { writeAllSync } from 'https://deno.land/std/streams/conversion.ts';
let input = await Deno.readTextFile('./resources/input11.txt');
//input = await Deno.readTextFile('./resources/input11.txt');
// input = `noop
// addx 3
// addx -5`;
const mem = {};
let d = 1;
const prg = input
.trim()
.split('\n\n')
.map((s) => s.split('\n'))
.map((s) => {
const m = {};
m.id = parseInt(s[0].match(/\d+/)[0], 10);
m.items = s[1]
.split(':')[1]
.trim()
.split(', ')
.map((i) => parseInt(i, 10));
m.op = (old) => {
const exp = s[2].split('= ')[1].replace('old', old);
return eval(exp);
};
s[3] = parseInt(s[3].match(/\d+/), 10);
s[4] = parseInt(s[4].match(/\d+/), 10);
s[5] = parseInt(s[5].match(/\d+/), 10);
m.cnt = 0;
d *= s[3];
m.test = (v) => (v % s[3] ? s[5] : s[4]);
return m;
});
console.log({ prg });
for (let i = 0; i < 10000; i++) {
console.log('Round: ', i + 1);
for (let m = 0; m < prg.length; m++) {
const mn = prg[m];
mn.cnt += mn.items.length;
while (mn.items.length) {
const it = mn.items.shift();
let newIt = mn.op(it) % d;
//console.log({ it, newIt }, mn.test(newIt));
prg[mn.test(newIt)].items.push(newIt);
}
}
//console.log({ prg });
}
const srt = prg.sort((a, b) => b.cnt - a.cnt);
console.log({ srt }, srt[0].cnt * srt[1].cnt);