Skip to content

Commit 7586bdf

Browse files
committed
Solved day14 part 1
1 parent 7ee0d16 commit 7586bdf

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2023'
7-
version '0.13.1'
7+
version '0.13.2'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {

day14/src/main/kotlin/de/havox_design/aoc2023/day14/Day14.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package de.havox_design.aoc2023.day14
22

33
class Day14(private var filename: String) {
44
fun solvePart1(): Long =
5-
136L
5+
Matrix(getResourceAsText(filename))
6+
.rollNorth()
7+
.northLoad()
8+
.toLong()
69

710
fun solvePart2(): Long =
811
0L
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package de.havox_design.aoc2023.day14
2+
3+
data class Matrix(val matrix: MutableList<CharArray>) {
4+
private val ICON_ROUND_STONE = 'O'
5+
private val ICON_SQUARE_STONE = '#'
6+
7+
fun rollNorth(): Matrix =
8+
apply {
9+
matrix
10+
.first()
11+
.indices
12+
.forEach { columnIndex ->
13+
14+
matrix
15+
.indices
16+
.joinToString("") { rowIndex ->
17+
matrix[rowIndex][columnIndex].toString()
18+
}
19+
.split(ICON_SQUARE_STONE)
20+
.joinToString(ICON_SQUARE_STONE.toString()) {
21+
it
22+
.toCharArray()
23+
.sortedDescending()
24+
.joinToString("")
25+
}
26+
27+
.forEachIndexed { rowIndex, c ->
28+
matrix[rowIndex][columnIndex] = c
29+
}
30+
}
31+
}
32+
33+
fun northLoad(): Int =
34+
matrix
35+
.first()
36+
.indices
37+
.sumOf { columnIndex ->
38+
matrix.indices
39+
.sumOf { rowIndex ->
40+
when (ICON_ROUND_STONE) {
41+
matrix[rowIndex][columnIndex] -> matrix.size - rowIndex
42+
else -> 0
43+
}
44+
}
45+
}
46+
47+
override fun toString(): String =
48+
matrix
49+
.joinToString("\n") { line -> line.joinToString("") }
50+
}
51+
52+
fun Matrix(input: List<String>): Matrix =
53+
input
54+
.map(String::toCharArray)
55+
.toMutableList()
56+
.let(::Matrix)

0 commit comments

Comments
 (0)