Skip to content

Commit ae7be05

Browse files
committed
Add 2023 Day 14 Part 2 solution
1 parent 6733c1e commit ae7be05

File tree

1 file changed

+59
-17
lines changed

1 file changed

+59
-17
lines changed

adventofcode2023/Day14.kt

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,76 @@ object Day14 {
88

99
private val inputs = File("resources/adventofcode2023/Day14.txt").readLines()
1010

11-
private fun rollAllNorth(): List<String> {
12-
val rolledInputs = inputs.map { it.toMutableList() }
13-
14-
for (y in rolledInputs.indices) {
15-
for (x in rolledInputs[y].indices) {
16-
if (rolledInputs[y][x] == ROUNDED_ROCK) {
17-
var currentY = y
18-
19-
while (currentY > 0 && rolledInputs[currentY - 1][x] == EMPTY) {
20-
rolledInputs[currentY][x] = EMPTY
21-
currentY--
22-
rolledInputs[currentY][x] = ROUNDED_ROCK
11+
private fun rollAll(rocks: List<String>, direction: Pair<Int, Int>): List<String> {
12+
val rolled = rocks.map { it.toMutableList() }
13+
14+
val yIndices = if (direction.second < 0) rolled.indices else rolled.indices.reversed()
15+
val xIndices = if (direction.first < 0) rolled[0].indices else rolled[0].indices.reversed()
16+
17+
for (y in yIndices) {
18+
for (x in xIndices) {
19+
if (rolled[y][x] == ROUNDED_ROCK) {
20+
var nextX = x + direction.first
21+
var nextY = y + direction.second
22+
23+
while (
24+
nextY >= 0 && nextX >= 0 && nextY < rolled.size && nextX < rolled[nextY].size &&
25+
rolled[nextY][nextX] == EMPTY
26+
) {
27+
rolled[nextY - direction.second][nextX - direction.first] = EMPTY
28+
rolled[nextY][nextX] = ROUNDED_ROCK
29+
30+
nextX += direction.first
31+
nextY += direction.second
2332
}
2433
}
2534
}
2635
}
2736

28-
return rolledInputs.map { it.joinToString(separator = "") }
37+
return rolled.map { it.joinToString(separator = "") }
38+
}
39+
40+
private fun spinCycle(rocks: List<String>): List<String> =
41+
rollAll(rollAll(rollAll(rollAll(rocks, Pair(0, -1)), Pair(-1, 0)), Pair(0, 1)), Pair(1, 0))
42+
43+
private fun runSpinCycles(rocks: List<String>, cycles: Int): List<String> {
44+
var current = rocks
45+
val history = mutableListOf<List<String>>()
46+
var cycle = 0
47+
48+
while (cycle < cycles) {
49+
current = spinCycle(current)
50+
51+
val lastAppearedIndex = history.indexOf(current)
52+
if (lastAppearedIndex > -1) {
53+
val toAdd = cycle - lastAppearedIndex
54+
cycle += ((cycles - cycle) / toAdd) * toAdd
55+
}
56+
57+
history.add(current)
58+
cycle++
59+
}
60+
61+
return current
2962
}
3063

31-
fun part1() =
32-
println(rollAllNorth().withIndex().sumOf { line ->
64+
private fun calculateNorthLoad(rocks: List<String>): Int =
65+
rocks.withIndex().sumOf { line ->
3366
line.value.sumOf {
34-
if (it == ROUNDED_ROCK) inputs.size - line.index else 0
67+
if (it == ROUNDED_ROCK) rocks.size - line.index else 0
3568
}
36-
})
69+
}
70+
71+
fun part1() = println(
72+
calculateNorthLoad(rollAll(inputs, Pair(0, -1)))
73+
)
74+
75+
fun part2() = println(
76+
calculateNorthLoad(runSpinCycles(inputs, 1000000000))
77+
)
3778
}
3879

3980
fun main() {
4081
Day14.part1()
82+
Day14.part2()
4183
}

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