@@ -4,7 +4,7 @@ class Day09 : Day {
44 override fun partOne (filename : String , verbose : Boolean ): Long {
55 val redTiles = filename.asPath().readLines().map {
66 val (x, y) = it.split(" ," )
7- Coordinate (x.toInt (), y.toInt ())
7+ LongCoordinate (x.toLong (), y.toLong ())
88 }
99 if (verbose) {
1010 val minX = redTiles.minOf { it.x }
@@ -14,7 +14,7 @@ class Day09 : Day {
1414
1515 (minY.. maxY).forEach { y ->
1616 (minX.. maxX).forEach { x ->
17- if (Coordinate (x, y) in redTiles) {
17+ if (LongCoordinate (x, y) in redTiles) {
1818 print (' #' )
1919 } else {
2020 print (' .' )
@@ -27,9 +27,8 @@ class Day09 : Day {
2727
2828 redTiles.forEachIndexed { index, firstTile ->
2929 redTiles.drop(index + 1 ).forEach { secondTile ->
30- val ySize = maxOf(firstTile.y, secondTile.y) - minOf(firstTile.y, secondTile.y) + 1
31- val xSize = maxOf(firstTile.x, secondTile.x) - minOf(firstTile.x, secondTile.x) + 1
32- val size = ySize.toLong() * xSize.toLong()
30+ val rectangle = Rectangle (firstTile, secondTile)
31+ val size = rectangle.size
3332 if (size > largestRectangle) {
3433 largestRectangle = size
3534 }
@@ -38,8 +37,47 @@ class Day09 : Day {
3837 return largestRectangle
3938 }
4039
41- override fun partTwo (filename : String , verbose : Boolean ): Any {
42- TODO (" Not yet implemented" )
40+ override fun partTwo (filename : String , verbose : Boolean ): Long {
41+ val redTiles = filename.asPath().readLines().map {
42+ val (x, y) = it.split(" ," )
43+ LongCoordinate (x.toLong(), y.toLong())
44+ }
45+
46+ val lines = redTiles.mapIndexed { index, a ->
47+ Line (a, redTiles[(index + 1 ) % redTiles.size])
48+ }
49+
50+ var max = 0L
51+ redTiles.forEachIndexed { index, firstTile ->
52+ var localMax = 0L
53+ redTiles.drop(index + 1 ).forEach { secondTile ->
54+ val probe = Rectangle (firstTile, secondTile).size
55+ if (probe > localMax) {
56+ if (lines.none { it intersects Line (firstTile, secondTile) }) {
57+ localMax = probe
58+ }
59+ }
60+ }
61+ max = maxOf(max, localMax)
62+ }
63+ return max
64+ }
65+
66+ private infix fun Line.intersects (line2 : Line ): Boolean {
67+ return line2.maxX > minX && line2.minX < maxX && line2.maxY > minY && line2.minY < maxY
68+ }
69+
70+ private data class Line (val a : LongCoordinate , val b : LongCoordinate ) {
71+ val minX = minOf(a.x, b.x)
72+ val maxX = maxOf(a.x, b.x)
73+ val minY = minOf(a.y, b.y)
74+ val maxY = maxOf(a.y, b.y)
75+ }
76+
77+ private data class Rectangle (val a : LongCoordinate , val b : LongCoordinate ) {
78+ val width = maxOf(a.x, b.x) - minOf(a.x, b.x)
79+ val height = maxOf(a.y, b.y) - minOf(a.y, b.y)
80+ val size = (width + 1 ) * (height + 1 )
4381 }
4482
4583 companion object : Day .Main (" Day09.txt" ) {
0 commit comments