generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathY2022D04.kt
35 lines (28 loc) · 1.44 KB
/
Y2022D04.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
package aockt.y2022
import io.github.jadarma.aockt.core.Solution
object Y2022D04 : Solution {
private val inputRegex = Regex("""^(\d+)-(\d+),(\d+)-(\d+)$""")
/** Parses the [input] and returns the list of elf cleanup section assignments. */
private fun parseInput(input: String): List<Pair<IntRange, IntRange>> =
input
.lineSequence()
.map { line -> inputRegex.matchEntire(line)!!.destructured }
.map { (x1, x2, y1, y2) -> x1.toInt()..x2.toInt() to y1.toInt()..y2.toInt() }
.toList()
/** Returns whether this [IntRange] is fully contained within the [other] or vice-versa. */
private infix fun IntRange.fullyOverlapsWith(other: IntRange): Boolean = when {
isEmpty() || other.isEmpty() -> false
this.first <= other.first && this.last >= other.last -> true
this.first >= other.first && this.last <= other.last -> true
else -> false
}
/** Returns whether this [IntRange] has any overlap with the [other]. */
private infix fun IntRange.overlapsWith(other: IntRange): Boolean = when {
isEmpty() || other.isEmpty() -> false
this.last < other.first -> false
this.first > other.last -> false
else -> true
}
override fun partOne(input: String): Any = parseInput(input).count { (x, y) -> x fullyOverlapsWith y }
override fun partTwo(input: String): Any = parseInput(input).count { (x, y) -> x overlapsWith y }
}