use v6; # sliding-window() with possibility to adjust the sliding-speed: sub sliding-window(@scenery, \$width, \$speed) { gather for 0, \$speed ...^ * > +@scenery - \$width -> \$pos { take @scenery[ \$pos .. \$pos + \$width - 1 ]; } } my regex number { '-'? \d+ '.' \d+ } my @polygon = \$*IN.get.comb: /<&number>/; my (\$px, \$py) = \$*IN.get.comb: /<&number>/; if +@polygon < 6 || +@polygon % 2 or ! defined (\$px && \$py) { say 'invalid input'; exit; } # now, let us count the intersections of a straight line going down from the # point in question, and the polygon's edges. @polygon.push: @polygon[0,1]; # so that we get the Pn->P1 edge my \$intersections = 0; my \$ON_OUTLINE = 0; # should be: my constant \$ON_OUTLINE for sliding-window(@polygon, 4, 2) -> \$ax, \$ay, \$bx, \$by { next if \$ay & \$by > \$py # skip upper edges or \$ax & \$bx < \$px # skip left edges or \$ax & \$bx > \$px; # skip right edges # function representing the edge: Ey(x) = mx + b if \$bx - \$ax -> \$dx { my \$m = (\$by - \$ay) / \$dx; my \$b = \$ay - \$m * \$ax; given \$m * \$px + \$b { when * < \$py { ++\$intersections } when * == \$py { \$intersections = \$ON_OUTLINE; last; } } } elsif \$ay | \$by > \$py { \$intersections = \$ON_OUTLINE; last; } } say \$intersections % 2 ?? 'yes' !! 'no';