-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.js
86 lines (68 loc) · 2.02 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
'use strict' // Analyzing braced syntax.
const { assert, loadData } = require('./core/utils')
const rawInput = [loadData(module.filename)]
const bracesA = '([{<', bracesB = ')]}>'
const costs = [3, 57, 1197, 25137]
/**
* @param {string | string[]} line
* @return {{cost: number, missing: string}}
*/
const analyze = (line) => {
for (let row = Array.from(line), iB; ; row.splice(iB - 1, 2)) {
let i, cB, last, missing
if ((iB = row.findIndex(c => !bracesA.includes(cB = c))) < 0) {
if ((last = row.pop())) {
missing = bracesB[bracesA.indexOf(last)]
}
return { cost: 0, missing }
}
assert((i = bracesB.indexOf(cB)) >= 0)
if (row[iB - 1] !== (missing = bracesA[i])) {
return { cost: costs[i], missing }
}
}
}
const puzzle1 = (data) => {
let sum = 0
for (const line of data) {
sum += analyze(line).cost
}
return sum
}
const puzzle2 = (data) => {
let scores = []
for (const line of data) {
let cost, missing, row = Array.from(line), score = 0
while (({ cost, missing } = analyze(row)) && !cost && missing) {
score = 5 * score + bracesB.indexOf(missing) + 1
row.push(missing)
}
score && scores.push(score)
}
return scores.sort((a, b) => a - b)[Math.floor(scores.length / 2)]
}
const parse = (dsn) => {
let data = rawInput[dsn]
if (data && (data = data.split('\n').filter(v => Boolean(v))).length) {
return data
}
return data // NOTE: The runner will distinguish between undefined and falsy!
}
// Example (demo) data.
rawInput[1] = `
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]
`
module.exports = { parse, puzzles: [puzzle1, puzzle2] }
/*
"demo": { "1": { "value": 26397, "time": 341 }, "2": { "value": 288957, "time": 1279 } },
"main": { "1": { "value": 341823, "time": 5313 }, "2": { "value": 2801302861, "time": 20117 } }
*/