Skip to content

Commit b30ae5b

Browse files
authored
Swap from apache math PolygonSet contains to java aws Area contains (#304)
1 parent 4309926 commit b30ae5b

File tree

1 file changed

+28
-14
lines changed
  • src/main/kotlin/me/peckb/aoc/_2025/calendar/day09

1 file changed

+28
-14
lines changed

src/main/kotlin/me/peckb/aoc/_2025/calendar/day09/Day09.kt

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package me.peckb.aoc._2025.calendar.day09
22

33
import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory
4-
import org.apache.commons.math3.geometry.euclidean.twod.PolygonsSet
5-
import org.apache.commons.math3.geometry.euclidean.twod.Vector2D
6-
import org.apache.commons.math3.geometry.euclidean.twod.hull.ConvexHullGenerator2D
7-
import org.apache.commons.math3.geometry.euclidean.twod.hull.MonotoneChain
4+
import java.awt.Polygon
5+
import java.awt.geom.Area
6+
import java.awt.geom.Rectangle2D
87
import javax.inject.Inject
98
import kotlin.math.abs
109
import kotlin.math.max
11-
10+
import kotlin.math.min
1211

1312
class Day09 @Inject constructor(
1413
private val generatorFactory: InputGeneratorFactory,
@@ -33,11 +32,20 @@ class Day09 @Inject constructor(
3332
}
3433

3534
fun partTwo(filename: String) = generatorFactory.forFile(filename).readAs(::day09) { input ->
36-
val vertices = input.map { Vector2D(it.x, it.y) }.toList()
37-
val polygonSet = PolygonsSet(1e-3, *vertices.toTypedArray())
38-
val hullGenerator: ConvexHullGenerator2D = MonotoneChain()
35+
val vertices = input.toList()
36+
37+
val xPoints = mutableListOf<Int>()
38+
val yPoints = mutableListOf<Int>()
39+
40+
vertices.forEach {
41+
xPoints.add(it.x.toInt())
42+
yPoints.add(it.y.toInt())
43+
}
3944

40-
val areas = mutableListOf<Pair<Long, List<Vector2D>>>()
45+
val polygon = Polygon(xPoints.toIntArray(), yPoints.toIntArray(), xPoints.size)
46+
val area = Area(polygon)
47+
48+
val areas = mutableListOf<Pair<Long, Pair<Location, Location>>>()
4149

4250
vertices.indices.forEach { ti1 ->
4351
((ti1 + 1) until vertices.size).forEach { ti2 ->
@@ -47,15 +55,21 @@ class Day09 @Inject constructor(
4755
if (t1.x == t2.x || t1.y == t2.y) { return@forEach }
4856

4957
val size = ((abs(t1.x - t2.x) + 1) * (abs(t1.y - t2.y) + 1)).toLong()
50-
val i1 = Vector2D(t1.x, t2.y)
51-
val i2 = Vector2D(t2.x, t1.y)
5258

53-
areas.add(size to listOf(t1, i1, t2, i2))
59+
areas.add(size to (t1 to t2))
5460
}
5561
}
5662

57-
areas.sortedByDescending { it.first }.first { (_, vertices) ->
58-
polygonSet.contains(hullGenerator.generate(vertices).createRegion())
63+
areas.sortedByDescending { it.first }.first { (_, corners) ->
64+
val t1 = corners.first
65+
val t2 = corners.second
66+
67+
val minX = min(t1.x, t2.x)
68+
val minY = min(t1.y, t2.y)
69+
val width = abs(t1.x - t2.x)
70+
val height = abs(t1.y - t2.y)
71+
72+
area.contains(Rectangle2D.Double(minX, minY, width, height))
5973
}.first
6074
}
6175

0 commit comments

Comments
 (0)