-
Notifications
You must be signed in to change notification settings - Fork 1
/
18.js
93 lines (83 loc) · 1.92 KB
/
18.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
'use strict';
class SnailPair {
/**
*
* @param {number|SnailPair} l
* @param {number|SnailPair} r
*/
constructor(l, r) {
this.left = l;
this.right = r;
}
getHeight() {
let height = 0;
if (typeof this.left !== 'number') {
height = this.left.getHeight();
}
if (typeof this.right !== 'number') {
height = Math.max(height, this.right.getHeight());
}
return height + 1;
}
reduce(targetHeight = 4) {
while (this.getHeight() > targetHeight) {
let leftReturn = 0;
if (typeof this.left == 'object' && this.left.getHeight() > targetHeight - 1) {
this.left.explode(targetHeight - 1);
}
if (typeof this.right == 'object' && this.right.getHeight() > targetHeight - 1) {
this.right.explode(targetHeight - 1);
}
break;
}
}
/**
* This pair needs to be reduced in height because the parent pair is too high
* @param {number} targetHeight
*/
explode(targetHeight) {
console.log(targetHeight);
}
}
/**
* @param {string} strNumber
*/
function parseSnailNumberString(strNumber) {
const pairs = [];
const pairRegex = /\[(p?\d+),(p?\d+)]/;
while (strNumber[0] === '[') {
const idx = 'p' + pairs.length;
const [pairStr, ...pairArray] = pairRegex.exec(strNumber);
strNumber = strNumber.replace(pairStr, idx);
if (pairArray[0][0] == 'p') {
pairArray[0] = pairs[pairArray[0].slice(1)];
} else {
pairArray[0] = parseInt(pairArray[0]);
}
if (pairArray[1][0] == 'p') {
pairArray[1] = pairs[pairArray[1].slice(1)];
} else {
pairArray[1] = parseInt(pairArray[1]);
}
pairs.push(new SnailPair(...pairArray));
}
return pairs.pop();
}
/**
* @param {string} d
*/
export const part1 = async d => {
const data = d.split('\n');
// Debugging reducing
const pair = parseSnailNumberString(data[0]);
pair.reduce();
console.log(pair.getHeight());
return false;
};
/**
* @param {string} d
*/
export const part2 = async d => {
const data = d.split('\n');
return data;
};