Skip to content

Commit

Permalink
2024 - Day 23 - part 1&2
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 23, 2024
1 parent e7abba6 commit 641c2c7
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/main/kotlin/no/rodland/advent_2024/Day23.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package no.rodland.advent_2024

import no.rodland.advent.Day

// template generated: 23/12/2024
// Fredrik Rødland 2024

class Day23(val input: List<String>) : Day<Int, String, List<Pair<String, String>>> {

private val parsed = input.parse()
private val computers = buildMap<String, MutableSet<String>> {
parsed.forEach { (first, second) ->
computeIfAbsent(first) { mutableSetOf() }.add(second)
computeIfAbsent(second) { mutableSetOf() }.add(first)
}
}

override fun partOne(): Int {
return groupsOfThree().filter { it.any { c -> c.startsWith("t") } }.size
}

override fun partTwo(): String {
return findLargestGroup().sorted().joinToString(",")
}

private fun groupsOfThree(): Set<List<String>> {
return computers
.mapNotNull { (computer, neighbours) ->
val neighboursList = neighbours.toList()
neighboursList.indices.mapNotNull { i ->
val restRange = i + 1 until neighboursList.size
restRange.mapNotNull { j ->
val neighbor1 = neighboursList[i]
val neighbor2 = neighboursList[j]
if (computers[neighbor1]?.contains(neighbor2) == true) {
setOf(computer, neighbor1, neighbor2).sorted()
} else {
null
}
}
}.flatten()
}
.flatten()
.toSet()
}

private fun findLargestGroup(): Set<String> {
return groups().maxBy { it.size }.sorted().toSet()
}

private fun groups(): List<MutableSet<String>> {
val visited = mutableSetOf<String>()
val groups = computers.keys.mapNotNull { computer ->
if (computer !in visited) {
val group = mutableSetOf(computer)
val neighbors = computers[computer].orEmpty()
for (neighbor in neighbors) {
if (group.all { computers[it]?.contains(neighbor) == true }) {
group.add(neighbor)
}
}
visited.addAll(group)
group
} else {
null
}
}
return groups
}

override fun List<String>.parse(): List<Pair<String, String>> {
return map { line ->
val (first, second) = line.split("-")
first to second
}
}

override val day = "23".toInt()
}
84 changes: 84 additions & 0 deletions src/test/kotlin/no/rodland/advent_2024/Day23Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package no.rodland.advent_2024

import no.rodland.advent.*
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import readFile

//
// run: download_aoc_input.sh to download input
//

@Suppress("ClassName")
@DisableSlow
internal class Day23Test {
private val data23 = "2024/input_23.txt".readFile()
private val test23 = "2024/input_23_test.txt".readFile()

private val resultTestOne = 7
private val resultTestTwo = "co,de,ka,ta"
private val resultOne = 1215
private val resultTwo = "bm,by,dv,ep,ia,ja,jb,ks,lv,ol,oy,uz,yt"

val test = defaultTestSuiteParseOnInit(
Day23(data23),
Day23(test23),
resultTestOne,
resultOne,
resultTestTwo,
resultTwo,
{ Day23(data23) },
{ Day23(test23) },
numTestPart1 = 4,
numTestPart2 = 100
)

@Nested
inner class Init {
@Test
fun `23,-,example,1`() {
report(AOCTest({ "123".toInt() }, Unit, 123, 5, "23".toInt(), Part.TWO, false, "example"))
}

@Test
fun `23,-,example,2`() {
report(test.initTest.copy())
}

@Test
fun `23,-,test,init`() {
report(test.initTest)
}

@Test
fun `23,-,live,init`() {
report(test.initLive)
}
}

@Nested
inner class `Part 1` {
@Test
fun `23,1,test`() {
report(test.testPart1)
}

@Test
fun `23,1,live,1`() {
report(test.livePart1)
}
}

@Nested
inner class `Part 2` {
@Test
fun `23,2,test`() {
report(test.testPart2)
}

@Test
fun `23,2,live,1`() {
report(test.livePart2)
}
}
}
32 changes: 32 additions & 0 deletions src/test/resources/2024/input_23_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
kh-tc
qp-kh
de-cg
ka-co
yn-aq
qp-ub
cg-tb
vc-aq
tb-ka
wh-tc
yn-cg
kh-ub
ta-co
de-co
tc-td
tb-wq
wh-td
ta-ka
td-qp
aq-cg
wq-ub
ub-vc
de-ta
wq-aq
wq-vc
wh-yn
ka-de
kh-ta
co-tc
wh-qp
tb-vc
td-yn

0 comments on commit 641c2c7

Please sign in to comment.