Last active
July 17, 2019 10:48
-
-
Save Sawtaytoes/66358a14f3f502045dace5f60962a21b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getContiguousIds = ({ | |
contiguousIds = [], | |
node, | |
nodes, | |
}) => ( | |
node | |
.adjacentIds | |
.reduce( | |
( | |
contiguousIds, | |
adjacentId, | |
) => ( | |
contiguousIds | |
.includes(adjacentId) | |
? contiguousIds | |
: ( | |
getContiguousIds({ | |
contiguousIds, | |
node: ( | |
nodes | |
.find(({ | |
id, | |
}) => ( | |
id | |
=== adjacentId | |
)) | |
), | |
nodes, | |
}) | |
) | |
), | |
( | |
contiguousIds | |
.concat( | |
node | |
.id | |
) | |
), | |
) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getLargestContiguousNodes = ( | |
nodes, | |
) => ( | |
nodes | |
.reduce( | |
( | |
prevState, | |
node, | |
) => { | |
if ( | |
prevState | |
.scannedIds | |
.includes(node.id) | |
) { | |
return prevState | |
} | |
const contiguousIds = ( | |
getContiguousIds({ | |
node, | |
nodes, | |
}) | |
) | |
const { | |
largestContiguousIds, | |
scannedIds, | |
} = prevState | |
return { | |
largestContiguousIds: ( | |
contiguousIds.length | |
> largestContiguousIds.length | |
? contiguousIds | |
: largestContiguousIds | |
), | |
scannedIds: ( | |
scannedIds | |
.concat(contiguousIds) | |
), | |
} | |
}, | |
{ | |
largestContiguousIds: [], | |
scannedIds: [], | |
}, | |
) | |
.largestContiguousIds | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment