-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay04.kt
34 lines (27 loc) · 1.13 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
package aoc.years.year2022
import aoc.Day
@Year2022
class Day04 : Day() {
override fun solvePart1(input: List<String>): Any {
return input
.asSequence()
.mapToRanges()
.map { (first, second) -> rangesOverlap(first, second) }
.count { it }
}
override fun solvePart2(input: List<String>): Any {
return input
.asSequence()
.mapToRanges()
.map { (first, second) -> first.intersect(second).isNotEmpty() }
.count { it }
}
private fun rangesOverlap(firstRange: IntRange, secondRange: IntRange): Boolean {
return (firstRange.contains(secondRange.first) && firstRange.contains(secondRange.last)) ||
(secondRange.contains(firstRange.first) && secondRange.contains(firstRange.last))
}
private fun Sequence<String>.mapToRanges(): Sequence<Pair<IntRange, IntRange>> =
map { it.split(",") }
.map { (first, second) -> Pair(first.split("-"), second.split("-")) }
.map { (first, second) -> Pair(first[0].toInt()..first[1].toInt(), second[0].toInt()..second[1].toInt()) }
}