Skip to content

Instantly share code, notes, and snippets.

@thedeemon
Created February 23, 2026 10:42
Show Gist options
  • Select an option

  • Save thedeemon/d8df30420f3e57bcb853250b6c06ab5c to your computer and use it in GitHub Desktop.

Select an option

Save thedeemon/d8df30420f3e57bcb853250b6c06ab5c to your computer and use it in GitHub Desktop.
Direct translation from .e1 original
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
import sys
@dataclass
class Point:
x: int
y: int
@dataclass
class Border:
bx: int
up: bool
@dataclass
class Rect:
a: int
b: int
area: int
def parse_int_or(s: str, default: int) -> int:
try:
return int(s)
except ValueError:
return default
def by_line(text: str) -> list[str]:
if text == "":
return []
parts = text.split("\n")
if parts and parts[-1] == "":
parts.pop()
return parts
def to_points(line: str) -> Point:
v = [parse_int_or(s, -1) for s in line.split(",")]
return Point(v[0], v[1])
def area(points: list[Point], i: int, j: int) -> int:
a = points[i]
b = points[j]
return (abs(a.x - b.x) + 1) * (abs(a.y - b.y) + 1)
def process_line(lst: list[Border]) -> list[Border]:
arr = list(lst)
arr.sort(key=lambda b: b.bx)
return [arr[i] for i in range(len(arr)) if i == 0 or arr[i].up != arr[i - 1].up]
def fits(st: int, en: int, line: list[Border]) -> bool:
return any(
line[i].up
and not line[i + 1].up
and line[i].bx <= st
and line[i + 1].bx >= en
for i in range(len(line) - 1)
)
def main() -> None:
input_path = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("input9t.txt")
text = input_path.read_text() if input_path.exists() else ""
points = [to_points(line) for line in by_line(text)]
np = len(points)
lines: list[list[Border]] = [[] for _ in range(100000)]
def draw_vert_line(x: int, first: int, last: int, going_up: bool) -> None:
for y in range(first, last + 1):
lines[y].insert(0, Border(x, going_up))
for i in range(np):
j = (i + 1) % np
pi = points[i]
pj = points[j]
going_up = pi.y > pj.y
if pi.x == pj.x:
draw_vert_line(pi.x, min(pi.y, pj.y), max(pi.y, pj.y), going_up)
spans = [process_line(line) for line in lines]
def rect_fits(r: Rect) -> bool:
p1 = points[r.a]
p2 = points[r.b]
st = min(p1.x, p2.x)
en = max(p1.x, p2.x)
y0 = min(p1.y, p2.y)
y1 = max(p1.y, p2.y)
for y in range(y0, y1 + 1):
if not fits(st, en, spans[y]):
return False
return True
rects = [
Rect(j, i + 1, area(points, j, i + 1))
for i in range(np - 1)
for j in range(i + 1)
]
rects = [r for r in rects if rect_fits(r)]
rects.sort(key=lambda r: r.area, reverse=True)
print(rects[0].area)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment