Skip to content

Commit 173ab6d

Browse files
refactor(2019-day-03): support multiple ways of finding closest intersection
1 parent 7a6c55b commit 173ab6d

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

2019/day-03/wires.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,33 @@ const findWireIntersections = (wires) => {
2727
return { x: parseInt(point.x), y: parseInt(point.y) }
2828
})
2929

30-
return ints.sort(isCloser)
30+
return ints.sort(isCloser.manhattan)
3131
}
3232

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
33+
const isCloser = {
34+
manhattan: (intA, intB) => {
35+
const origin = { x: 0, y: 0 }
36+
intA.distance = distance(origin, intA)
37+
intB.distance = distance(origin, intB)
38+
if (intA.distance < intB.distance) {
39+
return -1
40+
}
41+
if (intA.distance > intB.distance) {
42+
return 1
43+
}
44+
if (intA.distance === intB.distance) {
45+
return 0
46+
}
3947
}
40-
if (intA.distance > intB.distance) {
41-
return 1
42-
}
43-
if (intA.distance === intB.distance) {
44-
return 0
48+
}
4549
}
4650
}
4751

48-
const getClosesetIntersection = (intersections) => {
49-
intersections.sort(isCloser)
52+
const getClosesetIntersection = ({
53+
intersections,
54+
method = 'manhattan'
55+
}) => {
56+
intersections.sort(isCloser[method])
5057

5158
// TODO: Remove workaround for bug in SVG intersection library
5259
// https://github.com/bpmn-io/path-intersection/issues/10

0 commit comments

Comments
 (0)