-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 34 additions & 1 deletion
35
2024/kotlin/src/main/kotlin/com/tymscar/day23/part1/part1.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
2024/kotlin/src/main/kotlin/com/tymscar/day23/part2/part2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(",") | ||
} |