aboutsummaryrefslogtreecommitdiff
path: root/aoc-2021-kotlin/src/Utils.kt
blob: 2be34acb357d68a083bf290c584e456ee56f20c6 (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
import java.io.File

fun readInputAsLines(name: String): List<String> = File("aoc-2021-kotlin/src", "$name.txt").readLines()

fun readInputAsString(name: String): String = File("aoc-2021-kotlin/src", "$name.txt").readText()

fun readInputAsNumbers(name: String): List<Int> = readInputAsLines(name).map(String::toInt)

fun readInputAsBitLists(name: String): List<List<Int>> =
    readInputAsLines(name)
        .map { binaryString -> binaryString.toList().map { bit -> bit.toString().toInt() } }

data class Pos2D(val x: Int, val y: Int) {
    companion object {
        val directions4 = listOf(Pos2D(0, 1), Pos2D(1, 0), Pos2D(0, -1), Pos2D(-1, 0))
        val directions8 = directions4 + listOf(
            Pos2D(1, 1),
            Pos2D(1, -1),
            Pos2D(-1, -1),
            Pos2D(-1, 1),
        )
    }

    operator fun plus(other: Pos2D) = Pos2D(x + other.x, y + other.y)

    operator fun minus(other: Pos2D) = Pos2D(x - other.x, y - other.y)
}

data class Pos3D(val x: Int, val y: Int, val z: Int) {
    companion object {
        val zero = Pos3D(0, 0, 0)
        val unique = Pos3D(1, 2, 3)

        fun fromString(string: String) = string
            .split(",")
            .map(String::toInt)
            .let { Pos3D(it[0], it[1], it[2]) }
    }

    operator fun unaryMinus() = Pos3D(-x, -y, -z)

    operator fun plus(other: Pos3D) = Pos3D(x + other.x, y + other.y, z + other.z)

    operator fun minus(other: Pos3D) = Pos3D(x - other.x, y - other.y, z - other.z)
}

fun parseToMap(input: List<String>): Map<Pos2D, Int> =
    input.flatMapIndexed { y, line ->
        line.mapIndexed { x, char ->
            Pos2D(x, y) to char.toString().toInt()
        }
    }.toMap()

fun <T> combinations(first: Iterable<T>, second: Iterable<T> = first): Sequence<Pair<T, T>> =
    sequence {
        first.forEach { a ->
            second.forEach { b ->
                yield(a to b)
            }
        }
    }