-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10Part2.js
136 lines (127 loc) · 4.42 KB
/
day10Part2.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
125
126
127
128
129
130
131
132
133
134
135
136
function readInput(fileName) {
const fs = require('fs');
const inputArray = [];
try {
const data = fs.readFileSync(fileName, 'UTF-8');
const lines = data.split(/\r?\n/);
lines.forEach((line) => {
inputArray.push(line);
});
} catch (err) {
console.error(err);
}
console.log(inputArray);
return inputArray;
}
function checkBrackets(fileName) {
const bracketArray = readInput(fileName); // I should not have named it this
const incompleteLines = [];
const corruptedLines = [];
const illegalCharacters = [];
let matchingBracketObject = {
'\(' : '\)',
'\[' : '\]',
'\{' : '\}',
'\<' : '\>'
};
bracketArray.forEach(line => {
let bracketQueue = [];
// iterate through characters
for (let i in line) {
// for each opening bracket, add to queue of brackets that need matching
// for each closing bracket, deque - if the next to deque is not matching, add to illegal characters
// (might be best to use push/pop)
if (line[i] === '\{' || line[i] === '\(' || line[i] === '\<' || line[i] === '\[') {
bracketQueue.push(line[i]);
// console.log('bracketQueue', bracketQueue);
} else {
// current bracket is not an opening bracket, so check it matches the last opening bracket
const lastOpeningBracket = bracketQueue.pop();
if (matchingBracketObject[lastOpeningBracket] !== line[i]) {
corruptedLines.push(line);
illegalCharacters.push(line[i]);
return;
}
}
}
});
bracketArray.forEach(line => {
if (!corruptedLines.includes(line)) {
incompleteLines.push(line);
}
});
console.log('incompleteLines', incompleteLines);
return incompleteLines;
}
function completeLines(fileName) {
const incompleteLines = checkBrackets(fileName);
let endsOfLines = [];
let matchingBracketObject = {
'\(' : '\)',
'\[' : '\]',
'\{' : '\}',
'\<' : '\>'
};
incompleteLines.forEach(line => {
let bracketQueue = [];
let endOfLine = [];
// iterate through characters
for (let i in line) {
// for each opening bracket, add to queue of brackets that need matching
// for each closing bracket, deque - if the next to deque is not matching, add to illegal characters
// (might be best to use push/pop)
if (line[i] === '\{' || line[i] === '\(' || line[i] === '\<' || line[i] === '\[') {
bracketQueue.push(line[i]);
// console.log('bracketQueue', bracketQueue);
} else {
// current bracket is not an opening bracket, so check it matches the last opening bracket
const lastOpeningBracket = bracketQueue.pop();
if (matchingBracketObject[lastOpeningBracket] !== line[i]) {
corruptedLines.push(line);
illegalCharacters.push(line[i]);
return;
}
}
}
while (bracketQueue.length > 0) {
const lastBracket = bracketQueue.pop();
endOfLine.push(matchingBracketObject[lastBracket]);
}
endsOfLines.push(endOfLine);
});
console.log(endsOfLines);
return endsOfLines;
}
function addUpScore(fileName) {
const endsOfLines = completeLines(fileName);
const scores = {
'\)' : 1,
'\]' : 2,
'\}' : 3,
'\>' : 4
};
let totalScores = [];
endsOfLines.forEach(line => {
let totalScore = 0;
line.forEach(character => {
totalScore = totalScore*5;
totalScore+=scores[character];
});
totalScores.push(totalScore);
});
return totalScores;
}
function getMiddleScore(fileName) {
let scores = addUpScore(fileName);
scores = scores.sort((a, b) => a - b);
return scores[(scores.length-1)/2];
}
function runTests(fileName) {
// readInput(fileName);
// checkBrackets(fileName);
completeLines(fileName);
console.log(addUpScore(fileName));
console.log(getMiddleScore(fileName));
}
runTests('day10TestInput.txt'); // scores: [ 288957, 5566, 1480781, 995444, 294 ] // middle score: 288957
runTests('day10Input.txt'); // 2380061249