-
Notifications
You must be signed in to change notification settings - Fork 16
/
part-one.js
59 lines (45 loc) · 980 Bytes
/
part-one.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
const { poly, rules } = require('./input');
class Element {
constructor(name) {
this.name = name;
this.next = null;
}
toString() {
return this.name;
}
reset() {
this.next = null;
}
}
let list = poly.split('').map((v) => new Element(v));
for (let i = 0; i < 10; i++) {
for (let j = 0; j < list.length - 1; j++) {
let a = list[j];
let b = list[j + 1];
let pair = '' + a + b;
if (rules.has(pair)) {
a.next = new Element(rules.get(pair));
}
}
let new_list = [];
for (let j = 0; j < list.length; j++) {
let a = list[j];
new_list.push(a);
if (a.next) {
new_list.push(a.next);
a.reset();
}
}
list = new_list;
}
let obj = list.reduce((obj, item) => {
obj[item.name] = (obj[item.name] || 0) + 1;
return obj;
}, {});
let things = Object.entries(obj);
things.sort((a, b) => a[1] - b[1]);
let min = things[0];
let max = things[things.length - 1];
console.log('min:', min);
console.log('max:', max);
console.log(max[1] - min[1]);