-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathy23day5part2.kt
More file actions
31 lines (29 loc) · 1.37 KB
/
Copy pathy23day5part2.kt
File metadata and controls
31 lines (29 loc) · 1.37 KB
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
import kotlin.time.measureTime
fun main() {
fun longRangeOf(first: Long, size: Long) = first until first + size
data class Range(val src: LongRange, val dst: LongRange)
data class CategoryMap(val scr: String, val dst: String, val ranges: List<Range>)
val seeds = readln().substringAfter("seeds: ").splitToSequence(' ').map(String::toLong)
.chunked(2).map { longRangeOf(it[0], it[1]) }.toList().sortedBy { it.first }
readln()
val maps = generateSequence {
readlnOrNull()?.let { header ->
val (srcName, dstName) = """(\w+)-to-(\w+) map:""".toRegex().matchEntire(header)!!.destructured
val ranges = generateSequence { readlnOrNull()?.takeIf { it.isNotEmpty() } }.map {
val (dst, src, size) = it.split(' ').map(String::toLong)
Range(longRangeOf(src, size), longRangeOf(dst, size))
}.toList().sortedBy { it.dst.first }
CategoryMap(srcName, dstName, ranges)
}
}.toList().reversed()
measureTime {
// brute force )
generateSequence(0L, Long::inc).first { location ->
println(location)
val origin = maps.fold(location) { num, map ->
map.ranges.find { num in it.dst }?.let { num - it.dst.first + it.src.first } ?: num
}
seeds.any { origin in it }
}.let(::println)
}.let(::println)
}