@@ -9,55 +9,55 @@ object Day11 {
9
9
10
10
private val inputs = File (" resources/adventofcode2023/Day11.txt" ).readLines()
11
11
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)
36
24
}
37
25
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()
39
28
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
+
43
38
return positions
44
39
}
45
40
46
41
private fun getDistance (positionA : Pair <Int , Int >, positionB : Pair <Int , Int >): Int =
47
42
abs(positionA.first - positionB.first) + abs(positionA.second - positionB.second)
48
43
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
52
47
53
48
for (i in galaxiesPositions.indices)
54
49
for (j in i + 1 .. < galaxiesPositions.size)
55
50
distancesSum + = getDistance(galaxiesPositions[i], galaxiesPositions[j])
56
51
57
- println ( distancesSum)
52
+ return distancesSum
58
53
}
54
+
55
+ fun part1 () = println (getDistancesSum(1 ))
56
+
57
+ fun part2 () = println (getDistancesSum(999999 ))
59
58
}
60
59
61
60
fun main () {
62
61
Day11 .part1()
62
+ Day11 .part2()
63
63
}
0 commit comments