-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay04.kt
30 lines (24 loc) · 867 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
25
26
27
28
29
30
package tr.emreone.adventofcode.days
import tr.emreone.adventofcode.overlaps
import tr.emreone.adventofcode.contains
object Day04 {
private val pattern = """(\d+)-(\d+),(\d+)-(\d+)""".toRegex()
private fun parseRange(line: String): Pair<IntRange, IntRange> {
val (a, b, c, d) = pattern.matchEntire(line)!!.destructured
val range1 = IntRange(a.toInt(), b.toInt())
val range2 = IntRange(c.toInt(), d.toInt())
return range1 to range2
}
fun part1(input: List<String>): Int {
return input.count { line ->
val (range1, range2) = parseRange(line)
(range1 in range2) || (range2 in range1)
}
}
fun part2(input: List<String>): Int {
return input.count { line ->
val (range1, range2) = parseRange(line)
range1 overlaps range2
}
}
}