-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package me.gergo | ||
|
||
import java.io.File | ||
import kotlin.math.abs | ||
|
||
fun main() { | ||
val sensors = File("src/main/resources/input15.txt").readLines() | ||
.map(::parseSensors) | ||
|
||
val maxRange = sensors.maxOf(Sensor::range) | ||
val minX = sensors.minOf { it.closestBeacon.x } - maxRange | ||
val maxX = sensors.maxOf { it.closestBeacon.x } + maxRange | ||
val beacons = sensors.map(Sensor::closestBeacon).toSet() | ||
|
||
// Part One | ||
val atY = 2000000 | ||
val result1 = (minX..maxX).count { x -> | ||
sensors.any { it.inRange(Coordinate(x, atY)) } // Number of coordinates covered by the sensor in our row, | ||
} - beacons.count { it.y == atY } // ...minus the number of known beacons in that row | ||
|
||
println("Positions that cannot contain a beacon: $result1") | ||
|
||
// Part Two | ||
val max = 4000000 | ||
val tuningFrequency = | ||
sensors.flatMap(Sensor::outerBorder) // The search space can be greatly limited by considering the outer border of the sensor range | ||
.filter { it.x in 0..max && it.y in 0..max } // ...except for all the coordinates that fall outside the 0..4000000 range | ||
.distinct() // ...removing duplicates | ||
.filter { c -> sensors.none { s -> s.inRange(c) } } // ...finally selecting the undetected coordinate | ||
.map { p -> p.x * 4000000L + p.y } | ||
println("Distress beacon tuning frequency: $tuningFrequency") | ||
} | ||
|
||
private data class Sensor(val position: Coordinate, val closestBeacon: Coordinate) { | ||
val range = manhattanDistance(position, closestBeacon.x, closestBeacon.y) | ||
fun inRange(c: Coordinate) = manhattanDistance(position, c.x, c.y) <= range | ||
|
||
fun outerBorder() = sequence<Coordinate> { | ||
val radius = range | ||
|
||
for (i in 0..radius) { // Left to top | ||
yield(Coordinate(position.x - radius - 1 + i, position.y - i)) | ||
} | ||
for (i in 0..radius) { // Top to right | ||
yield(Coordinate(position.x + i, position.y - radius - 1 + i)) | ||
} | ||
for (i in 0..radius) { // Right to bottom | ||
yield(Coordinate(position.x + radius + 1 - i, position.y + i)) | ||
} | ||
for (i in 0..radius) { // Bottom to left | ||
yield(Coordinate(position.x - i, position.y + radius + 1 - i)) | ||
} | ||
} | ||
} | ||
|
||
private fun manhattanDistance(a: Coordinate, x: Int, y: Int) = abs(a.x - x) + abs(a.y - y) | ||
|
||
private val SensorBeaconFormat = Regex("Sensor at x=(-?\\d+), y=(-?\\d+): closest beacon is at x=(-?\\d+), y=(-?\\d+)") | ||
private fun parseSensors(line: String): Sensor { | ||
val (_, sx, sy, bx, by) = SensorBeaconFormat.matchEntire(line)!!.groupValues | ||
return Sensor(Coordinate(sx.toInt(), sy.toInt()), Coordinate(bx.toInt(), by.toInt())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Sensor at x=3772068, y=2853720: closest beacon is at x=4068389, y=2345925 | ||
Sensor at x=78607, y=2544104: closest beacon is at x=-152196, y=4183739 | ||
Sensor at x=3239531, y=3939220: closest beacon is at x=3568548, y=4206192 | ||
Sensor at x=339124, y=989831: closest beacon is at x=570292, y=1048239 | ||
Sensor at x=3957534, y=2132743: closest beacon is at x=3897332, y=2000000 | ||
Sensor at x=1882965, y=3426126: closest beacon is at x=2580484, y=3654136 | ||
Sensor at x=1159443, y=3861139: closest beacon is at x=2580484, y=3654136 | ||
Sensor at x=2433461, y=287013: closest beacon is at x=2088099, y=-190228 | ||
Sensor at x=3004122, y=3483833: closest beacon is at x=2580484, y=3654136 | ||
Sensor at x=3571821, y=799602: closest beacon is at x=3897332, y=2000000 | ||
Sensor at x=2376562, y=1539540: closest beacon is at x=2700909, y=2519581 | ||
Sensor at x=785113, y=1273008: closest beacon is at x=570292, y=1048239 | ||
Sensor at x=1990787, y=38164: closest beacon is at x=2088099, y=-190228 | ||
Sensor at x=3993778, y=3482849: closest beacon is at x=4247709, y=3561264 | ||
Sensor at x=3821391, y=3986080: closest beacon is at x=3568548, y=4206192 | ||
Sensor at x=2703294, y=3999015: closest beacon is at x=2580484, y=3654136 | ||
Sensor at x=1448314, y=2210094: closest beacon is at x=2700909, y=2519581 | ||
Sensor at x=3351224, y=2364892: closest beacon is at x=4068389, y=2345925 | ||
Sensor at x=196419, y=3491556: closest beacon is at x=-152196, y=4183739 | ||
Sensor at x=175004, y=138614: closest beacon is at x=570292, y=1048239 | ||
Sensor at x=1618460, y=806488: closest beacon is at x=570292, y=1048239 | ||
Sensor at x=3974730, y=1940193: closest beacon is at x=3897332, y=2000000 | ||
Sensor at x=2995314, y=2961775: closest beacon is at x=2700909, y=2519581 | ||
Sensor at x=105378, y=1513086: closest beacon is at x=570292, y=1048239 | ||
Sensor at x=3576958, y=3665667: closest beacon is at x=3568548, y=4206192 | ||
Sensor at x=2712265, y=2155055: closest beacon is at x=2700909, y=2519581 |