28 lines
1.1 KiB
Kotlin
28 lines
1.1 KiB
Kotlin
import java.io.File
|
|
|
|
//TIP Press <shortcut raw="SHIFT"/> twice to open the Search Everywhere dialog and type <b>show whitespaces</b>,
|
|
// then press <shortcut raw="ENTER"/>. You can now see whitespace characters in your code.
|
|
fun main() {
|
|
val file = File("b.input").readLines();
|
|
val pairsOfSets = file.map {
|
|
Pair(it.substringBefore(","), it.substringAfter(","))
|
|
};
|
|
|
|
val number = pairsOfSets.filter { (a, b) -> cntains(parseRange(a), parseRange(b)) }.count();
|
|
println("The number of contained sets is $number");
|
|
|
|
val number2 = pairsOfSets.filter { (a, b) -> overlaps(parseRange(a), parseRange(b)) }.count();
|
|
println("The number of overlapping sets is $number2");
|
|
}
|
|
|
|
fun cntains(a:Pair<Int,Int>, b:Pair<Int,Int>): Boolean {
|
|
return ((a.first <= b.first) and (a.second >= b.second)) or
|
|
((b.first <= a.first) and (b.second >= a.second));
|
|
}
|
|
fun overlaps(a:Pair<Int,Int>, b:Pair<Int,Int>): Boolean {
|
|
return (a.first in b.first..b.second) or (a.second in b.first..b.second) or cntains(a,b);
|
|
}
|
|
|
|
fun parseRange(a:String): Pair<Int, Int> {
|
|
return Pair(a.substringBefore("-").toInt(),a.substringAfter("-").toInt());
|
|
} |