|
| 1 | +from collections import namedtuple |
| 2 | +from itertools import combinations, product |
| 3 | +from matplotlib.path import Path |
| 4 | +from src.utils.data import load_data |
| 5 | +from src.utils.submission import submit_or_print |
| 6 | + |
| 7 | +Point = namedtuple("Point", ["x", "y"]) |
| 8 | +Segment = namedtuple("Segment", ["start", "end"]) |
| 9 | + |
| 10 | + |
| 11 | +def area(point1: Point, point2: Point) -> int: |
| 12 | + return (abs(point1.x - point2.x) + 1) * (abs(point1.y - point2.y) + 1) |
| 13 | + |
| 14 | + |
| 15 | +def no_points_inside(point1, point2, points): |
| 16 | + x1, x2 = point1.x, point2.x |
| 17 | + if x1 > x2: |
| 18 | + x1, x2 = x2, x1 |
| 19 | + y1, y2 = point1.y, point2.y |
| 20 | + if y1 > y2: |
| 21 | + y1, y2 = y2, y1 |
| 22 | + |
| 23 | + for point in points: |
| 24 | + if x1 < point.x < x2 and y1 < point.y < y2: |
| 25 | + return False |
| 26 | + return True |
| 27 | + |
| 28 | + |
| 29 | +def polygon(points): |
| 30 | + return [ |
| 31 | + Segment(point_from, point_to) |
| 32 | + for point_from, point_to in zip(points, points[1:] + points[0:1]) |
| 33 | + ] |
| 34 | + |
| 35 | + |
| 36 | +def ccw(A, B, C): |
| 37 | + return (C.y - A.y) * (B.x - A.x) > (B.y - A.y) * (C.x - A.x) |
| 38 | + |
| 39 | + |
| 40 | +def intersect(A, B, C, D): |
| 41 | + return ccw(A, C, D) != ccw(B, C, D) and ccw(A, B, C) != ccw(A, B, D) |
| 42 | + |
| 43 | + |
| 44 | +def intersects(lines1, lines2): |
| 45 | + print("checking...") |
| 46 | + print(lines1) |
| 47 | + print(lines2) |
| 48 | + |
| 49 | + for line1, line2 in product(lines1, lines2): |
| 50 | + if intersect(line1.start, line1.end, line2.start, line2.end): |
| 51 | + print("intersect!") |
| 52 | + print(line1, line2) |
| 53 | + return True |
| 54 | + print("no intersections") |
| 55 | + return False |
| 56 | + |
| 57 | + |
| 58 | +def rectangle(point1, point2): |
| 59 | + xs = sorted([point1.x, point2.x]) |
| 60 | + ys = sorted([point1.y, point2.y]) |
| 61 | + points = [ |
| 62 | + Point(xs[0], ys[0]), |
| 63 | + Point(xs[0], ys[1]), |
| 64 | + Point(xs[1], ys[1]), |
| 65 | + Point(xs[1], ys[0]), |
| 66 | + ] |
| 67 | + return polygon(points) |
| 68 | + |
| 69 | + |
| 70 | +def main(debug: bool) -> None: |
| 71 | + input_data = load_data(debug) |
| 72 | + |
| 73 | + points = [Point(*map(int, line.split(","))) for line in input_data.splitlines()] |
| 74 | + print(len(points), "points") |
| 75 | + |
| 76 | + result_part1 = max( |
| 77 | + area(point1, point2) for point1, point2 in combinations(points, 2) |
| 78 | + ) |
| 79 | + |
| 80 | + polygon = Path(points) |
| 81 | + print(polygon) |
| 82 | + result_part2 = max( |
| 83 | + area(point1, point2) |
| 84 | + for point1, point2 in combinations(points, 2) |
| 85 | + if not polygon.intersects_path(Path(rectangle(point1, point2))) |
| 86 | + ) |
| 87 | + |
| 88 | + print(polygon.contains_point((5, 5))) |
| 89 | + |
| 90 | + result_part2 = None |
| 91 | + xs = sorted({p.x for p in points}) |
| 92 | + ys = sorted({p.y for p in points}) |
| 93 | + |
| 94 | + print("Xs:", min(xs), max(xs), "all", xs) |
| 95 | + print("Ys:", min(ys), max(ys), "all", ys) |
| 96 | + |
| 97 | + x_map = {x: i for i, x in enumerate(xs)} |
| 98 | + y_map = {y: i for i, y in enumerate(ys)} |
| 99 | + |
| 100 | + mapped_points = [Point(x_map[p.x], y_map[p.y]) for p in points] |
| 101 | + print(points) |
| 102 | + print(mapped_points) |
| 103 | + |
| 104 | + submit_or_print(result_part1, result_part2, debug) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + debug_mode = True |
| 109 | + # debug_mode = False |
| 110 | + main(debug_mode) |
0 commit comments