Opening your workspace…
Workspace/Coding room
Opening your workspace…
You spot two little white ears moving across the bottom of a tide pool. A minute later a second pair comes out from under the same rock. They're sea bunnies, a kind of sea slug, and you've been asked a surprisingly difficult question about them: do they all take the same paths?
There's a square grid under the camera. For each slug, you've marked the grid points where its trail starts and ends. The trails in this photograph run horizontally, vertically, or at 45 degrees. Count every grid point along a trail, including both ends. You're looking for places visited by at least two different slugs, even if one passed through hours before the other.
Two trails can cross without sharing a grid point. Imagine drawing an X across one little square: the lines meet dead center, but none of the four corners gets a second visitor. Return the shared grid points just once each, in ascending order of x, then y.
[[0,0,4,4],[4,0,0,4],[2,0,2,4]]Output[[2,2]]Both diagonals cross the vertical trail at [2,2]. All three slugs visit that point, so it appears once.
[[4,1,1,1],[2,1,5,1],[1,3,1,1]]Output[[1,1],[2,1],[3,1],[4,1]]The horizontal trails share three points from x = 2 through x = 4. The vertical trail also touches the first slug's trail at [1,1].
Walk each trail at integer steps and count visits from different trails.
function solve(trails) { // Write your solution here. }
Tab inserts two spaces. Shift+Tab outdents. Use the arrow keys and Tab to choose a suggestion. Press Escape, then Tab, to leave the editor. Press Control or Command with apostrophe to run samples or with Enter to submit.