Last active
February 23, 2026 11:51
-
-
Save thedeemon/c938cd6b989d9b2d1d0e2041f51a6d46 to your computer and use it in GitHub Desktop.
translated from Python version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| struct Point { | |
| let x: Int | |
| let y: Int | |
| } | |
| struct Border { | |
| let bx: Int | |
| let up: Bool | |
| } | |
| struct Rect { | |
| let a: Int | |
| let b: Int | |
| let area: Int | |
| } | |
| func byLine(_ text: String) -> [String] { | |
| if text.isEmpty { return [] } | |
| var parts = text.split(separator: "\n", omittingEmptySubsequences: false).map(String.init) | |
| if let last = parts.last, last.isEmpty { | |
| parts.removeLast() | |
| } | |
| return parts | |
| } | |
| func toPoint(_ line: String) -> Point { | |
| let vals = line.split(separator: ",").map { Int($0) ?? -1 } | |
| let x = vals.count > 0 ? vals[0] : -1 | |
| let y = vals.count > 1 ? vals[1] : -1 | |
| return Point(x: x, y: y) | |
| } | |
| func area(_ points: [Point], _ i: Int, _ j: Int) -> Int { | |
| let a = points[i] | |
| let b = points[j] | |
| return (abs(a.x - b.x) + 1) * (abs(a.y - b.y) + 1) | |
| } | |
| func processLine(_ lst: [Border]) -> [Border] { | |
| let arr = lst.sorted { $0.bx < $1.bx } | |
| var out: [Border] = [] | |
| for i in arr.indices { | |
| if i == 0 || arr[i].up != arr[i - 1].up { | |
| out.append(arr[i]) | |
| } | |
| } | |
| return out | |
| } | |
| func fits(_ st: Int, _ en: Int, _ line: [Border]) -> Bool { | |
| for i in 0..<(line.count - 1) { | |
| if line[i].up && !line[i + 1].up && line[i].bx <= st && line[i + 1].bx >= en { | |
| return true | |
| } | |
| } | |
| return false | |
| } | |
| let args = CommandLine.arguments | |
| let inputPath = args.count > 1 ? args[1] : "input9t.txt" | |
| let text: String | |
| if FileManager.default.fileExists(atPath: inputPath) { | |
| text = (try? String(contentsOfFile: inputPath, encoding: .utf8)) ?? "" | |
| } else { | |
| text = "" | |
| } | |
| let points = byLine(text).map(toPoint) | |
| let np = points.count | |
| var lines = Array(repeating: [Border](), count: 100_000) | |
| func drawVertLine(_ x: Int, _ first: Int, _ last: Int, _ goingUp: Bool) { | |
| for y in first...last { | |
| if y >= 0 && y < lines.count { | |
| lines[y].insert(Border(bx: x, up: goingUp), at: 0) | |
| } | |
| } | |
| } | |
| for i in 0..<np { | |
| let j = (i + 1) % np | |
| let pi = points[i] | |
| let pj = points[j] | |
| let goingUp = pi.y > pj.y | |
| if pi.x == pj.x { | |
| drawVertLine(pi.x, min(pi.y, pj.y), max(pi.y, pj.y), goingUp) | |
| } | |
| } | |
| let spans = lines.map(processLine) | |
| func rectFits(_ r: Rect) -> Bool { | |
| let p1 = points[r.a] | |
| let p2 = points[r.b] | |
| let st = min(p1.x, p2.x) | |
| let en = max(p1.x, p2.x) | |
| let y0 = min(p1.y, p2.y) | |
| let y1 = max(p1.y, p2.y) | |
| for y in y0...y1 { | |
| if !fits(st, en, spans[y]) { | |
| return false | |
| } | |
| } | |
| return true | |
| } | |
| var rects: [Rect] = [] | |
| for i in 0..<(np - 1) { | |
| for j in 0...i { | |
| rects.append(Rect(a: j, b: i + 1, area: area(points, j, i + 1))) | |
| } | |
| } | |
| rects = rects.filter(rectFits) | |
| rects.sort { $0.area > $1.area } | |
| print(rects[0].area) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment