aboutsummaryrefslogtreecommitdiff
path: root/aoc-2021-kotlin/src/Day05.kt
blob: 2c5e219bdf057773b58c79ca3efd460497decad8 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import kotlin.math.absoluteValue
import kotlin.math.max
import kotlin.math.sign

object Day05 {
    private data class Line(val start: Pos2D, val end: Pos2D) {
        companion object {
            fun fromString(input: String): Line {
                val (start, end) = input.split(" -> ").map { coordinateString ->
                    val (x, y) = coordinateString.split(",").map(String::toInt)
                    Pos2D(x, y)
                }

                return Line(start, end)
            }
        }

        val isHorizontalOrVertical: Boolean
            get() = start.x == end.x || start.y == end.y

        val isDiagonal: Boolean
            get() = (end.x - start.x).absoluteValue == (end.y - start.y).absoluteValue

        val pointSequence: Sequence<Pos2D>
            get() = sequence {
                val xOffset = end.x - start.x
                val yOffset = end.y - start.y

                for (s in 0..max(xOffset.absoluteValue, yOffset.absoluteValue)) {
                    val x = start.x + s * xOffset.sign
                    val y = start.y + s * yOffset.sign

                    yield(Pos2D(x, y))
                }
            }
    }

    private fun helper(input: List<String>, linePredicate: (line: Line) -> Boolean) = input
        .asSequence()
        .map(Line::fromString)
        .filter(linePredicate)
        .flatMap(Line::pointSequence)
        .groupingBy { it }
        .eachCount()
        .values
        .count { it >= 2 }

    fun part1(input: List<String>): Int = helper(input, Line::isHorizontalOrVertical)

    fun part2(input: List<String>): Int = helper(input) { it.isHorizontalOrVertical || it.isDiagonal }

}

fun main() {
    val testInput = readInputAsLines("Day05_test")
    check(Day05.part1(testInput) == 5)
    check(Day05.part2(testInput) == 12)

    val input = readInputAsLines("Day05")
    println(Day05.part1(input))
    println(Day05.part2(input))
}