-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.mjs
39 lines (33 loc) · 1.04 KB
/
day10.mjs
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
import fs from 'node:fs/promises'
const opener = ['(', '[', '{', '<']
const closer = [')', ']', '}', '>']
const input = await fs.readFile('./inputs/day10.txt', 'utf-8')
const lines = input.split('\n').filter(str => str.length).map(str => str.split(''))
let completerScores = []
let offenders = closer.reduce((acc, char) => Object.assign(acc, { [char]: 0 }), {})
for (let line of lines) {
let stack = []
for (let char of line) {
const opos = opener.indexOf(char)
if (opos > -1)
stack.unshift(opos)
else if (stack.shift() != closer.indexOf(char)) {
offenders[char]++
stack = []
break
}
}
if (stack.length)
completerScores.push(
stack.reduce(
(acc, val) => acc * 5 + val + 1,
0
)
)
}
console.log(
offenders[')'] * 3 + offenders[']'] * 57 + offenders['}'] * 1197 + offenders['>'] * 25137
)
console.log(
completerScores.sort((a, b) => a - b)[Math.floor(completerScores.length / 2)]
)