Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
bag of intersections solution in Clear category for Mountain Scape by juestr
from typing import Counter, Iterable, Optional, Tuple
Top = Tuple[int, int]
def intersect(t1: Top, t2: Top) -> Optional[Top]:
(x1, y1), (x2, y2) = (t1, t2) if t1 < t2 else (t2, t1)
dx, dy = x2 - x1, y2 - y1
return \
(dx <= abs(dy)) and ((x1, y1) if dy > 0 else (x2, y2)) or \
(y1 > (d := abs(dx - dy) // 2)) and (x1 + d, y1 - d) or \
None
def mountain_scape(tops: Iterable[Top]) -> int:
bag: Counter[Top] = Counter()
for top in tops:
for top2, n in list(bag.items()):
if n and (new := intersect(top, top2)):
bag.subtract({new: n})
bag.update([top])
return sum(y**2 * n for (_, y), n in bag.items())
Dec. 29, 2020