-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day15.kt
164 lines (137 loc) · 5.73 KB
/
Day15.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
package tr.emreone.adventofcode.days
import kotlinx.coroutines.*
import tr.emreone.adventofcode.manhattanDistanceTo
import tr.emreone.kotlin_utils.Logger.logger
import tr.emreone.kotlin_utils.math.Point2D
import kotlin.math.abs
object Day15 {
private val REPORT_PATTERN =
"""Sensor at x=(-?\d+), y=(-?\d+): closest beacon is at x=(-?\d+), y=(-?\d+)""".toRegex()
open class AreaElem(val coord: Point2D)
class Beacon(coord: Point2D) : AreaElem(coord) {}
class Sensor(coord: Point2D, val closestBeacon: Beacon) : AreaElem(coord) {
val distanceToClosestBeacon = coord.manhattanDistanceTo(closestBeacon.coord)
override fun toString(): String {
return "Sensor(coord=${coord}, distanceToClosestBeacon=$distanceToClosestBeacon)"
}
}
class Area {
val beacons = mutableListOf<Beacon>()
val sensors = mutableListOf<Sensor>()
companion object {
fun parseInput(input: List<String>): Area {
return Area().apply {
input.forEach { line ->
val (sensorX, sensorY, beaconX, beaconY) = REPORT_PATTERN.matchEntire(line)!!.destructured
val beacon = Beacon(Point2D(beaconX.toLong(), beaconY.toLong()))
beacons.add(beacon)
sensors.add(Sensor(Point2D(sensorX.toLong(), sensorY.toLong()), beacon))
}
// for part 2
sensors.sortBy { it.coord.x }
}
}
}
fun getMapElemAt(coord: Point2D): AreaElem? {
sensors.find { it.coord == coord }?.let { return it }
beacons.find { it.coord == coord }?.let { return it }
return null
}
fun calcTuningFrequency(coord: Point2D): Long {
return coord.x * 4_000_000 + coord.y
}
fun findDistressBeaconInRow(area: Area, yRow: Long, xMin: Long, xMax: Long): Point2D? {
var x = xMin
area.sensors.forEach { s ->
val currentPoint = Point2D(x, yRow)
// if currentPoint is in a sensor area
if (s.coord.manhattanDistanceTo(currentPoint) <= s.distanceToClosestBeacon) {
x = s.coord.x + s.distanceToClosestBeacon - abs(s.coord.y - yRow) + 1
}
}
return if (x <= xMax)
Point2D(x, yRow)
else {
null
}
}
// Printing methods
private fun isInSensorArea(coord: Point2D): Boolean {
return sensors.any { s ->
coord.manhattanDistanceTo(s.coord) <= s.distanceToClosestBeacon
}
}
fun printMap(min: Long, max: Long) {
val sb = StringBuilder()
sb.appendLine()
(min..max).forEach { y ->
(min..max).forEach { x ->
val currentPoint = Point2D(x, y)
when (this.getMapElemAt(currentPoint)) {
is Beacon -> {
sb.append("B")
}
is Sensor -> {
sb.append("S")
}
else -> {
if (this.isInSensorArea(currentPoint)) {
sb.append("#")
} else {
sb.append(" ")
}
}
}
}
sb.appendLine()
}
logger.info { sb.toString() }
}
}
fun part1(input: List<String>, yCoord: Long): Int {
val area = Area.parseInput(input)
val setOfPossibleCoords = mutableSetOf<Point2D>()
area.sensors.forEach { s ->
val xMin = s.coord.x - (s.distanceToClosestBeacon - abs(s.coord.y - yCoord))
val xMax = s.coord.x + (s.distanceToClosestBeacon - abs(s.coord.y - yCoord))
if (xMin <= xMax) {
(xMin..xMax).forEach { x ->
// if area is not empty
val currentPoint = Point2D(x, yCoord)
if (area.getMapElemAt(currentPoint) == null && s.coord.manhattanDistanceTo(currentPoint) <= s.distanceToClosestBeacon) {
setOfPossibleCoords.add(currentPoint)
}
}
}
}
// area.printMap(-10, 30)
return setOfPossibleCoords.size
}
fun part2(input: List<String>, minCoord: Long, maxCoord: Long): Long {
val area = Area.parseInput(input)
var foundLocation: Point2D? = null
try {
runBlocking {
launch(Dispatchers.Default.limitedParallelism(20)) {
(minCoord..maxCoord)
.shuffled()
.flatMap { y ->
listOf(async {
if (foundLocation == null) {
val distressBeacon = area.findDistressBeaconInRow(area, y, minCoord, maxCoord)
if (distressBeacon != null) {
logger.debug { "found distress beacon at $distressBeacon, --> Blocking Coroutines ..." }
foundLocation = distressBeacon
this@runBlocking.cancel()
}
}
})
}
}
}
} catch (e: Exception) {
// do nothing :D
}
return foundLocation?.let { area.calcTuningFrequency(it) } ?: -1
}
}