/
Day04.kt
37 lines (32 loc) · 1.47 KB
/
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
31
32
33
34
35
36
37
package com.akikanellis.adventofcode.year2022
object Day04 {
fun numberOfFullyOverlappingPairs(input: String) = firstToSecondSectionRange(input)
.count { (firstSectionRange, secondSectionRange) ->
firstSectionRange.containsFully(secondSectionRange) ||
secondSectionRange.containsFully(firstSectionRange)
}
fun numberOfPartiallyOverlappingPairs(input: String) = firstToSecondSectionRange(input)
.count { (firstSectionRange, secondSectionRange) ->
firstSectionRange.containsPartially(secondSectionRange) ||
secondSectionRange.containsPartially(firstSectionRange)
}
private fun firstToSecondSectionRange(input: String) = input
.lines()
.filter { it.isNotBlank() }
.map { Pair(it.substringBefore(","), it.substringAfter(",")) }
.map { (firstSectionRange, secondSectionRange) ->
Pair(
IntRange(
firstSectionRange.substringBefore("-").toInt(),
firstSectionRange.substringAfter("-").toInt()
),
IntRange(
secondSectionRange.substringBefore("-").toInt(),
secondSectionRange.substringAfter("-").toInt()
)
)
}
private fun IntRange.containsFully(other: IntRange) =
contains(other.first) && contains(other.last)
private fun IntRange.containsPartially(other: IntRange) = contains(other.first)
}