Published
Edited
Dec 13, 2021
Insert cell
# Advent of code - Day 13
Insert cell
fold = (points, instructions, n) => {
let result = [...points]

instructions.forEach(({dir, pos}) => {
result
.filter(p => p[dir] > pos)
.forEach((p, i, arr) => {
arr[i][dir] = 2*pos - p[dir]
})
})

result = [...new Set(result.map(p => p.x+'-'+p.y))].map(p => p.split('-').map(v => parseInt(v)))
let [w,h] = ["x", "y"].map(dir => d3.min(instructions.filter(d => d.dir == dir).map(d => d.pos)))
return {result, w, h}
}
Insert cell
{
let { points, instructions } = parse(example)
let result = fold(points, instructions)

return { result, points, instructions }
}
Insert cell
{
let { points, instructions } = parse(puzzle)
let result = fold(points, instructions)

return { result, points, instructions }
}
Insert cell
{
let { points, instructions } = parse(puzzle)
let { result } = fold(points, instructions)

return Plot.cell(result).plot()
}
Insert cell
parse = (input) => {
let points = input.split("\n\n")[0].split("\n").map(l => ({ x: parseInt(l.split(',')[0]), y: parseInt(l.split(',')[1])}))
let instructions = input
.split("\n\n")[1]
.split('\n')
.map(l => l.split('='))
.map(arr => ({ dir: arr[0][arr[0].length-1], pos: parseInt(arr[1])}))

return { points, instructions, input}
}
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