fun parseInput(input: List): Map>> { return input.map { conn -> conn.split("/").map(String::toInt) } .flatMap { listOf(it[0] to (it[0] to it[1]), it[1] to (it[0] to it[1])) } .groupBy { it.first } .map { it.key to it.value.map { it.second }.distinct() } .toMap() } fun getPossibleConnections(connMap: Map>>, currentPin: Int = 0, currentConnection: Set> = emptySet(), connections: MutableSet>>) { connections.add(currentConnection) connMap[currentPin] ?.filter { it !in currentConnection && currentConnection + it !in connections } ?.forEach { val newConn = currentConnection + it val newPin = if (it.first != currentPin) it.first else it.second getPossibleConnections(connMap, newPin, newConn, connections) } } fun getConnectionValue(connection: Set>): Int { return connection.map { it.first + it.second }.sum() } fun getBestBridge(bridges : MutableSet>>): Int? { val maxSize = bridges.maxBy { it.size }?.size return bridges.filter { it.size == maxSize }.map(::getConnectionValue).max() } fun main(args: Array) { val rawInput = resourceToList("day24_electromagnetic_moat.txt") val connMap = parseInput(rawInput) val connections = mutableSetOf>>() getPossibleConnections(connMap, connections = connections) println(connections.size) println(connections.map(::getConnectionValue).max()) println(getBestBridge(connections)) }