Coverage Summary for Class: Platform (day14p1)
Class |
Method, %
|
Line, %
|
Platform |
100%
(4/4)
|
100%
(15/15)
|
Platform$calcTotalLoad$1 |
100%
(1/1)
|
100%
(2/2)
|
Platform$look$1 |
100%
(1/1)
|
100%
(1/1)
|
Total |
100%
(6/6)
|
100%
(18/18)
|
package day14p1
fun String.toPlatform(): Platform = Platform(lineSequence().map { it.toMutableList() }.toList())
data class Platform(val value: List<MutableList<Char>>) {
fun tiltNorth(): Platform {
val copy = copy()
val numOfColumns = copy.value.first().size
for (col in (0..<numOfColumns)) {
for (row in copy.value.indices) {
var currentRow = row
while (currentRow > 0 && copy.value[currentRow - 1][col] == '.' && copy.value[currentRow][col] == 'O') {
copy.value[currentRow - 1][col] = 'O'
copy.value[currentRow][col] = '.'
currentRow--
}
}
}
return copy
}
fun look(): String = value.joinToString("\n") { it.joinToString("") }
fun calcTotalLoad(): Long {
val numOfRows = value.size.toLong()
return value.asSequence().mapIndexed { rowIndex, row ->
val load = numOfRows - rowIndex
row.sumOf { if (it == 'O') load else 0 }
}.sum()
}
}