Skip to content

Commit

Permalink
Finish 2024 day 23 both parts
Browse files Browse the repository at this point in the history
  • Loading branch information
tymscar committed Dec 23, 2024
1 parent 856cdfb commit a54a20f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
2 changes: 2 additions & 0 deletions 2024/kotlin/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -46,4 +47,5 @@ fun main() {
day20()
day21()
day22()
day23()
}
35 changes: 34 additions & 1 deletion 2024/kotlin/src/main/kotlin/com/tymscar/day23/part1/part1.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
package com.tymscar.day23.part1


private fun getAllConnected(from: String, map: Map<String, List<String>>, groupCount: Int): List<String> {
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()
}
36 changes: 35 additions & 1 deletion 2024/kotlin/src/main/kotlin/com/tymscar/day23/part2/part2.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
package com.tymscar.day23.part2


fun getAllCliques(
graph: Map<String, List<String>>,
currentClique: Set<String>,
remainingNodes: MutableSet<String>,
visitedNodes: MutableSet<String>
): List<Set<String>> {
if (remainingNodes.isEmpty() && visitedNodes.isEmpty()) return listOf(currentClique)
val results = mutableListOf<Set<String>>()

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<String>, mutableSetOf())
.maxBy { it.count() }
.sorted()
.joinToString(",")
}

0 comments on commit a54a20f

Please sign in to comment.