Published
Edited
Dec 10, 2021
Insert cell
Insert cell
Insert cell
syntaxErrorScore = {
let score = 0
for (let line of input) {
let testResult = testForCorruption(line)
if (testResult[1] !== null)
if (charValue.has(testResult[1]))
score += charValue.get(testResult[1])
else
return testResult
}
return score
}
Insert cell
safeOrIncomplete = input.filter( line => testForCorruption(line)[1] === null).map( line => testForCorruption(line))
Insert cell
charValue = new Map([
[")", 3],
["]", 57],
["}", 1197],
[">", 25137]
])
Insert cell
corruptedValue = {
let count = 0
for (let val of Object.entries(corruptedCount))
count += val[1] * charValue.get(val[0])
return count
}
Insert cell
3*10+1197*14+57*7+14*25137
Insert cell
369105-361125
Insert cell
corruptedCount = corrupted.reduce((accumulator, currentValue, index, array) => {
accumulator[currentValue[1]]++
return accumulator
}, {")":0, "}":0, "]":0, ">":0})
Insert cell
corrupted = input.filter( line => testForCorruption(line)[1] !== null).map( line => testForCorruption(line))
Insert cell
function testForCorruption(line) {
let stack = []
for (let [idx, char] of line.split("").entries()) {
switch(char) {
case "(":
case "{":
case "[":
case "<":
stack.push(char)
break
case ")":
if (stack[stack.length - 1] !== "(")
return [stack, ")", idx]
else
stack.pop()
break
case "]":
if (stack[stack.length - 1] !== "[")
return [stack, "]", idx]
else
stack.pop()
break
case "}":
if (stack[stack.length - 1] !== "{")
return [stack, "}", idx]
else
stack.pop()
break
case ">":
if (stack[stack.length - 1] !== "<")
return [stack, ">", idx]
else
stack.pop()
break
default:
return "error"
}
}
return [stack, null, null]
}
Insert cell
Insert cell
sortedScores[(sortedScores.length - 1) / 2]
Insert cell
sortedScores = completionScores.sort( (a,b) => a - b)
Insert cell
completionScores = safeOrIncomplete.map( analysis => computeCompletionScore(completeLine(analysis[0])))
Insert cell
function computeCompletionScore(completion) {
let charScore = new Map([
[")", 1],
["]", 2],
["}", 3],
[">", 4]
])
let score = 0
for (let char of completion) {
score *= 5
score += charScore.get(char)
}
return score
}
Insert cell
function completeLine(stack) {
let closingMatch = new Map([
["(", ")"],
["[", "]"],
["{", "}"],
["<", ">"]
])

return stack.reverse().map( opening => closingMatch.get(opening))
}
Insert cell
Insert cell
test = `[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]`.split("\n")
Insert cell
input = FileAttachment("10_input.txt").text().then( txt => txt.trim().split("\n"))
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more