Skip to content

Commit 5c567f6

Browse files
feat(2019-day-03): utilities for finding wire intersections
1 parent 020e93f commit 5c567f6

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

2019/day-03/wires.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const intersection = require('path-intersection')
2+
const { distance } = require('../../2018/day-06/coordinates') // Manhattan distance function from last year
3+
4+
const elfWireToSVGPath = (path) => {
5+
const replacements = {
6+
R: 'h', // R(ight) becomes relative positive horizontal lineto
7+
L: 'h-', // L(eft) becomes relative negative horizontal lineto
8+
U: 'v-', // U(p) becomes relative negative vertical line
9+
D: 'v', // D(own) becomes relative positive vertical line
10+
',': ' ' // Separators are done with whitespace
11+
}
12+
path = path.trim()
13+
14+
const pattern = new RegExp(Object.keys(replacements).join('|'), 'gi')
15+
path = path.replace(pattern, (match) => {
16+
return replacements[match]
17+
})
18+
19+
return `M0,0 ${path}`
20+
}
21+
22+
const findWireIntersections = (wires) => {
23+
wires = wires.map(elfWireToSVGPath)
24+
const ints = intersection(
25+
...wires
26+
).map((point) => {
27+
return { x: parseInt(point.x), y: parseInt(point.y) }
28+
})
29+
30+
return ints.sort(isCloser)
31+
}
32+
33+
const isCloser = (intA, intB) => {
34+
const origin = { x: 0, y: 0 }
35+
intA.distance = distance(origin, intA)
36+
intB.distance = distance(origin, intB)
37+
if (intA.distance < intB.distance) {
38+
return -1
39+
}
40+
if (intA.distance > intB.distance) {
41+
return 1
42+
}
43+
if (intA.distance === intB.distance) {
44+
return 0
45+
}
46+
}
47+
48+
const getClosesetIntersection = (intersections) => {
49+
intersections.sort(isCloser)
50+
// Skip the origin since all wires start at origin
51+
return intersections[1]
52+
}
53+
54+
module.exports = {
55+
elfWireToSVGPath,
56+
findWireIntersections,
57+
getClosesetIntersection
58+
}

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "index.js",
66
"private": true,
77
"scripts": {
8+
"start": "node index.js",
89
"pretest": "npm run lint",
910
"test": "nyc mocha \"./20*/**/*.test.js\"",
1011
"posttest": "nyc report --reporter=html --reporter=text-lcov > coverage.lcov",
@@ -35,5 +36,8 @@
3536
"nyc": "^14.1.1",
3637
"semantic-release": "^15.13.31",
3738
"standard": "^14.3.1"
39+
},
40+
"dependencies": {
41+
"path-intersection": "^2.0.1"
3842
}
3943
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy