Advent of Code 2023 day 23
12/23/2024, 5:00 AMMichael de Kaste
12/23/2024, 6:01 AMDan Fingal-Surma
12/23/2024, 6:04 AMkingsley
12/23/2024, 6:18 AMJonathan Kolberg
12/23/2024, 6:22 AMJakub Gw贸藕d藕
12/23/2024, 6:26 AMtypealias Input = Map<String, Set<String>>
fun part2(graph: Input): String {
val result = mutableSetOf<Set<String>>()
graph.keys.forEach { result += setOf(it) }
while (result.size > 1) {
val newResult = mutableSetOf<Set<String>>()
val asList = result.toList()
asList.forEach { s1 ->
graph.filter { (v,es)-> v !in s1 && es.containsAll(s1) }
.forEach { newResult += s1 + it.key }
}
result.clear()
result += newResult
}
return result.first().sorted().joinToString(",")
}
Jakub Gw贸藕d藕
12/23/2024, 6:30 AMaa-bb
cc-dd
aa-cc
aa-dd
my UF would first group aa
with bb
, then cc
with dd
, but not aa
-cc
-dd
ephemient
12/23/2024, 6:40 AMMarcin Wisniowski
12/23/2024, 6:42 AMephemient
12/23/2024, 6:43 AMmapOf("aa" to setOf("bb", "cc"), "bb" to setOf("cc"), "cc" to setOf())
where the values are limited to those which are lexicographically greater than the key. that means my searching code doesn't have to handle any loops or duplicatesJakub Gw贸藕d藕
12/23/2024, 6:49 AMJakub Gw贸藕d藕
12/23/2024, 6:53 AMbj0
12/23/2024, 7:21 AMDan Fingal-Surma
12/23/2024, 7:21 AMbj0
12/23/2024, 7:21 AMbj0
12/23/2024, 7:22 AMDan Fingal-Surma
12/23/2024, 7:25 AMJakub Gw贸藕d藕
12/23/2024, 7:40 AMbj0
12/23/2024, 7:55 AMbj0
12/23/2024, 7:56 AMbj0
12/23/2024, 7:57 AMbj0
12/23/2024, 8:11 AMbj0
12/23/2024, 8:15 AMbj0
12/23/2024, 8:16 AMDan Fingal-Surma
12/23/2024, 8:24 AMDan Fingal-Surma
12/23/2024, 8:24 AMDan Fingal-Surma
12/23/2024, 8:24 AMDan Fingal-Surma
12/23/2024, 8:24 AMfun Set<String>.fullyConnected() = map { connections.getValue(it) }.reduce(Set<String>::intersect)
Dan Fingal-Surma
12/23/2024, 8:24 AMfun Set<Set<String>>.next(): Set<Set<String>> {
val n = mutableSetOf<Set<String>>()
for (s in this) {
for (candidate in s.fullyConnected()) {
n.add(s + candidate)
}
}
return n
}
Dan Fingal-Surma
12/23/2024, 8:25 AMDan Fingal-Surma
12/23/2024, 8:26 AMDan Fingal-Surma
12/23/2024, 8:30 AMJakub Gw贸藕d藕
12/23/2024, 8:45 AMclass Box<T>(var value: T)
fun <V> bronKerbosch(r: List<V>, p: List<V>, x: List<V>, graph: Map<V, Set<V>>, result: Box<List<V>>) {
if (p.isEmpty() && x.isEmpty()) {
if (result.value.size < r.size) result.value = r
return
}
val pivot = p.firstOrNull()?:x.first()
val p1 = p.toMutableList()
val x1 = x.toMutableList()
(p - graph[pivot]!!).forEach { v ->
val es = graph[v]!!
bronKerbosch(r + v, p1.filter { it in es }, x1.filter { it in es }, graph, result)
p1 -= v
x1 += v
}
}
fun part2(graph: Map<String, Set<String>>): Any {
val result = Box(emptyList<String>())
bronKerbosch(emptyList(), graph.keys.toList(), emptyList(), graph, result)
return result.value.joinToString(",")
}
3ms, I need to take a break nowphldavies
12/23/2024, 9:10 AMWarming up 1 puzzles over 15s for year 2024 day 23...
Warmup finished after 15.000042584s with 12709 iterations
year 2024 day 23 part 1
Default took 594.475us 馃憫: 1062
year 2024 day 23 part 2
Default took 574.32us 馃憫: bz,cs,fx,ms,oz,po,sy,uh,uv,vw,xu,zj,zm
Dan Fingal-Surma
12/23/2024, 9:12 AMDan Fingal-Surma
12/23/2024, 9:13 AMDan Fingal-Surma
12/23/2024, 9:19 AMDan Fingal-Surma
12/23/2024, 9:23 AMphldavies
12/23/2024, 10:03 AMJaap Beetstra
12/23/2024, 10:05 AMHCP
12/23/2024, 11:38 AMHCP
12/23/2024, 11:39 AMfun part2(): String {
// Function to recursively look along the chain of neighbours, finding sets of computers
// that all have each other as neighbours
fun findAllSets(neighbour: String, prevNeighbours: MutableList<String>): List<List<String>> {
val foundSets = mutableSetOf<List<String>>()
if (graph[neighbour]!!.containsAll(prevNeighbours)) {
// this computer has all previous computers in the chain as neighbours, so add it to the set
// and continue the search on unvisited neighbours
prevNeighbours.add(neighbour)
graph[neighbour]!!.forEach { nextNeighbour ->
if (!prevNeighbours.contains(nextNeighbour)) {
foundSets += findAllSets(nextNeighbour, prevNeighbours)
}
}
return foundSets.toList()
} else {
// This computer doesn't have all previous computers in the set as neighbours, so the set is complete.
foundSets += prevNeighbours
return foundSets.toList()
}
}
val allSets = mutableListOf<List<String>>()
// Iterate through all the computers and their neighbours looking for "Sets of connected computers"
// Have since learned, these sets are called "cliques" in Graph theory.
graph.forEach { (comp, neighbours) ->
val prevNeighbours = mutableListOf(comp)
neighbours.forEach { neighbour ->
allSets += findAllSets(neighbour, prevNeighbours)
}
}
// return the largest set as a comma separated list ie. the LAN Party password
return allSets.maxBy { it.size }.sortedBy { it }.joinToString(",")
}
Max Thiele
12/23/2024, 11:55 AMMax Thiele
12/23/2024, 11:56 AMkingsley
12/23/2024, 12:25 PMkingsley
12/23/2024, 12:25 PMphldavies
12/23/2024, 12:41 PMWarming up 3 puzzles for 15s each for year 2024 day 23...
BitSet warmed up with 23839 iterations
Default warmed up with 15991 iterations
Kingsley warmed up with 2606 iterations
year 2024 day 23 part 1
BitSet took 458.908us 馃憫: 1062
Default took 495.233us (1.08x): 1062
Kingsley took 1.456684ms (3.17x): 1062
year 2024 day 23 part 2
BitSet took 198.283us 馃憫: bz,cs,fx,ms,oz,po,sy,uh,uv,vw,xu,zj,zm
Default took 486.706us (2.45x): bz,cs,fx,ms,oz,po,sy,uh,uv,vw,xu,zj,zm
Kingsley took 4.306325ms (21.72x): bz,cs,fx,ms,oz,po,sy,uh,uv,vw,xu,zj,zm
kingsley
12/23/2024, 12:49 PMphldavies
12/23/2024, 1:22 PMyear 2024 day 23 part 1
BitSet took 94.455us 馃憫: 1062
Default took 288.479us (3.05x): 1062
Kingsley took 1.134040ms (12.01x): 1062
year 2024 day 23 part 2
BitSet took 83.13us 馃憫: bz,cs,fx,ms,oz,po,sy,uh,uv,vw,xu,zj,zm
Default took 262.556us (3.16x): bz,cs,fx,ms,oz,po,sy,uh,uv,vw,xu,zj,zm
Kingsley took 3.512550ms (42.25x): bz,cs,fx,ms,oz,po,sy,uh,uv,vw,xu,zj,zm
kingsley
12/23/2024, 1:32 PMMax Thiele
12/23/2024, 2:01 PM<
by a <=
Enough for today!phldavies
12/23/2024, 2:12 PMyear 2024 day 23 part 1
BitSet took 196.226us 馃憫: 1062
year 2024 day 23 part 2
BitSet took 185.713us 馃憫: bz,cs,fx,ms,oz,po,sy,uh,uv,vw,xu,zj,zm
bj0
12/23/2024, 4:33 PMbj0
12/23/2024, 4:35 PMaa-ab
aa-ba
ba-ca
ca-aa
phldavies
12/23/2024, 4:51 PMbj0
12/23/2024, 4:54 PMDan Fingal-Surma
12/23/2024, 6:16 PMBitset
, same algorithm, see what speed up we getPaul Woitaschek
12/23/2024, 6:29 PMphldavies
12/23/2024, 6:42 PMcontext(PuzzleInput) fun part2() = parse { network ->
var best = emptyList<String>()
val splits = ArrayDeque<Int>()
val lan = ArrayDeque<String>()
for ((first, nodes) in network) {
if (nodes.size < best.lastIndex) continue
val bestThreshold = (nodes.size / 2) + 1
splits.add(0)
while (splits.isNotEmpty()) {
val split = splits.removeFirst()
for (idx in split downTo 0) {
val next = nodes[idx]
if (lan.all { it in network[next].orEmpty() }) lan.addFirst(next)
}
lan.addFirst(first)
for (idx in split + 1..nodes.lastIndex) {
val next = nodes[idx]
if (lan.all { next in network[it].orEmpty() }) {
lan.addLast(next)
splits.remove(idx)
} else splits.add(idx)
}
if (lan.size > best.size) best = lan.toList()
if (lan.size > bestThreshold) splits.clear()
lan.clear()
}
}
best.joinToString(",")
}
@bj0 fixed (I think) - still ~190us so I'm happybj0
12/23/2024, 6:47 PMbj0
12/23/2024, 6:47 PMbj0
12/23/2024, 6:48 PMphldavies
12/23/2024, 6:54 PMaa-ab
aa-ac
aa-ba
aa-cb
ab-ac
ba-ca
cb-ca
cb-ba
ca-aa
part 2 gets "aa,ac,ba,ca,cb" when ba and ac aren't connectedbj0
12/23/2024, 7:06 PMbj0
12/23/2024, 7:06 PMadd((buildSet {
add(first)
add(second)
for (next in connected - visited) {
if (all { next in network[it].orEmpty() }) add(next)
}
}).also { visited += it })
phldavies
12/23/2024, 7:24 PMBrianFix took 1.258929ms (6.50x): bz,cs,fx,ms,oz,po,sy,uh,uv,vw,xu,zj,zm
Dan Fingal-Surma
12/23/2024, 7:50 PMphldavies
12/23/2024, 8:38 PMDan Fingal-Surma
12/23/2024, 9:21 PMDan Fingal-Surma
12/23/2024, 9:25 PMphldavies
12/23/2024, 10:43 PMWarming up 2 puzzles for 5s each for year 2024 day 23...
BitSet warmed up with 37760 iterations
Default warmed up with 6001 iterations
year 2024 day 23 part 1
BitSet took 73.336us 馃憫: 1062
Default took 483.22us (6.59x): 1062
year 2024 day 23 part 2
BitSet took 65.698us 馃憫: bz,cs,fx,ms,oz,po,sy,uh,uv,vw,xu,zj,zm
Default took 332.059us (5.05x): bz,cs,fx,ms,oz,po,sy,uh,uv,vw,xu,zj,zm
no cached parsing - 74us and 66usDan Fingal-Surma
12/24/2024, 4:36 AMephemient
12/24/2024, 4:51 AM