-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
99 additions
and
1 deletion.
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
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
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 |
---|---|---|
@@ -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 not shown.
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 |
---|---|---|
@@ -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()) | ||
} | ||
} |