Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
convert to x-axis coordinates and remove intersections solution in Clear category for Mountain Scape by kdim
from typing import List, Tuple
def mountain_scape(tops: List[Tuple[int, int]]) -> int:
# convert tops to begin/end of mountanis on x-axis
coord = [(x - y, x + y) for x, y in tops]
coord = sorted(coord, key=lambda x: (x[0], -x[1]))
# remove all small mountains that completely cross large mountains
cleared = list()
while coord:
cleared.append(coord[0])
coord = coord[1:]
while coord and cleared[-1][0] <= coord[0][0] and cleared[-1][1] >= coord[0][1]:
coord = coord[1:]
# sum of total area
total = sum((x2 - x1) ** 2 / 4 for x1, x2 in cleared)
# sum of intersections
intersect = sum((x2 - x1) ** 2 / 4 for (_, x2), (x1, _) in zip(cleared, cleared[1:]) if x2 > x1)
return total - intersect
if __name__ == '__main__':
print("Example:")
print(mountain_scape([(1, 1), (4, 2), (7, 3)]))
# These "asserts" are used for self-checking and not for an auto-testing
assert mountain_scape([(1, 1), (4, 2), (7, 3)]) == 13
assert mountain_scape([(0, 2), (5, 3), (7, 5)]) == 29
assert mountain_scape([(1, 3), (5, 3), (5, 5), (8, 4)]) == 37
print("Coding complete? Click 'Check' to earn cool rewards!")
Aug. 17, 2022