Public
Edited
Dec 7, 2022
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
pairs = splitIntoLines(input).map(line => line.split(',').map(parseRange).map(rangeToFlags))
Insert cell
pairsWithFullyContainedRanges = pairs.filter(pairwiseEither(isSuperset)).length
Insert cell
Insert cell
getPairs = R.pipe(
splitIntoLines,
R.map(R.pipe(
R.split(','),
R.map(R.pipe(
parseRange,
rangeToFlags
))
))
)
Insert cell
isOneContainedInTheOther = R.pipe(
pair => [pair, R.reverse(pair)],
R.map(R.apply(isSubset)),
R.apply(R.or),
)
Insert cell
solvePart1 = R.pipe(
getPairs,
R.map(isOneContainedInTheOther),
R.count(R.identity),
)
Insert cell
solvePart1(input)
Insert cell
Insert cell
overlappingPairs = pairs.filter(([first, second]) => first & second).length
Insert cell
Insert cell
solvePart2 = R.pipe(
getPairs,
R.map(([first, second]) => first & second),
R.count(R.identity),
)
Insert cell
solvePart2(input)
Insert cell
Insert cell
pairwiseEither = R.curry((f, [a, b]) => f(a, b) || f(b, a))
Insert cell
isSuperset = (a, b) => a === (a | b)
Insert cell
isSubset = (a, b) => a === (a & b)
Insert cell
parseRange = (s) => s.split('-').map(Number)
Insert cell
/**
* Convert a one-based numeric range (both ends inclusive) into a corresponding bitmask, setting all bits whose index is included in the range
* e.g., the range [2, 4] is represented as 0b1110 (first bit unset, bits 2 through 4 set), while the range [5, 6] would be represented as 0b110000 (bits 1 through 4 unset, bits 5 and 6 set)
*/
rangeToFlags = ([a, b]) => {
return BigInt(`0b${'1'.repeat(b - a + 1)}${'0'.repeat(a)}`)
}
Insert cell
Insert cell
import {R, splitIntoLines} from '@senritsu/advent-of-code-2022-day-3'
Insert cell
import {adventOfCodeHeader, renderInput} from '@senritsu/advent-of-code-2022'
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