-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.js
70 lines (64 loc) · 1.97 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
const fs = require("fs")
const charMap = {
")": { inValidScore: 3, missingScore: 1, pair: "(" },
"]": { inValidScore: 57, missingScore: 2, pair: "[" },
"}": { inValidScore: 1197, missingScore: 3, pair: "{" },
">": { inValidScore: 25137, missingScore: 4, pair: "<" },
}
const closingChars = Object.keys(charMap)
const data = fs
.readFileSync("./data/day10.txt", "utf8")
.split("\n")
.reduce(
(acc, line) => {
const open = []
for (let i = 0; i < line.length; i++) {
const char = line.charAt(i)
//if it's opening char just push it into the array and continue if this isn't the last iteration
if (!closingChars.includes(char)) {
open.push(char)
if (i < line.length - 1) continue
}
//if the string is invalid push it to the invalidLines array if it's not the last line
if (open.at(-1) !== charMap[char]?.pair && i < line.length - 1) {
acc["invalidLines"].push(char)
break
}
if (i === line.length - 1) {
if (closingChars.includes(char)) {
open.pop()
}
open.reverse()
acc["incompleteLines"].push([...open])
break
}
open.pop()
}
return acc
},
{ invalidLines: [], incompleteLines: [] }
)
console.log(data)
part1Answer = data["invalidLines"].reduce((acc, char) => {
acc += charMap[char].inValidScore
return acc
}, 0)
part2Answer = data["incompleteLines"]
.reduce((acc, chars) => {
acc.push(
chars.reduce((score, char) => {
missingScore = Object.entries(charMap).reduce((acc, e) => {
if (e[1].pair === char) {
acc += e[1].missingScore
}
return acc
}, 0)
score = score * 5 + missingScore
return score
}, 0)
)
return acc
}, [])
.sort((a, b) => (a < b ? 1 : -1))[Math.floor(data["incompleteLines"].length / 2)]
console.log(`Part 1: ${part1Answer}`)
console.log(`Part 2: ${part2Answer}`)