Skip to content

Commit 04dd43a

Browse files
committed
Day09 - Part 2
1 parent 4a4fe45 commit 04dd43a

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

src/main/kotlin/Day09.kt

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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") {

src/test/kotlin/Day09Test.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Day09Test : DayTest<Day09>("Day09_test.txt") {
22
override val partOneExpected = 50L
3-
override val partTwoExpected = null
3+
override val partTwoExpected = 24L
44
}

0 commit comments

Comments
 (0)