Skip to content

Commit

Permalink
Day 23
Browse files Browse the repository at this point in the history
  • Loading branch information
hibob224 committed Dec 23, 2024
1 parent 624bac9 commit 942b6d8
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/main/kotlin/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,14 @@ fun List<Long>.gcd(): Long = reduce { x, y -> gcd(x, y) }
fun lcm(x: Long, y: Long): Long = x * (y / gcd(x, y))
fun List<Long>.lcm(): Long = reduce { x, y -> lcm(x, y) }

//region Input
fun Any.getInputFile(example: Boolean = false): File = getInputFile(this::class.java.packageName, example)
private fun getInputFile(packageName: String, example: Boolean = false): File = File("src/main/kotlin/${packageName.replace('.', '/')}/${if (example) "example" else "input"}.txt")

fun File.longs(seperator: String = "\n") = strings(seperator).map(String::toLong)
fun File.strings(seperator: String = "\n") = readText().split(seperator)
//endregion

fun <T> List<T>.permutations(): List<List<T>> {
return if (this.size == 1) listOf(this)
else this.flatMap { i -> (this - i).permutations().map { listOf(i) + it } }
Expand Down Expand Up @@ -154,3 +159,10 @@ fun findSubsequenceIndex(mainList: List<Long>, subList: List<Long>): Int {
}
return -1
}

fun Any?.print() = also(::println)

fun <T> List<T>.toPair(): Pair<T, T> {
check(size == 2)
return get(0) to get(1)
}
1 change: 0 additions & 1 deletion src/main/kotlin/y2024/day17/Day17.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,4 @@ object Day17 {
private const val OUT = 5 // combo % 8, output
private const val BDV = 6 // A / combo^2 > B
private const val CDV = 7 // A / combo^2 > C

}
68 changes: 68 additions & 0 deletions src/main/kotlin/y2024/day23/Day23.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package y2024.day23

import utils.Memo2
import utils.getInputFile
import utils.memoize

fun main() {
val day = Day23()
println("Part one: " + day.solvePartOne())
println("Part two: " + day.solvePartTwo())
}

class Day23 {

private val adjacent = mutableMapOf<String, MutableList<String>>().withDefault { mutableListOf() }

init {
getInputFile(example = false)
.forEachLine {
val (a, b) = it.split("-")
adjacent.getOrPut(a, ::mutableListOf).add(b)
adjacent.getOrPut(b, ::mutableListOf).add(a)
}
}

fun solvePartOne(): Int {
return adjacent.keys.sumOf { a ->
adjacent.getValue(a).sumOf b@{ b ->
if (a >= b) return@b 0
adjacent.getValue(a).intersect(adjacent.getValue(b).toSet()).count { c ->
if (c >= b) return@count false
a.startsWith("t") || b.startsWith("t") || c.startsWith("t")
}
}
} / 2
}

fun solvePartTwo(): String {
var bestSolution = setOf<String>()
val entries = adjacent.entries.toList()

fun check(solution: Set<String>, index: Int) {
val (id, adj) = entries.getOrNull(index) ?: return
if (adj.containsAll(solution)) {
if (solution.size.inc() > bestSolution.size) {
bestSolution = solution + id
}
check(solution + id, index + 1)
}
check(solution, index + 1)
}

check(emptySet(), 0)
return bestSolution.sorted().joinToString(",")
}

private val fillParty = Memo2<Map<String, List<String>>, Set<String>, List<Set<String>>>::fillParty.memoize()

}

private fun Memo2<Map<String, List<String>>, Set<String>, List<Set<String>>>.fillParty(adjacent: Map<String, List<String>>, party: Set<String>): List<Set<String>> {
val intersect = party.fold(adjacent.getValue(party.first()).toSet()) { acc, s -> acc.intersect(adjacent.getValue(s)) }
return if (intersect.isEmpty()) {
listOf(party)
} else {
intersect.flatMap { recurse(adjacent, party + it) }
}
}
Binary file added src/main/kotlin/y2024/day23/input.txt
Binary file not shown.
19 changes: 19 additions & 0 deletions src/test/kotlin/y2024/day23/Day23Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package y2024.day23

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

internal class Day23Test {

private val day = Day23()

@Test
fun solvePartOne() {
assertEquals(1227, day.solvePartOne())
}

@Test
fun solvePartTwo() {
assertEquals("cl,df,ft,ir,iy,ny,qp,rb,sh,sl,sw,wm,wy", day.solvePartTwo())
}
}

0 comments on commit 942b6d8

Please sign in to comment.