Published
Edited
Dec 3, 2019
1 star
Insert cell
Insert cell
html`<svg viewBox="-10000 -10000 20000 20000" width="${width}" height="${width}">
<circle cx="0" cy="0" r="50" fill="green" />
<path d="${path(wires[0])}" stroke="black" stroke-width="5" fill="none" />
<path d="${path(wires[1])}" stroke="blue" stroke-width="5" fill="none" />
</svg>`
Insert cell
path = wire =>
[
`M0,0`,
...wire.map(s =>
s
.replace('R', 'h')
.replace('L', 'h-')
.replace('U', 'v')
.replace('D', 'v-')
)
].join('')
Insert cell
coords = wire => {
let last = [0, 0];
let step = 0;
const points = {};
for (const segment of wire) {
const n = +segment.slice(1);
switch (segment[0]) {
case 'R':
for (let d = 0; d < n; d++) {
last[0]++;
step++;
points[last] = points[last] || step;
}
break;
case 'L':
for (let d = 0; d < n; d++) {
last[0]--;
step++;
points[last] = points[last] || step;
}
break;
case 'D':
for (let d = 0; d < n; d++) {
last[1]--;
step++;
points[last] = points[last] || step;
}
break;
case 'U':
for (let d = 0; d < n; d++) {
last[1]++;
step++;
points[last] = points[last] || step;
}
break;
}
}
return points;
}
Insert cell
steps = wires.map(coords)
Insert cell
part2 = intersections
.map(p => steps[0][p] + steps[1][p])
.sort((a, b) => a - b)[0]
Insert cell
part1 = intersections
.map(([x, y]) => Math.abs(x) + Math.abs(y))
.sort((a, b) => a - b)[0]
Insert cell
intersections = _.intersection(points[0], points[1]).map(s =>
s.split(',').map(Number)
)
Insert cell
points = steps.map(Object.keys)
Insert cell
wires = input.split('\n').map(l => l.split(','))
Insert cell
test = `R75,D30,R83,U83,L12,D49,R71,U7,L72
U62,R66,U55,R34,D71,R55,D58,R83`
Insert cell
Insert cell
_ = require('lodash')
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