-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay04CampCleanup.kt
More file actions
32 lines (25 loc) · 1001 Bytes
/
Day04CampCleanup.kt
File metadata and controls
32 lines (25 loc) · 1001 Bytes
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
package adventofcode.year2022
import adventofcode.Puzzle
import adventofcode.PuzzleInput
class Day04CampCleanup(
customInput: PuzzleInput? = null,
) : Puzzle(customInput) {
private val assignmentPairs by lazy {
input
.lines()
.map {
ASSIGNMENT_PAIR_REGEX
.find(it)!!
.destructured
.toList()
.map(String::toInt)
}.map { (a, b, c, d) -> a..b to c..d }
}
override fun partOne() = assignmentPairs.count { (a, b) -> a in b || b in a }
override fun partTwo() = assignmentPairs.count { (a, b) -> a overlaps b }
companion object {
private val ASSIGNMENT_PAIR_REGEX = """(\d+)-(\d+),(\d+)-(\d+)""".toRegex()
private operator fun IntRange.contains(other: IntRange) = first <= other.first && last >= other.last
private infix fun IntRange.overlaps(other: IntRange) = first <= other.last && other.first <= last
}
}