# Advent of Code 2018 - Day 25, in Kotlin

Kotlin solutions to parts 1 and 2 of Advent of Code 2018, Day 25: 'Four-Dimensional Adventure'

Posted on

The last day of Advent of Code 2018 is finally here, and with is is the traditional one part problem that isn’t so hard you have to spend all day on it!

If you’d rather just view code, the GitHub Repository is here .

Problem Input

We will pull in input as a `List<String>` and parse it further in part 1.

#### ⭐ Day 25, Part 1

The puzzle text can be found here.

First, let’s create another point class, this time for 4-dimensions! I decided to call the 4th dimension `t` for time, but you can really pick anything for today, they don’t really have much meaning. In principle we could go back and refactor `Point`, `Point3d` and `Point4d` into a single n-dimensional point class, but I didn’t want to have to refactor all of the code that depends on it. Maybe before Advent of Code 2019 starts I’ll do that to prepare.

``````class Day25(rawInput: List<String>) {

private val points: List<Point4d> = rawInput.map { Point4d.of(it) }

private data class Point4d(val x: Int, val y: Int, val z: Int, val t: Int) {

fun distanceTo(other: Point4d): Int =
abs(x - other.x) + abs(y - other.y) + abs(z - other.z) + abs(t - other.t)

companion object {
fun of(input: String): Point4d {
val (x, y, z, t) = input.split(",").map { it.trim().toInt() }
return Point4d(x, y, z, t)
}
}
}
}
``````

The `distanceTo` function should look familiar, as should our parsing logic once again using an `of` function in the companion. This time parsing is pretty straight forward - split our `String` on comma and turn the parts into `Int`s.

Next, we need to make a map of each point and what its immediate neighbors are. Remember, we consider something a neighbor if it is 3 or fewer units away (and it is not the same point).

``````// In Day25

private fun mapNeighbors(): Map<Point4d, Set<Point4d>> =
points.map { point ->
Pair(point, points.filterNot { it == point }.filter { point.distanceTo(it) <= 3 }.toSet())
}.toMap()
``````

Now that we have our neighborhood plotted, let’s talk about how we turn them into constellations. Let’s say we have some point A whose neighbors are B and C. We know that at a minimum, A, B, and C represent a constellation because B and C are within 3 units of A. Taking that one step further, anything that is a neighbor of B is also part of that constellation. The same goes for C. Therefore our approach is going to be to pick a point and add all of its neighbors to a constellation. Add all of the neighbors of those to it, and keep doing that until we stop adding new points. Poof! Constellation. Repeat until we run out of points to look at.

The code I wrote to do that is here, you can experiment and write it differently if you’d like:

``````// In Day25

private fun constellations(neighbors: Map<Point4d, Set<Point4d>>): Sequence<Set<Point4d>> = sequence {
val allPoints = neighbors.keys.toMutableList()
while (allPoints.isNotEmpty()) {
val point = allPoints.removeAt(0)
val thisConstellation = mutableSetOf(point)
val foundNeighbors = ArrayDeque<Point4d>(neighbors.getValue(point))
while (foundNeighbors.isNotEmpty()) {
foundNeighbors.removeFirst().also {
allPoints.remove(it) // Not the basis of a new constellation
thisConstellation.add(it)   // Because it is part of our constellation

// And all this point's neighbors are therefore part of our constellation too
foundNeighbors.addAll(neighbors.getValue(it).filterNot { other -> other in thisConstellation })
}
}
// We've run out of found neighbors to check, this is a constellation.
yield(thisConstellation)
}
}
``````

As you can see, we’ll implement this with a queue (`foundNeighbors`), and keep adding its neighbors to the queue until we run out of things that aren’t already in the constellation. We’ll implement this as a sequence, but we could just keep a counter if we wanted as that’s all the problem asks for.

Running this solves our final puzzle of Advent of Code 2018:

``````// In Day25

fun solvePart1(): Int =
constellations(mapNeighbors()).count()

``````

Star earned!

Thanks for following along with me! I enjoyed solving and writing about these problems. Reach out and let me know what you think (good or bad).