val p03 = fun() { val claims = input_3.lines().map { line -> line.split("#", "@", ",", ":", "x") .filter { it.isNotEmpty() } .map { it.trim().toInt() }.let { Claim(it[0], it[1] + 1, it[2] + 1, it[3], it[4]) } }.asSequence() val common = mutableSetOf>() claims.forEachIndexed { index, claim -> claims.drop(index + 1).forEach { other -> if (other.intersects(claim)) { common.addAll(claim.pointsInCommon(other)) } } } println("Total square inch overlapping: ${common.count()}") claims.first { claim -> claims.none { it.intersects(claim) } } .print { "ID of non-overlapping claim: ${it.id}" } } data class Claim(val id: Int, val x: Int, val y: Int, val width: Int, val height: Int) { val points = (x until x + width).flatMap { x -> (y until y + height).map { y -> Pair(x, y) } } fun pointsInCommon(other: Claim) = points.intersect(other.points) fun intersects(other: Claim): Boolean { if (id == other.id) { return false } val (left, right) = if (x < other.x) Pair(this, other) else Pair(other, this) val (top, bottom) = if (y < other.y) Pair(this, other) else Pair(other, this) return (right.x >= left.x && right.x <= left.x + left.width - 1) && (bottom.y >= top.y && bottom.y <= top.y + top.height - 1) } }