Skip to content

Commit 2addee2

Browse files
committed
Solved day14 part 2
1 parent b0c86a8 commit 2addee2

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
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.3'
7+
version '0.13.4'
88

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

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,39 @@ class Day14(private var filename: String) {
77
.northLoad()
88
.toLong()
99

10-
fun solvePart2(): Long =
11-
64L
10+
fun solvePart2(numberOfCycles: Int = 1000000000): Long {
11+
val matrix = Matrix(getResourceAsText(filename))
12+
val cycleMap: MutableMap<Int, String> = mutableMapOf(0 to matrix.toString())
13+
var cycleIndex = 0
14+
var cycleLength = 0
15+
16+
for (index in 1..numberOfCycles) {
17+
matrix.cycle()
18+
when {
19+
cycleMap.containsValue(matrix.toString()) -> {
20+
cycleIndex = cycleMap
21+
.filterValues { it == matrix.toString() }
22+
.keys
23+
.single()
24+
cycleLength = index - cycleIndex
25+
break
26+
}
27+
28+
else -> {
29+
cycleMap[index] = matrix.toString()
30+
}
31+
}
32+
}
33+
34+
val pointInCycle = (numberOfCycles - cycleIndex) % cycleLength
35+
val actualIndex = cycleIndex + pointInCycle
36+
37+
return cycleMap[actualIndex]!!
38+
.split("\n")
39+
.let(::Matrix)
40+
.northLoad()
41+
.toLong()
42+
}
1243

1344
private fun getResourceAsText(path: String): List<String> =
1445
this.javaClass.classLoader.getResourceAsStream(path)!!.bufferedReader().readLines()

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ data class Matrix(val matrix: MutableList<CharArray>) {
44
private val ICON_ROUND_STONE = 'O'
55
private val ICON_SQUARE_STONE = '#'
66

7+
fun cycle(cycles: Int = 1): Matrix = apply {
8+
repeat(cycles) {
9+
rollNorth()
10+
.rollWest()
11+
.rollSouth()
12+
.rollEast()
13+
}
14+
}
15+
716
fun rollNorth(): Matrix =
817
apply {
918
matrix
@@ -30,6 +39,66 @@ data class Matrix(val matrix: MutableList<CharArray>) {
3039
}
3140
}
3241

42+
fun rollSouth(): Matrix =
43+
apply {
44+
matrix
45+
.first()
46+
.indices
47+
.forEach { columnIndex ->
48+
49+
matrix
50+
.indices
51+
.joinToString("") { rowIndex ->
52+
matrix[rowIndex][columnIndex].toString()
53+
}
54+
.split(ICON_SQUARE_STONE)
55+
.joinToString(ICON_SQUARE_STONE.toString()) {
56+
it
57+
.toCharArray()
58+
.sorted()
59+
.joinToString("")
60+
}
61+
62+
.forEachIndexed { rowIndex, c ->
63+
matrix[rowIndex][columnIndex] = c
64+
}
65+
}
66+
}
67+
68+
fun rollWest(): Matrix =
69+
apply {
70+
matrix
71+
.forEachIndexed { rowIndex, row ->
72+
matrix[rowIndex] = row
73+
.joinToString("")
74+
.split(ICON_SQUARE_STONE)
75+
.joinToString(ICON_SQUARE_STONE.toString()) {
76+
it
77+
.toCharArray()
78+
.sortedDescending()
79+
.joinToString("")
80+
}
81+
.toCharArray()
82+
}
83+
}
84+
85+
fun rollEast(): Matrix =
86+
apply {
87+
matrix
88+
.forEachIndexed { rowIndex, row ->
89+
matrix[rowIndex] = row
90+
.joinToString("")
91+
.split(ICON_SQUARE_STONE)
92+
.joinToString(ICON_SQUARE_STONE.toString()) {
93+
it
94+
.toCharArray()
95+
.sorted()
96+
.joinToString("")
97+
}
98+
.toCharArray()
99+
}
100+
}
101+
33102
fun northLoad(): Int =
34103
matrix
35104
.first()

0 commit comments

Comments
 (0)