Skip to content

Latest commit

 

History

History
40 lines (31 loc) · 2.96 KB

spidey_swings_across_town.md

File metadata and controls

40 lines (31 loc) · 2.96 KB

Spider-Man ("Spidey") needs to get across town for a date with Mary Jane and his web-shooter is low on web fluid. He travels by slinging his web rope to latch onto a building rooftop, allowing him to swing to the opposite end of the latch point.

Write a function that, when given a list of buildings, returns a list of optimal rooftop latch points for minimal web expenditure.

Input

Your function will receive an array whose elements are subarrays in the form [h,w] where h and w represent the height and width, respectively, of each building in sequence

Output

An array of latch points (integers) to get from 0 to the end of the last building in the input list

Technical Details

  • An optimal latch point is one that yields the greatest horizontal distance gain relative to length of web rope used. Specifically, the highest horizontal distance yield per length unit of web rope used. Give this value rounded down to the nearest integer as a distance from Spidey's origin point (0)
  • At the start of each swing, Spidey selects a latch point based on his current position.
  • At the end of each swing, Spidey ends up on the opposite side of the latch point equal to his distance from the latch point before swing.
  • Spidey's initial altitude is 50, and will always be the same at the start and end of each swing. His initial horizontal position is 0.
  • To avoid collision with people/traffic below, Spidey must maintain a minimum altitude of 20 at all times.
  • Building height (h) range limits: 125 <= h < 250
  • Building width (w) range limits: 50 <= w <= 100
  • Inputs will always be valid.

Test Example

test example image

  • Spidey's initial position is at 0, marked by the Spidey icon on the left. His first latch point is marked by the green circle at 76 (horizontal position) on buildings[0].
  • At the end of the swing, Spidey's position is at the point marked B with a horizontal position of 152. The green arc represents Spidey's path during swing.
  • The orange circle on the 3rd building and the orange arc going from point B to point C represent the latch point (258) and arc path for the next swing.
let buildings = [[162,76], [205,96], [244,86], [212,68], [174,57], [180,89], [208,70], [214,82], [181,69], [203,67]];

spideySwings(buildings); //[76,258,457,643,748]
buildings = [[162,76], [205,96], [244,86], [212,68], [174,57], [180,89], [208,70], [214,82], [181,69], [203,67]]

spidey_swings(buildings)# [76,258,457,643,748]

If you enjoyed this kata, be sure to check out my other katas.