Notebooks 2.0 is here.

Public
Edited
Dec 29, 2022
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
map = parseMap(input)
Insert cell
isVisible = R.curry((map, coords) => {
const currentHeight = parseInt(map.data[coordsToIndex(map.size, coords)])
return R.pipe(
traceAll(map),
Object.values,
R.map(x => !x.length || x.every(tree => tree < currentHeight)),
R.any(R.identity)
)(coords)
})
Insert cell
Insert cell
getVisibleTrees = map => R.addIndex(R.count)(R.pipe((x, i) => indexToCoords(map.size, i), isVisible(map)))(map.data)
Insert cell
getVisibleTrees(map)
Insert cell
Insert cell
exampleMap = parseMap(exampleInput)
Insert cell
Insert cell
getScenicScore = R.curry((map, coords) => {
const currentHeight = parseInt(map.data[coordsToIndex(map.size, coords)])
return R.pipe(
traceAll(map),
Object.values,
R.map(R.pipe(
R.identity,
R.reduce((visible, tree) => tree < currentHeight ? visible.concat(tree) : R.reduced(visible.concat(tree)), []),
R.length
)),
R.flatten,
R.reduce(R.multiply, 1)
)(coords)
})
Insert cell
Insert cell
getBestLocation = map => Math.max(...R.addIndex(R.map)(R.pipe((x, i) => indexToCoords(map.size, i), getScenicScore(map)))(map.data))
Insert cell
getBestLocation(map)
Insert cell
Insert cell
cardinalDirections = ({
right: [1, 0],
left: [-1, 0],
down: [0, 1],
up: [0, -1]
})
Insert cell
parseMap = s => ({
data: s.replace(/\n/g, ''),
size: [s.indexOf('\n'), s.split('\n').length]
})
Insert cell
// for debugging
trace(map, [1, 0], [3, 0])
Insert cell
traceAll = R.curry((map, coords) => R.map(R.pipe(
direction => trace(map, direction, coords),
R.map(parseInt),
), cardinalDirections))
Insert cell
trace = R.curry((map, direction, startCoords) => {
const items = []
let currentCoords = startCoords

let timeout = 9999
while (timeout > 0) {
currentCoords = add(currentCoords, direction)
if (!isInMap(map.size, currentCoords)) break

items.push(map.data[coordsToIndex(map.size, currentCoords)])

timeout--
}

return items
})
Insert cell
indexToCoords = R.curry(([mapWidth, mapHeight], index) => [index % mapWidth, Math.floor(index / mapWidth)])
Insert cell
coordsToIndex = R.curry(([mapWidth, mapHeight], [x, y]) => y * mapWidth + x)
Insert cell
isInMap = R.curry(([mapWidth, mapHeight], [x, y]) => isBetween(0, mapWidth - 1, x) && isBetween(0, mapHeight - 1, y))
Insert cell
isBetween = R.curry((min, max, value) => value >= min && value <= max)
Insert cell
add = R.pipe(R.zip, R.map(R.sum))
Insert cell
getMapSize = s => [s.indexOf('\n'), s.split('\n').length]
Insert cell
Insert cell
import {adventOfCodeHeader, renderInput, R} from '@senritsu/advent-of-code-2022'
Insert cell
import {describe, it, expect} from '@senritsu/unit-testing'
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