Skip to content

Commit 5c16915

Browse files
fix(2019-day-03): inconsistent wire intersection lists
Shared origin of two wires does not consistently show up in the list of intersections when wires are longer and more complex. See bpmn-io/path-intersection#10
1 parent 5c567f6 commit 5c16915

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

2019/day-03/wires.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,16 @@ const isCloser = (intA, intB) => {
4747

4848
const getClosesetIntersection = (intersections) => {
4949
intersections.sort(isCloser)
50-
// Skip the origin since all wires start at origin
51-
return intersections[1]
50+
51+
// TODO: Remove workaround for bug in SVG intersection library
52+
// https://github.com/bpmn-io/path-intersection/issues/10
53+
//
54+
// The shared origin inconsistently shows up in the intersection list
55+
if (parseInt(intersections[0].x) === 0 && parseInt(intersections[0].y) === 0) {
56+
// Skip the shared origin since all wires start at origin
57+
return intersections[1]
58+
}
59+
return intersections[0]
5260
}
5361

5462
module.exports = {

2019/day-03/wires.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,20 @@ describe('--- 2019 Day 3: Crossed Wires ---', () => {
4040
expect(actual).to.deep.equal(expected)
4141
})
4242
})
43+
it('can be used to find the distance to the closest intersection', () => {
44+
const wiresets = [
45+
[
46+
'R75,D30,R83,U83,L12,D49,R71,U7,L72',
47+
'U62,R66,U55,R34,D71,R55,D58,R83'
48+
], [
49+
'R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51',
50+
'U98,R91,D20,R16,D67,R40,U7,R15,U6,R7'
51+
]
52+
]
53+
const distances = [159, 135]
54+
wiresets.forEach((wires, idx) => {
55+
expect(getClosesetIntersection(findWireIntersections(wires)).distance).to.equal(distances[idx])
56+
})
57+
})
4358
})
4459
})

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
require('./2019/day-02/solution')
1+
require('./2019/day-03/solution')

0 commit comments

Comments
 (0)