Published
Edited
Dec 12, 2020
1 fork
1 star
Insert cell
Insert cell
{
return emoji().map(l => l.join('')).join('\n')
}
Insert cell
emoji = () => map.map(l => l.map(c => c === '#' ? '🌲' : ' '))
Insert cell
map = input.trim().split('\n').map(l => l.split(''))
Insert cell
step = ([y, x], [dy, dx]) => {
x += dx
y += dy
return { next: [y, x], tree: check([y, x]) === '#' ? 1 : 0 }
}
Insert cell
check = ([y, x]) => {
const row = map[y]
return row ? row[x % row.length] : ''
}
Insert cell
function* run(delta) {
let current = [0, 0]
while (current[0] < map.length) {
const {next, tree} = step(current, delta)
yield {next, tree}
current = next
}
}
Insert cell
total = (delta) => {
let total = 0
for (const {tree} of run(delta))
total += tree
return total
}
Insert cell
part1 = total([1, 3])
Insert cell
part2 = [
[1, 1], [1, 3], [1, 5], [1, 7], [2, 1]
].map(total)
.reduce((product, i) => product * i)
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