-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path05.kts
65 lines (64 loc) · 2.37 KB
/
05.kts
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
import java.util.*
val scanner = Scanner(System.`in`)
val mappings = ArrayList<List<Pair<LongRange, Long>>>()
val seeds = ArrayList<Long>()
val number = Regex("\\d+")
while (scanner.hasNext()) {
val line = scanner.nextLine()
if (line.startsWith("seeds:")) {
seeds.addAll(number.findAll(line).map { it.value.toLong() })
} else if (line.endsWith("map:")) {
val m = ArrayList<Pair<LongRange, Long>>()
while (scanner.hasNext()) {
val l = scanner.nextLine()
if (l == "") break
val mm = number.findAll(l).map { it.value.toLong() }.toList()
m.add(Pair((mm[1]..<(mm[1] + mm[2])), mm[0]))
}
mappings.add(m)
}
}
var minLocation = Long.MAX_VALUE
for (seed in seeds) {
var mapped = seed
for (mapping in mappings) {
for (r in mapping) {
if (r.first.contains(mapped)) {
mapped = r.second + (mapped - r.first.first)
break
}
}
}
minLocation = minOf(minLocation, mapped)
}
var ranges = ArrayList<LongRange>()
for (i in 0..<seeds.size step 2) {
ranges.add((seeds[i]..<seeds[i] + seeds[i + 1]))
}
for (mapping in mappings) {
val mapped = ArrayList<LongRange>()
fun map(rr: LongRange) {
var mr: Optional<LongRange> = Optional.empty()
for (r in mapping) {
val offset = r.second - r.first.first
if (r.first.contains(rr.first) && r.first.contains(rr.last)) {
mr = Optional.of(((rr.first + offset)..(rr.last + offset)))
} else if (r.first.contains(rr.first)) {
mr = Optional.of(((rr.first + offset)..(r.first.last + offset)))
map(((r.first.last + 1)..rr.last))
} else if (r.first.contains(rr.last)) {
mr = Optional.of(((r.first.first + offset)..(rr.last + offset)))
map((rr.first..(r.first.first - 1)))
} else if (rr.contains(r.first.first) && rr.contains(r.first.last)) {
map((rr.first..(r.first.first - 1)))
map(((r.first.last + 1)..rr.last))
mr = Optional.of(((r.first.first + offset)..(r.first.last + offset)))
}
if (mr.isPresent) break
}
mapped.add(if (mr.isPresent) mr.get() else rr)
}
for (r in ranges) map(r)
ranges = mapped
}
println(listOf(minLocation, ranges.minOf { it.first }))