|
1 | 1 | package cz.glubo.adventofcode.y2025.day8 |
2 | 2 |
|
| 3 | +import cz.glubo.adventofcode.utils.IVec3 |
3 | 4 | import cz.glubo.adventofcode.utils.input.Input |
4 | 5 | import io.klogging.noCoLogger |
| 6 | +import kotlin.math.roundToLong |
| 7 | +import kotlin.math.sqrt |
5 | 8 |
|
6 | 9 | val logger = noCoLogger({}.javaClass.toString()) |
7 | 10 |
|
8 | | -suspend fun y2025day8part1(input: Input): Long { |
| 11 | +class UnionFind( |
| 12 | + size: Int, |
| 13 | +) { |
| 14 | + val parents = MutableList(size) { it } |
| 15 | + |
| 16 | + fun findRoot(x: Int): Int { |
| 17 | + val parent = parents[x] |
| 18 | + return if (parents[parent] != parent) { |
| 19 | + findRoot(parent).also { parents[x] = it } |
| 20 | + } else { |
| 21 | + parent |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + fun markUnion( |
| 26 | + x: Int, |
| 27 | + y: Int, |
| 28 | + ) { |
| 29 | + val rootX = findRoot(x) |
| 30 | + val rootY = findRoot(y) |
| 31 | + parents[rootX] = rootY |
| 32 | + } |
| 33 | + |
| 34 | + fun sizes(): List<Int> = |
| 35 | + parents |
| 36 | + .groupBy { findRoot(it) } |
| 37 | + .map { (_, it) -> it.size } |
| 38 | +} |
| 39 | + |
| 40 | +suspend fun y2025day8part1( |
| 41 | + input: Input, |
| 42 | + num: Int = 1000, |
| 43 | +): Long { |
9 | 44 | logger.info("year 2025 day 8 part 1") |
10 | | - return 0 |
| 45 | + val points = mutableListOf<IVec3>() |
| 46 | + input.lineFlow().collect { line -> |
| 47 | + val (x, y, z) = |
| 48 | + line |
| 49 | + .split(',') |
| 50 | + .map { it.toInt() } |
| 51 | + points.add(IVec3(x, y, z)) |
| 52 | + } |
| 53 | + |
| 54 | + data class Connection( |
| 55 | + val i: Int, |
| 56 | + val j: Int, |
| 57 | + val squareLength: Long, |
| 58 | + ) { |
| 59 | + fun length() = sqrt(squareLength.toDouble()).roundToLong() |
| 60 | + } |
| 61 | + |
| 62 | + val allConnections = |
| 63 | + points.indices.flatMap { i -> |
| 64 | + (i + 1..<points.size).map { j -> |
| 65 | + Connection( |
| 66 | + i, |
| 67 | + j, |
| 68 | + points[i] squareDistance points[j], |
| 69 | + ) |
| 70 | + } |
| 71 | + } |
| 72 | + val chosenConnections = |
| 73 | + allConnections |
| 74 | + .sortedBy { it.squareLength } |
| 75 | + .take(num) |
| 76 | + val union = UnionFind(points.size) |
| 77 | + chosenConnections.forEach { conn -> |
| 78 | + union.markUnion(conn.i, conn.j) |
| 79 | + } |
| 80 | + |
| 81 | + return union |
| 82 | + .sizes() |
| 83 | + .sortedByDescending { it } |
| 84 | + .take(3) |
| 85 | + .fold(1L) { acc, i -> acc * i } |
11 | 86 | } |
12 | 87 |
|
13 | 88 | suspend fun y2025day8part2(input: Input): Long { |
14 | 89 | logger.info("year 2025 day 8 part 2") |
15 | | - return 0 |
| 90 | + val points = mutableListOf<IVec3>() |
| 91 | + input.lineFlow().collect { line -> |
| 92 | + val (x, y, z) = |
| 93 | + line |
| 94 | + .split(',') |
| 95 | + .map { it.toInt() } |
| 96 | + points.add(IVec3(x, y, z)) |
| 97 | + } |
| 98 | + |
| 99 | + data class Connection( |
| 100 | + val i: Int, |
| 101 | + val j: Int, |
| 102 | + val squareLength: Long, |
| 103 | + ) { |
| 104 | + fun length() = sqrt(squareLength.toDouble()).roundToLong() |
| 105 | + } |
| 106 | + |
| 107 | + val allConnections = |
| 108 | + points.indices |
| 109 | + .flatMap { i -> |
| 110 | + (i + 1..<points.size).map { j -> |
| 111 | + Connection( |
| 112 | + i, |
| 113 | + j, |
| 114 | + points[i] squareDistance points[j], |
| 115 | + ) |
| 116 | + } |
| 117 | + }.sortedBy { it.squareLength } |
| 118 | + val union = UnionFind(points.size) |
| 119 | + |
| 120 | + var lastConnection: Connection |
| 121 | + var i = 0 |
| 122 | + do { |
| 123 | + val conn = allConnections[i] |
| 124 | + union.markUnion(conn.i, conn.j) |
| 125 | + lastConnection = conn |
| 126 | + i++ |
| 127 | + } while (union.sizes().size > 1 && allConnections.indices.contains((i))) |
| 128 | + |
| 129 | + return points[lastConnection.i].x.toLong() * points[lastConnection.j].x.toLong() |
16 | 130 | } |
0 commit comments