Published
Edited
Dec 10, 2019
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
md`**${counts[0].count}**`
Insert cell
Insert cell
Insert cell
input2map = input => input.split('\n').map(r => r.split(''))
Insert cell
inputmap = input2map(puzzleInput)
Insert cell
coords = map2coordlist(inputmap)
Insert cell
angles = coords2angles(coords, {x:1, y:0})
Insert cell
uniqueangles = uniqueAngles(angles)
Insert cell
map2coordlist = map => {
return map.reduce(
(acc, row, y) => acc.concat(
row.map((space, x) => space == '#' ? {x: x, y: y} : space).filter(v => v != ".")
), []);
}
Insert cell
coords2angles = (coordlist, origin) => {
let angles = coordlist.map( xy => {
let dx = xy.x - origin.x;
let dy = xy.y - origin.y;
let strike = Math.atan2(dy, dx) + Math.PI/2; // y is down
strike = strike < 0 ? strike + 2 * Math.PI : strike; // 0 needs to be smallest for sorting
return {angle: dx == 0 && dy == 0 ? -999 : strike, sqdst: dx*dx + dy*dy, x:xy.x, y:xy.y}
});
return angles
}
Insert cell
uniqueAngles = angles => {
return new Set(
angles.map( ad => ad.angle ))
}
Insert cell
findStation = input => {
let coords = map2coordlist(input2map(input));
let stationCounts = coords.map(s => {
let angles = coords2angles(coords, s);
return {
count: uniqueAngles(angles).size-1,
angles: angles,
station: s}});
return stationCounts.sort((a,b) => a.count-b.count).reverse();
}
Insert cell
Insert cell
md`The multplied coordinates are **${a200.x*100+a200.y}**.`
Insert cell
Insert cell
a200 = sorted[199]
Insert cell
mostAngles = findStation(puzzleInput)[0]
Insert cell
sorted = {
let firstCount = mostAngles.count;
let u = 0, v = -1, prevAngle;
return Array.from(mostAngles.angles.filter(a => a.angle != -999))
.sort((a, b) => { //sort by angle, then distance
let d = a.angle - b.angle;
d = d == 0 ? a.sqdst - b.sqdst : d;
return d })
.map(angle => { // rearrange
if (angle.angle != prevAngle) { u = 0; v++ }
angle.order = u++ * firstCount + v; // compute order
prevAngle = angle.angle;
return angle
}).sort((a, b) => a.order - b.order); // sort by order
}

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