aboutsummaryrefslogtreecommitdiff
path: root/src/Day05.kt
blob: eaa7b662784e5dbc559f8157f94cba98abf95189 (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
import kotlin.math.absoluteValue
import kotlin.math.max
import kotlin.math.sign

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))
            }
        }
}

fun main() {
    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 }

    val testInput = readInputAsLines("Day05_test")
    check(part1(testInput) == 5)
    check(part2(testInput) == 12)

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