-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day14.kt
177 lines (150 loc) · 5.71 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
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
package tr.emreone.adventofcode.days
import tr.emreone.kotlin_utils.Logger.logger
import tr.emreone.kotlin_utils.math.Point2D
import java.lang.StringBuilder
object Day14 {
enum class MapElem(val symbol: Char) {
AIR(' '),
START_SAND('+'),
ROCK('#'),
SAND('·'),
DROPPING_SAND('~')
}
class Terrain {
private val sandDropStartCoord = Point2D(500, 0)
private var xMin = 500L
private var xMax = 500L
private var yMin = 0L
private var yMax = 0L
var numberOfGrainOfSand = 0
private var currentGrainOfSand: Point2D? = null
private val terrainMap: MutableMap<Point2D, MapElem> = mutableMapOf()
var infiniteDepth = true
companion object {
fun parse(input: List<String>): Terrain {
return Terrain().apply {
val rocks = input.map { rockDef ->
val rockDefParts = mutableSetOf<Point2D>()
rockDef.split(" -> ")
.map { coords ->
coords.split(",").map { it.toLong() }
}
.windowed(2)
.map {
val (from, to) = it
val fromPoint = Point2D(from[0], from[1])
val toPoint = Point2D(to[0], to[1])
rockDefParts.addAll(fromPoint.lineTo(toPoint))
}
rockDefParts.toSet()
}
initMap(rocks)
}
}
}
private fun setElemAtPoint(point: Point2D, elem: MapElem) {
this.terrainMap[point] = elem
// adjust boundaries
this.xMin = minOf(this.xMin, point.x)
this.xMax = maxOf(this.xMax, point.x)
this.yMin = minOf(this.yMin, point.y)
this.yMax = maxOf(this.yMax, point.y)
}
private fun getElemAtPoint(point: Point2D): MapElem {
return this.terrainMap[point] ?: MapElem.AIR
}
private fun initMap(rocks: List<Set<Point2D>>) {
rocks.forEach { rock ->
rock.forEach { point ->
setElemAtPoint(point, MapElem.ROCK)
}
}
setElemAtPoint(sandDropStartCoord, MapElem.START_SAND)
}
fun prepareForPart2() {
this.infiniteDepth = false
this.yMax += 2
}
fun tick(): Boolean {
if (currentGrainOfSand == null) {
currentGrainOfSand = sandDropStartCoord
if (!this.infiniteDepth && getElemAtPoint(sandDropStartCoord) == MapElem.SAND) {
return false
}
numberOfGrainOfSand++
}
val southWest = Point2D(currentGrainOfSand!!.x - 1, currentGrainOfSand!!.y + 1)
val south = Point2D(currentGrainOfSand!!.x, currentGrainOfSand!!.y + 1)
val southEast = Point2D(currentGrainOfSand!!.x + 1, currentGrainOfSand!!.y + 1)
if (this.infiniteDepth && south.y > yMax) {
numberOfGrainOfSand--
return false
}
else if (!this.infiniteDepth) {
if (south.y == yMax) {
setElemAtPoint(currentGrainOfSand!!, MapElem.SAND)
currentGrainOfSand = null
return true
}
}
if (getElemAtPoint(south) == MapElem.AIR) {
setElemAtPoint(currentGrainOfSand!!, MapElem.AIR)
setElemAtPoint(south, MapElem.DROPPING_SAND)
currentGrainOfSand = south
} else {
currentGrainOfSand = when (MapElem.AIR) {
getElemAtPoint(southWest) -> {
setElemAtPoint(currentGrainOfSand!!, MapElem.AIR)
setElemAtPoint(southWest, MapElem.DROPPING_SAND)
southWest
}
getElemAtPoint(southEast) -> {
setElemAtPoint(currentGrainOfSand!!, MapElem.AIR)
setElemAtPoint(southEast, MapElem.DROPPING_SAND)
southEast
}
else -> {
setElemAtPoint(currentGrainOfSand!!, MapElem.SAND)
null
}
}
}
return true
}
fun print() {
val sb = StringBuilder()
sb.appendLine()
for (y in (yMin - 1)..(yMax + 1)) {
if (!this.infiniteDepth && y == yMax) {
for (x in (xMin - 1)..(xMax + 1)) {
sb.append(MapElem.ROCK.symbol)
}
}
else {
for (x in (xMin - 1)..(xMax + 1)) {
sb.append(getElemAtPoint(Point2D(x, y)).symbol)
}
}
sb.appendLine()
}
logger.info { sb.toString() }
}
}
fun part1(input: List<String>): Int {
val terrain = Terrain.parse(input)
while (terrain.tick()) {
// let the sand drop down
// terrain.print()
}
return terrain.numberOfGrainOfSand
}
fun part2(input: List<String>): Int {
val terrain = Terrain.parse(input)
terrain.prepareForPart2()
while (terrain.tick()) {
// let the sand drop down
// terrain.print()
}
return terrain.numberOfGrainOfSand
}
}