generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathY2016D03.kt
35 lines (29 loc) · 1.42 KB
/
Y2016D03.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.y2016
import io.github.jadarma.aockt.core.Solution
object Y2016D03 : Solution {
/** Parses the input, returning the number rows as triples of integers. */
private fun parseInput(input: String): Sequence<Triple<Int, Int, Int>> =
input
.lineSequence()
.map {
val (a, b, c) = it
.trim()
.replace(Regex("""\s+"""), ",")
.split(',')
.map(String::toInt)
Triple(a, b, c)
}
/** Given a sequence of rows of three integers, maps it to a sequence of vertically transposed numbers. */
private fun Sequence<Triple<Int, Int, Int>>.readVertically(): Sequence<Triple<Int, Int, Int>> =
chunked(3) { row ->
sequenceOf(
Triple(row[0].first, row[1].first, row[2].first),
Triple(row[0].second, row[1].second, row[2].second),
Triple(row[0].third, row[1].third, row[2].third),
)
}.flatten()
/** Checks whether three integers could be the values of the length of a triangle. */
private fun Triple<Int, Int, Int>.couldBeTriangle(): Boolean = toList().sorted().let { (a, b, c) -> a + b > c }
override fun partOne(input: String) = parseInput(input).count { it.couldBeTriangle() }
override fun partTwo(input: String) = parseInput(input).readVertically().count { it.couldBeTriangle() }
}