Published
Edited
Dec 18, 2019
1 star
Insert cell
Insert cell
Insert cell
parseroids = d => d.split("\n").map(x => x.split(""))
.map((row, rowindex) => row.map((col,colindex) => ({x: colindex, y: rowindex, roid: col=="#" || col.match(/[a-z]/ig)}))) // parse map string as objects with x, y, and isroid properties
.reduce((acc, inc) => acc.concat(inc), []) // flatten 2d array
Insert cell
roidsLOS = roidbelt => point => {
const belt = roidbelt.slice()
.filter(z => z.roid && !(z.x==point.x && z.y==point.y)) // just roids, exclude empty space
.map(z => {z.angle = Math.atan2(z.y - point.y, z.x - point.x); return z}) // angle of point to roid
.map(z => {z.dist = Math.sqrt(Math.pow(z.x - point.x, 2) + Math.pow(z.y - point.y, 2)); return z}) // dist
const uniqueAngles = new Set(belt.map(z => z.angle)) // all unique roid angles (exclude duplicates)
const closest = Array.from(uniqueAngles)
.map(uniqueAngle => belt.filter(roid => roid.angle==uniqueAngle) //find all roids for each unique angle
.reduce((acc, inc) => inc.dist < acc.dist? inc : acc)) // get closest of those
return closest
}
Insert cell
Insert cell
Insert cell
solve1 = d => {
const roidfield = parseroids(d)
return roidfield
.filter(space => space.roid)
.map(roid => {roid.detects = roidsLOS(roidfield)(roid).length; return roid})
.reduce((acc, inc) => inc.detects > acc.detects ? inc : acc)
}
Insert cell
[testdata1,testdata2,testdata3,testdata4]
.map(solve1)
.map(site => `${site.x},${site.y}`)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
solve1(data)
Insert cell
Insert cell
solve2 = d => {
const allroids = roidsLOS(parseroids(d))(solve1(d))
.map(z => {z.angle *= 180/Math.PI; return z})
.sort((a,b) => a.angle - b.angle)
.map(z => {z.angle+=90; return z}) // offset so that "north" is zero degrees
const positives = allroids.filter(roid => roid.angle >= 0)
const negs = allroids.filter(roid => roid.angle < 0)
const clockwiseRoids = positives.concat(negs).map((z,i) => {z.vaped = i+1; return z})
const twoHundredthRoid = clockwiseRoids.find(z => z.vaped==200)
return twoHundredthRoid.x * 100 + twoHundredthRoid.y
}
Insert cell
solve2(testdata4)
Insert cell
solve2(data)
Insert cell
Insert cell
margin = ({top: 20, bottom: 15, left: 0, right: 0})
Insert cell
x = d3.scaleBand().domain(d3.range(0,43)).range([margin.left,width-margin.right])
Insert cell
y = d3.scaleBand().domain(d3.range(0,43)).range([margin.top,height-margin.bottom])
Insert cell
width = 500
Insert cell
height = 500
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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