-
Notifications
You must be signed in to change notification settings - Fork 1
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
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/main/kotlin/me/peckb/aoc/_2024/calendar/day23/Day23.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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package me.peckb.aoc._2024.calendar.day23 | ||
|
||
import javax.inject.Inject | ||
import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory | ||
|
||
class Day23 @Inject constructor( | ||
private val generatorFactory: InputGeneratorFactory, | ||
) { | ||
fun partOne(filename: String) = generatorFactory.forFile(filename).readAs(::connection) { input -> | ||
val connections = findFriends(input) | ||
val triplets = mutableSetOf<Set<String>>() | ||
|
||
findCommonFriend(connections) { me, them, ourFriends -> | ||
ourFriends.forEach { ourFriend -> triplets.add(setOf(me, them, ourFriend)) } | ||
} | ||
|
||
triplets.count { group -> group.any { it.startsWith('t') } } | ||
} | ||
|
||
fun partTwo(filename: String) = generatorFactory.forFile(filename).readAs(::connection) { input -> | ||
val connections = findFriends(input) | ||
val friends = mutableSetOf<Set<String>>() | ||
|
||
findCommonFriend(connections) { me, them, ourFriends -> | ||
if (ourFriends.isNotEmpty()) { | ||
val everyFriend = ourFriends.all { maybeMutual -> | ||
ourFriends.minus(maybeMutual).all { | ||
(maybeMutual in connections[it]!!) | ||
} | ||
} | ||
if (everyFriend) { | ||
friends.add(ourFriends.plus(me).plus(them)) | ||
} | ||
} | ||
} | ||
|
||
friends.maxBy { it.size }.toList().sorted().joinToString(",") | ||
} | ||
|
||
private fun connection(line: String) : Pair<String, String> { | ||
return line.split("-").let { (a, b) -> a to b } | ||
} | ||
|
||
private fun findFriends(input: Sequence<Pair<String, String>>): MutableMap<String, Set<String>> { | ||
val connections = mutableMapOf<String, Set<String>>() | ||
|
||
input.forEach { (c1, c2) -> | ||
connections.merge(c1, setOf(c2)) { a, b -> a + b } | ||
connections.merge(c2, setOf(c1)) { a, b -> a + b } | ||
} | ||
|
||
return connections | ||
} | ||
|
||
private fun findCommonFriend(connections: Map<String, Set<String>>, handleFriends: (String, String, Set<String>) -> Unit) { | ||
val keys = connections.keys.toList() | ||
(0 until keys.size - 2).forEach { i -> | ||
val me = keys[i] | ||
val myFriends = connections[me]!! | ||
|
||
(i + 1 until keys.size - 1).forEach { j -> | ||
val them = keys[j] | ||
val theirFriends = connections[them]!! | ||
|
||
if (me in theirFriends) { | ||
handleFriends(me, them, myFriends.intersect(theirFriends)) | ||
} | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
## [Day 23: LAN Party](https://adventofcode.com/2024/day/23) |
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
32 changes: 32 additions & 0 deletions
32
src/test/kotlin/me/peckb/aoc/_2024/calendar/day23/Day23Test.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package me.peckb.aoc._2024.calendar.day23 | ||
|
||
import javax.inject.Inject | ||
|
||
import me.peckb.aoc._2024.DaggerTestDayComponent | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class Day23Test { | ||
@Inject | ||
lateinit var day23: Day23 | ||
|
||
@BeforeEach | ||
fun setup() { | ||
DaggerTestDayComponent.create().inject(this) | ||
} | ||
|
||
@Test | ||
fun testDay23PartOne() { | ||
assertEquals(998, day23.partOne(DAY_23)) | ||
} | ||
|
||
@Test | ||
fun testDay23PartTwo() { | ||
assertEquals("cc,ff,fh,fr,ny,oa,pl,rg,uj,wd,xn,xs,zw", day23.partTwo(DAY_23)) | ||
} | ||
|
||
companion object { | ||
private const val DAY_23: String = "advent-of-code-input/2024/day23.input" | ||
} | ||
} |