-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day04.kt
38 lines (31 loc) · 1.07 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
38
package day04
import common.InputRepo
import common.readSessionCookie
import common.solve
import util.fullyContains
import util.splitIntoPair
fun main(args: Array<String>) {
val day = 4
val input = InputRepo(args.readSessionCookie()).get(day = day)
solve(day, input, ::solveDay04Part1, ::solveDay04Part2)
}
fun solveDay04Part1(input: List<String>): Int {
val sum: Int = mapToRanges(input)
.map { (range1, range2) ->
range1.fullyContains(range2) || range2.fullyContains(range1)
}
.count { it }
return sum
}
fun solveDay04Part2(input: List<String>): Int {
return mapToRanges(input)
.map { (range1, range2) -> range1.any { range2.contains(it) } }
.count { it }
}
private fun mapToRanges(input: List<String>) = input.map { it.splitIntoPair(",") }
.map { (left, right) ->
val leftPair = left.splitIntoPair("-")
val rightPair = right.splitIntoPair("-")
IntRange(leftPair.first.toInt(), leftPair.second.toInt()) to
IntRange(rightPair.first.toInt(), rightPair.second.toInt())
}