diff --git a/2024/kotlin/src/main/kotlin/Main.kt b/2024/kotlin/src/main/kotlin/Main.kt index cf98ae2..f2cb821 100644 --- a/2024/kotlin/src/main/kotlin/Main.kt +++ b/2024/kotlin/src/main/kotlin/Main.kt @@ -22,6 +22,7 @@ import com.tymscar.day19.solve as day19 import com.tymscar.day20.solve as day20 import com.tymscar.day21.solve as day21 import com.tymscar.day22.solve as day22 +import com.tymscar.day23.solve as day23 fun main() { day01() @@ -46,4 +47,5 @@ fun main() { day20() day21() day22() + day23() } \ No newline at end of file diff --git a/2024/kotlin/src/main/kotlin/com/tymscar/day23/part1/part1.kt b/2024/kotlin/src/main/kotlin/com/tymscar/day23/part1/part1.kt index 279270c..b70efe8 100644 --- a/2024/kotlin/src/main/kotlin/com/tymscar/day23/part1/part1.kt +++ b/2024/kotlin/src/main/kotlin/com/tymscar/day23/part1/part1.kt @@ -1,5 +1,38 @@ package com.tymscar.day23.part1 + +private fun getAllConnected(from: String, map: Map>, groupCount: Int): List { + var groups = listOf(listOf(from)) + + while (groups.first().count() < groupCount) { + groups = groups.flatMap { group -> + map[group.last()]!! + .filter { !group.contains(it) } + .map { group + it } + } + } + + return groups + .filter { group -> + group.all { computer -> + group.all { peer -> + map[computer]!!.contains(peer) || computer == peer + } + } + }.map { it.sorted().joinToString(",") } +} + fun solve(input: String): String { - return input + val map = input + .lines() + .flatMap { + val (from, towards) = it.split("-") + listOf(from to towards, towards to from) + }.groupBy({ it.first }, { it.second }) + + return map.keys.filter { it[0] == 't' } + .flatMap { getAllConnected(it, map, 3) } + .toSet() + .count() + .toString() } diff --git a/2024/kotlin/src/main/kotlin/com/tymscar/day23/part2/part2.kt b/2024/kotlin/src/main/kotlin/com/tymscar/day23/part2/part2.kt index 2326661..acc7165 100644 --- a/2024/kotlin/src/main/kotlin/com/tymscar/day23/part2/part2.kt +++ b/2024/kotlin/src/main/kotlin/com/tymscar/day23/part2/part2.kt @@ -1,5 +1,39 @@ package com.tymscar.day23.part2 + +fun getAllCliques( + graph: Map>, + currentClique: Set, + remainingNodes: MutableSet, + visitedNodes: MutableSet +): List> { + if (remainingNodes.isEmpty() && visitedNodes.isEmpty()) return listOf(currentClique) + val results = mutableListOf>() + + remainingNodes.toList().forEach { v -> + val neighbours = graph[v]?.toSet() ?: emptySet() + results.addAll(getAllCliques( + graph, + currentClique + v, + remainingNodes.intersect(neighbours).toMutableSet(), + visitedNodes.intersect(neighbours).toMutableSet() + )) + remainingNodes.remove(v) + visitedNodes.add(v) + } + return results +} + fun solve(input: String): String { - return input + val map = input + .lines() + .flatMap { + val (from, towards) = it.split("-") + listOf(from to towards, towards to from) + }.groupBy ({ it.first }, { it.second }) + + return getAllCliques(map, emptySet(), map.keys as MutableSet, mutableSetOf()) + .maxBy { it.count() } + .sorted() + .joinToString(",") }