SpinalHDL
1 / 1
Description
Tunnel Survey
Intelligence reports indicate that smugglers have dug multiple underground tunnels beneath a border wall. Each tunnel runs parallel to the wall, compromising a continuous segment of its foundation.
Your surveillance drones have mapped the tunnels. Each tunnel is described by its start and end positions (in meters from the western edge of the wall). Tunnels may overlap - a section of wall undermined by multiple tunnels is still just one compromised section.
Given a stream of up to 256 tunnel ranges (start, end inclusive), calculate the total meters of wall that are compromised (covered by at least one tunnel).
Example: Tunnels [(5, 15), (10, 20), (30, 40)]
- Tunnel 1 covers meters 5-15 (11 meters)
- Tunnel 2 covers meters 10-20 (11 meters), overlapping with tunnel 1
- Tunnel 3 covers meters 30-40 (11 meters), separate from the others
Merged coverage: 5-20 (16 meters) + 30-40 (11 meters) = 27 meters
Answer: 27
Input
tunnels
Stream of 64-bit Unsigned Integer PairTunnel ranges (start, end inclusive).
Output
64-bit Unsigned Integer
Total meters of wall compromised.