Skip to content

Commit 8196ed3

Browse files
committed
Refactoring Day 14 to use entropy to find the least random configuration of points to detect the tree
1 parent 6b21d78 commit 8196ed3

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/main/kotlin/adventofcode/day14/RestroomRedoubt.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ fun List<Point2D>.calculateShannonEntropy(maxX: Int, maxY: Int, gridWidth: Int,
9191
.sumOf { -it * ln(it) }
9292
}
9393

94+
private const val GRID_WIDTH = 10
95+
private const val GRID_HEIGHT = 10
9496

9597
fun String.findTree(): Int {
9698
val robots = parseRobots()
@@ -99,7 +101,7 @@ fun String.findTree(): Int {
99101
val sequences = robots.map { it.points(maxX, maxY) }
100102

101103
val minEntropyT = (1..lcmInt(maxX, maxY)).minBy { t ->
102-
sequences.map { it.next() }.calculateShannonEntropy(maxX, maxY, 10, 10)
104+
sequences.map { it.next() }.calculateShannonEntropy(maxX, maxY, GRID_WIDTH, GRID_HEIGHT)
103105
}
104106
robots.map { it.move(minEntropyT, maxX, maxY) }.toSet().printGrid(maxX, maxY)
105107
return minEntropyT

0 commit comments

Comments
 (0)