generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay04.kt
24 lines (17 loc) · 754 Bytes
/
Day04.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package mkuhn.aoc
import mkuhn.aoc.util.readInput
import mkuhn.aoc.util.overlaps
fun main() {
val ranges = readInput("Day04")
println(day4part1(ranges))
println(day4part2(ranges))
}
fun day4part1(input: List<String>): Int =
input.map { it.toRangePair() }
.count { it.first.contains(it.second) || it.second.contains(it.first) }
fun day4part2(input: List<String>): Int =
input.map { it.toRangePair() }
.count { it.first overlaps it.second }
fun String.toRangePair() = this.substringBefore(",").toRange() to this.substringAfter(",").toRange()
fun String.toRange(): IntRange = this.substringBefore("-").toInt() .. this.substringAfter("-").toInt()
fun IntRange.contains(range: IntRange) = range.minus(this).isEmpty()