Skip to content

Commit ee7b474

Browse files
committed
Add 2023 Day 11 Part 2 solution
1 parent 10d7ec5 commit ee7b474

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

adventofcode2023/Day11.kt

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,55 @@ object Day11 {
99

1010
private val inputs = File("resources/adventofcode2023/Day11.txt").readLines()
1111

12-
private fun getExpandedUniverse(universe: List<String>): List<String> {
13-
val expandedUniverse = universe.map { it.toMutableList() }.toMutableList()
14-
15-
// Add horizontal lines
16-
var i = 0
17-
while (i < expandedUniverse.size) {
18-
if (expandedUniverse[i].count {it == EMPTY_CHAR} == expandedUniverse[i].size) {
19-
expandedUniverse.add(i, MutableList(expandedUniverse[i].size) { EMPTY_CHAR })
20-
i++
21-
}
22-
i++
23-
}
24-
25-
// Add vertical lines
26-
i = 0
27-
while (i < expandedUniverse[0].size) {
28-
if (expandedUniverse.count { it[i] == EMPTY_CHAR } == expandedUniverse.count()) {
29-
expandedUniverse.forEach { it.add(i, EMPTY_CHAR) }
30-
i++
31-
}
32-
i++
33-
}
34-
35-
return expandedUniverse.map { it.joinToString("") }
12+
private fun getEmptyRowsAndColumns(): Pair<List<Int>, List<Int>> {
13+
val rows = mutableListOf<Int>()
14+
val columns = mutableListOf<Int>()
15+
16+
for (i in inputs.indices)
17+
if (inputs[i].count {it == EMPTY_CHAR} == inputs[i].length)
18+
rows.add(i)
19+
for (i in inputs[0].indices)
20+
if (inputs.count { it[i] == EMPTY_CHAR } == inputs.size)
21+
columns.add(i)
22+
23+
return Pair(rows, columns)
3624
}
3725

38-
private fun getGalaxiesPositions(universe: List<String>): List<Pair<Int, Int>> {
26+
private fun getGalaxiesPositions(additionalEmpty: Int = 0): List<Pair<Int, Int>> {
27+
val (emptyRows, emptyColumns) = getEmptyRowsAndColumns()
3928
val positions: MutableList<Pair<Int, Int>> = mutableListOf()
40-
for (y in universe.indices)
41-
for (x in universe[y].indices)
42-
if (universe[y][x] == GALAXY_CHAR) positions.add(Pair(x, y))
29+
30+
for (y in inputs.indices)
31+
for (x in inputs[y].indices)
32+
if (inputs[y][x] == GALAXY_CHAR)
33+
positions.add(Pair(
34+
x + emptyColumns.count {it < x} * additionalEmpty,
35+
y + emptyRows.count {it < y} * additionalEmpty
36+
))
37+
4338
return positions
4439
}
4540

4641
private fun getDistance(positionA: Pair<Int, Int>, positionB: Pair<Int, Int>): Int =
4742
abs(positionA.first - positionB.first) + abs(positionA.second - positionB.second)
4843

49-
fun part1() {
50-
val galaxiesPositions = getGalaxiesPositions(getExpandedUniverse(inputs))
51-
var distancesSum = 0
44+
private fun getDistancesSum(additionalEmpty: Int): Long {
45+
val galaxiesPositions = getGalaxiesPositions(additionalEmpty)
46+
var distancesSum = 0L
5247

5348
for (i in galaxiesPositions.indices)
5449
for (j in i + 1..<galaxiesPositions.size)
5550
distancesSum += getDistance(galaxiesPositions[i], galaxiesPositions[j])
5651

57-
println(distancesSum)
52+
return distancesSum
5853
}
54+
55+
fun part1() = println(getDistancesSum(1))
56+
57+
fun part2() = println(getDistancesSum(999999))
5958
}
6059

6160
fun main() {
6261
Day11.part1()
62+
Day11.part2()
6363
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy