-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.js
124 lines (108 loc) · 2.72 KB
/
day10.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
116
117
118
119
120
121
122
123
124
const { data, test } = require('./data/day10');
const OPEN = {
ANGLE: '<',
BRACKET: '[',
PAREN: '(',
CURLY: '{',
};
const OPEN_CHARS = Object.values(OPEN);
const CLOSE = {
ANGLE: '>',
BRACKET: ']',
PAREN: ')',
CURLY: '}',
};
const CLOSE_CHARS = Object.values(CLOSE);
const OPEN_TO_CLOSE = {
[OPEN.ANGLE]: CLOSE.ANGLE,
[OPEN.BRACKET]: CLOSE.BRACKET,
[OPEN.PAREN]: CLOSE.PAREN,
[OPEN.CURLY]: CLOSE.CURLY,
};
const CORRUPT_COST = {
')': 3,
']': 57,
'}': 1197,
'>': 25137,
};
const INCOMPLETE_COST = {
')': 1,
']': 2,
'}': 3,
'>': 4,
};
const determineFirstCorruptCharacter = (line) => {
const charArray = line.split('');
const openStack = [];
let nextChar = charArray.shift()
while (nextChar) {
if (OPEN_CHARS.includes(nextChar)) {
openStack.push(nextChar);
}
if (CLOSE_CHARS.includes(nextChar)) {
const matchingOpen = openStack.pop();
if (OPEN_TO_CLOSE[matchingOpen] !== nextChar) {
return nextChar;
}
}
nextChar = charArray.shift();
}
return false;
};
const determineIncompleteLine = (line) => {
const charArray = line.split('');
const openStack = [];
let nextChar = charArray.shift()
while (nextChar) {
if (OPEN_CHARS.includes(nextChar)) {
openStack.push(nextChar);
}
if (CLOSE_CHARS.includes(nextChar)) {
const matchingOpen = openStack.pop();
if (OPEN_TO_CLOSE[matchingOpen] !== nextChar) {
return false;
}
}
nextChar = charArray.shift();
}
return line;
};
const computeClosingSequence = (line) => {
const charArray = line.split('');
const openStack = [];
let nextChar = charArray.shift()
while (nextChar) {
if (OPEN_CHARS.includes(nextChar)) {
openStack.push(nextChar);
}
if (CLOSE_CHARS.includes(nextChar)) {
const matchingOpen = openStack.pop();
}
nextChar = charArray.shift();
}
return openStack.reverse().map(open => OPEN_TO_CLOSE[open]).join('');
}
const mapErrorCharToCost = (closeChar) => CORRUPT_COST[closeChar];
const getCorruptCost = (input) => input
.map(determineFirstCorruptCharacter)
.filter(el => el)
.map(mapErrorCharToCost)
.reduce((sum, x) => sum + x, 0);
const getMiddleIncompleteAggregation = (input) => input
.map(determineIncompleteLine)
.filter(el => el)
.map(computeClosingSequence)
.map(str => str.split(''))
.map(arr => arr.reduce((agg, x) => agg * 5 + INCOMPLETE_COST[x], 0))
.sort((a,b) => a - b)
.filter((el, idx, arr) => idx === Math.floor(arr.length / 2));
console.log({
test: {
corruptCost: getCorruptCost(test),
incompleteChars: getMiddleIncompleteAggregation(test),
},
data: {
corruptCost: getCorruptCost(data),
incompleteChars: getMiddleIncompleteAggregation(data)
},
});