-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day16.kt
208 lines (181 loc) · 7.45 KB
/
Day16.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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 Day16 : Day(16, 2023, "The Floor Will Be Lava") {
class Tile(var tile: Char) {
var energized: Boolean = false
val rayDirections: MutableList<Direction4> = mutableListOf()
}
class Grid(input: List<String>) {
val tiles = input.map { line ->
line.map {
Tile(it)
}
}
companion object {
const val MIRROR_TL_BR = '\\'
const val MIRROR_TR_BL = '/'
const val SPLITTER_HORIZONTAL = '-'
const val SPLITTER_VERTICAL = '|'
const val EMPTY = '.'
}
fun raytrace(position: Coords, direction: Direction4) {
if (position.second !in this.tiles.indices
|| position.first !in this.tiles[0].indices
) {
return
}
val tile = this.tiles[position.second][position.first]
tile.energized = true
if (tile.rayDirections.contains(direction)) {
return
}
else {
tile.rayDirections.add(direction)
}
val nextDirections = buildList {
when (tile.tile) {
EMPTY -> add(direction)
SPLITTER_VERTICAL -> addAll(listOf(Direction4.SOUTH, Direction4.NORTH))
SPLITTER_HORIZONTAL -> addAll(listOf(Direction4.EAST, Direction4.WEST))
MIRROR_TR_BL -> {
when (direction) {
Direction4.EAST -> add(Direction4.NORTH)
Direction4.SOUTH -> add(Direction4.WEST)
Direction4.WEST -> add(Direction4.SOUTH)
Direction4.NORTH -> add(Direction4.EAST)
}
}
MIRROR_TL_BR -> {
when (direction) {
Direction4.EAST -> add(Direction4.SOUTH)
Direction4.SOUTH -> add(Direction4.EAST)
Direction4.WEST -> add(Direction4.NORTH)
Direction4.NORTH -> add(Direction4.WEST)
}
}
}
}
for (d in nextDirections) {
raytrace(position.plus(d), d)
}
}
fun raytraceIterative(initialPosition: Coords, initialDirection: Direction4) {
val stack = mutableListOf(Pair(initialPosition, initialDirection))
while (stack.isNotEmpty()) {
val (position, direction) = stack.removeAt(stack.size - 1)
if (position.second !in this.tiles.indices || position.first !in this.tiles[0].indices) {
continue
}
val tile = this.tiles[position.second][position.first]
tile.energized = true
if (tile.rayDirections.contains(direction)) {
continue
}
else {
tile.rayDirections.add(direction)
}
val nextDirections = buildList {
when (tile.tile) {
EMPTY -> add(direction)
SPLITTER_VERTICAL -> addAll(listOf(Direction4.SOUTH, Direction4.NORTH))
SPLITTER_HORIZONTAL -> addAll(listOf(Direction4.EAST, Direction4.WEST))
MIRROR_TR_BL -> {
when (direction) {
Direction4.EAST -> add(Direction4.NORTH)
Direction4.SOUTH -> add(Direction4.WEST)
Direction4.WEST -> add(Direction4.SOUTH)
Direction4.NORTH -> add(Direction4.EAST)
}
}
MIRROR_TL_BR -> {
when (direction) {
Direction4.EAST -> add(Direction4.SOUTH)
Direction4.SOUTH -> add(Direction4.EAST)
Direction4.WEST -> add(Direction4.NORTH)
Direction4.NORTH -> add(Direction4.WEST)
}
}
}
}
for (d in nextDirections) {
stack.add(position.plus(d) to d)
}
}
}
fun show() {
for (line in this.tiles) {
for (t in line) {
when (t.tile) {
EMPTY -> {
if (t.energized) {
if (t.rayDirections.size == 1) {
when (t.rayDirections.first()) {
Direction4.EAST -> print('>')
Direction4.SOUTH -> print('v')
Direction4.WEST -> print('<')
Direction4.NORTH -> print('^')
}
}
else {
print(t.rayDirections.size)
}
}
else {
print(EMPTY)
}
}
else -> print(t.tile)
}
}
println()
}
}
}
override fun part1(): Int {
val grid = Grid(inputAsList)
grid.raytraceIterative(Coords(0, 0), Direction4.EAST)
grid.show()
return grid.tiles.flatten().count { it.energized }
}
override fun part2(): Int {
val width = inputAsList[0].length
val height = inputAsList.size
var maxEnergie = Int.MIN_VALUE
for (x in 0 until width) {
val grid = Grid(inputAsList)
grid.raytraceIterative(Coords(x, 0), Direction4.SOUTH)
val energie = grid.tiles.flatten().count { it.energized }
if (energie > maxEnergie) {
maxEnergie = energie
}
}
for (x in 0 until width) {
val grid = Grid(inputAsList)
grid.raytraceIterative(Coords(x, height - 1), Direction4.NORTH)
val energie = grid.tiles.flatten().count { it.energized }
if (energie > maxEnergie) {
maxEnergie = energie
}
}
for (y in 0 until height) {
val grid = Grid(inputAsList)
grid.raytraceIterative(Coords(0, y), Direction4.EAST)
val energie = grid.tiles.flatten().count { it.energized }
if (energie > maxEnergie) {
maxEnergie = energie
}
}
for (y in 0 until height) {
val grid = Grid(inputAsList)
grid.raytraceIterative(Coords(width-1, y), Direction4.WEST)
val energie = grid.tiles.flatten().count { it.energized }
if (energie > maxEnergie) {
maxEnergie = energie
}
}
return maxEnergie
}
}