Skip to content

Commit ef017b9

Browse files
committed
Add day 15
1 parent 02bc44a commit ef017b9

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

2022/15/15.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1+
fun dist(x1: Long, y1: Long, x2: Long, y2: Long) = Math.abs(x1 - x2) + Math.abs(y1 - y2)
2+
13
fun main() {
24
val sensors = generateSequence(::readlnOrNull).toList()
35
.map { Regex("-?\\d+").findAll(it).toList() }
46
.map { it.map { it.value.toLong() } }
5-
.sortedWith() { a: List<Long>, b: List<Long> -> a[0].compareTo(b[0]) }
6-
fun dist(x1: Long, y1: Long, x2: Long, y2: Long) = Math.abs(x1 - x2) + Math.abs(y1 - y2)
7+
.sortedBy() { it[0] }
78
val y = 2_000_000L
8-
(-1_000_000L..6_000_000L).map { x ->
9+
(-1_000_000L..6_000_000L).count { x ->
910
sensors
10-
.filter { (_, _, xb, yb) -> xb != x || yb != y }
11-
.map { (xs, ys, xb, yb) -> dist(xs, ys, xb, yb) >= dist(xs, ys, x, y) }
12-
.any { it }
13-
}.count { it }.run(::println)
11+
.filter { it[2] != x || it[3] != y }
12+
.any { (xs, ys, xb, yb) -> dist(xs, ys, x, y) <= dist(xs, ys, xb, yb) }
13+
}.run(::println)
1414

1515
for (y in 0L..4_000_000L) {
1616
var x = 0L
1717
for ((xs, ys, xb, yb) in sensors)
18-
if (dist(xs, ys, xb, yb) >= dist(xs, ys, x, y))
18+
if (dist(xs, ys, x, y) <= dist(xs, ys, xb, yb))
1919
x = xs + dist(xs, ys, xb, yb) - Math.abs(ys - y) + 1
2020
if (x <= 4_000_000L)
2121
println(x * 4_000_000L + y)

2022/15/input.ans

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
5716881
2+
10852583132904

2022/15/input.in

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Sensor at x=2885528, y=2847539: closest beacon is at x=2966570, y=2470834
2+
Sensor at x=2224704, y=1992385: closest beacon is at x=2018927, y=2000000
3+
Sensor at x=3829144, y=1633329: closest beacon is at x=2966570, y=2470834
4+
Sensor at x=43913, y=426799: closest beacon is at x=152363, y=369618
5+
Sensor at x=2257417, y=2118161: closest beacon is at x=2386559, y=2090397
6+
Sensor at x=8318, y=3994839: closest beacon is at x=-266803, y=2440278
7+
Sensor at x=69961, y=586273: closest beacon is at x=152363, y=369618
8+
Sensor at x=3931562, y=3361721: closest beacon is at x=3580400, y=3200980
9+
Sensor at x=476279, y=3079924: closest beacon is at x=-266803, y=2440278
10+
Sensor at x=2719185, y=2361091: closest beacon is at x=2966570, y=2470834
11+
Sensor at x=2533382, y=3320911: closest beacon is at x=2260632, y=3415930
12+
Sensor at x=3112735, y=3334946: closest beacon is at x=3580400, y=3200980
13+
Sensor at x=1842258, y=3998928: closest beacon is at x=2260632, y=3415930
14+
Sensor at x=3712771, y=3760832: closest beacon is at x=3580400, y=3200980
15+
Sensor at x=1500246, y=2684955: closest beacon is at x=2018927, y=2000000
16+
Sensor at x=3589321, y=142859: closest beacon is at x=4547643, y=-589891
17+
Sensor at x=1754684, y=2330721: closest beacon is at x=2018927, y=2000000
18+
Sensor at x=2476631, y=3679883: closest beacon is at x=2260632, y=3415930
19+
Sensor at x=27333, y=274008: closest beacon is at x=152363, y=369618
20+
Sensor at x=158732, y=2405833: closest beacon is at x=-266803, y=2440278
21+
Sensor at x=2955669, y=3976939: closest beacon is at x=3035522, y=4959118
22+
Sensor at x=1744196, y=13645: closest beacon is at x=152363, y=369618
23+
Sensor at x=981165, y=1363480: closest beacon is at x=2018927, y=2000000
24+
Sensor at x=2612279, y=2151377: closest beacon is at x=2386559, y=2090397
25+
Sensor at x=3897, y=2076376: closest beacon is at x=-266803, y=2440278
26+
Sensor at x=2108479, y=1928318: closest beacon is at x=2018927, y=2000000
27+
Sensor at x=1913043, y=3017841: closest beacon is at x=2260632, y=3415930
28+
Sensor at x=2446778, y=785075: closest beacon is at x=2386559, y=2090397
29+
Sensor at x=2385258, y=2774943: closest beacon is at x=2386559, y=2090397
30+
Sensor at x=3337656, y=2916144: closest beacon is at x=3580400, y=3200980
31+
Sensor at x=380595, y=66906: closest beacon is at x=152363, y=369618
32+
Sensor at x=1593628, y=3408455: closest beacon is at x=2260632, y=3415930

0 commit comments

Comments
 (0)