Public
Edited
Dec 28, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
solvePart1 = fp.flow(
parsePairs,
fp.map(pair => compare(...pair)),
generateChecksum
)
Insert cell
generateChecksum = comparisons => comparisons.reduce((acc, comparison, i) => acc + (comparison <= 0 ? i + 1 : 0), 0)
Insert cell
solvePart1(input)
Insert cell
Insert cell
findIndexOfDivider = (array, n) => array.findIndex(x => x.length === 1 && x[0].length === 1 && x[0][0] === n) + 1
Insert cell
getDecoderKey = array => findIndexOfDivider(array, 2) * findIndexOfDivider(array, 6)
Insert cell
solvePart2 = fp.flow(
parsePairs,
fp.flatten,
fp.concat([[[2]], [[6]]]),
x => x.sort(compare),
getDecoderKey
)
Insert cell
solvePart2(input)
Insert cell
Insert cell
parsePair = token => token.split('\n').map(text => JSON.parse(text))
Insert cell
parsePairs = text => text.split('\n\n').map(parsePair)
Insert cell
clamp = (min, max, value) => Math.min(max, Math.max(min, value))
Insert cell
Insert cell
compare = {
const recursiveCompare = (a, b) => {
if (typeof a === 'number' && typeof b === 'number') {
return clamp(-1, 1, a - b)
}
if (typeof a === 'number') return recursiveCompare([a], b)
if (typeof b === 'number') return recursiveCompare(a, [b])

const firstDefinitiveComparison = a.reduce((acc, _, i) => (acc || b[i] === undefined) ? acc : recursiveCompare(a[i], b[i]), 0)

return firstDefinitiveComparison || recursiveCompare(a.length, b.length)
}

return recursiveCompare
}
Insert cell
Insert cell
Insert cell
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