class Day11(input: String) { private val origin = HexSpot(0, 0, 0) private val steps = input.split(",") fun solvePart1(): Int = steps .fold(origin) { spot, dir -> spot.travel(dir) } .distanceTo(origin) fun solvePart2(): Int = steps .fold(listOf(origin)) { path, dir -> path + (path.last().travel(dir)) } .map { it.distanceTo(origin) } .max() ?: 0 } class HexSpot(private val x: Int, private val y: Int, private val z: Int) { fun travel(direction: String): HexSpot = when (direction) { "n" -> HexSpot(x, y + 1, z - 1) "s" -> HexSpot(x, y - 1, z + 1) "ne" -> HexSpot(x + 1, y, z - 1) "nw" -> HexSpot(x - 1, y + 1, z) "se" -> HexSpot(x + 1, y - 1, z) "sw" -> HexSpot(x - 1, y, z + 1) else -> throw IllegalArgumentException("Invalid direction: $direction") } fun distanceTo(there: HexSpot): Int = maxOf( (this.x - there.x).absoluteValue, (this.y - there.y).absoluteValue, (this.z - there.z).absoluteValue ) }