-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day14.kt
149 lines (127 loc) · 4.37 KB
/
Day14.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package tr.emreone.adventofcode.days
import tr.emreone.kotlin_utils.automation.Day
import tr.emreone.kotlin_utils.math.Coords
import tr.emreone.kotlin_utils.math.Direction4
import tr.emreone.kotlin_utils.math.plus
class Day14 : Day(14, 2023, "Parabolic Reflector Dish") {
class Platform(input: List<String>) {
val grid = input.map {
it.toMutableList()
}.toMutableList()
private val height = this.grid.size
private val width = this.grid.get(0).size
companion object {
const val ROUNDED_ROCK = 'O'
const val CUBED_ROCK = '#'
const val EMPTY = '.'
}
private fun _move(x: Int, y: Int, direction: Direction4) {
if (this.grid[y][x] == EMPTY || this.grid[y][x] == CUBED_ROCK) {
return
}
var position = Coords(x, y)
var next: Coords
while (true) {
next = position.plus(direction)
if (next.first !in this.grid.get(0).indices
|| next.second !in this.grid.indices
) {
break
}
if (this.grid[next.second][next.first] != EMPTY) {
break
}
this.grid[next.second][next.first] = ROUNDED_ROCK
this.grid[position.second][position.first] = EMPTY
position = next
}
}
fun tiltPlatform(direction: Direction4) {
when (direction) {
Direction4.NORTH -> {
for (xi in 0 until width) {
for (yi in 0 until height) {
this._move(xi, yi, direction)
}
}
}
Direction4.EAST -> {
for (yi in 0 until height) {
for (xi in (width - 1) downTo 0) {
this._move(xi, yi, direction)
}
}
}
Direction4.SOUTH -> {
for (xi in 0 until width) {
for (yi in (height - 1) downTo 0) {
this._move(xi, yi, direction)
}
}
}
Direction4.WEST -> {
for (yi in 0 until height) {
for (xi in 0 until width) {
this._move(xi, yi, direction)
}
}
}
}
}
fun cycle() {
this.tiltPlatform(Direction4.NORTH)
this.tiltPlatform(Direction4.WEST)
this.tiltPlatform(Direction4.SOUTH)
this.tiltPlatform(Direction4.EAST)
}
fun totalLoad(): Long {
return this.grid.mapIndexed { index, line ->
line.map {
if (it == ROUNDED_ROCK) {
(height - index).toLong()
}
else {
0L
}
}.sum()
}.sum()
}
fun getState(): String {
return this.grid.joinToString("\n") { line ->
line.joinToString("")
}
}
fun print() {
println(this.getState())
}
}
override fun part1(): Long {
val platform = Platform(inputAsList)
platform.tiltPlatform(Direction4.NORTH)
return platform.totalLoad()
}
override fun part2(): Long {
val platform = Platform(inputAsList)
var prevCounter = 0L
var counter = 0L
val stateMap = mutableMapOf(platform.getState() to 0L)
// find a loop instead of iterating 1_000_000_000
while (true) {
platform.cycle()
counter++
if (stateMap.containsKey(platform.getState())) {
prevCounter = stateMap.get(platform.getState())!!
break
}
if (counter == 1_000_000_000L) {
break
}
stateMap[platform.getState()] = counter
}
val remainingCycles = (1_000_000_000L - counter).mod(counter - prevCounter)
repeat(remainingCycles.toInt()) {
platform.cycle()
}
return platform.totalLoad()
}
}