chore: format and lint all files

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-12-02 14:24:52 +01:00
parent ab7a9ecf74
commit ef5509f4de
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8
2 changed files with 49 additions and 18 deletions

View file

@ -5,30 +5,51 @@ import java.security.MessageDigest
/** /**
* Converts string to md5 hash. * Converts string to md5 hash.
*/ */
fun String.md5() = BigInteger(1, MessageDigest.getInstance("MD5").digest(toByteArray())) fun String.md5() =
.toString(16) BigInteger(1, MessageDigest.getInstance("MD5").digest(toByteArray()))
.padStart(32, '0') .toString(16)
.padStart(32, '0')
/** /**
* The cleaner shorthand for printing output. * The cleaner shorthand for printing output.
*/ */
fun Any?.println() = println(this) fun Any?.println() = println(this)
private fun openFile(day: Int, name: String) = File("inputs/day%02d".format(day), "$name.txt") private fun openFile(
day: Int,
name: String,
) = File("inputs/day%02d".format(day), "$name.txt")
/** /**
* Reads lines from the given input txt file. * Reads lines from the given input txt file.
*/ */
fun readInput(day: Int, name: String) = openFile(day, name).readText().trim().lines() fun readInput(
day: Int,
name: String,
) = openFile(day, name).readText().trim().lines()
fun readInputAsString(day: Int, name: String) = openFile(day, name).readText() fun readInputAsString(
fun readInputAsInts(day: Int, name: String) = readInput(day, name).map { it.toInt() } day: Int,
fun readInputAsCommaSeparatedInts(day: Int, name: String) = openFile(day, name) name: String,
) = openFile(day, name).readText()
fun readInputAsInts(
day: Int,
name: String,
) = readInput(day, name).map { it.toInt() }
fun readInputAsCommaSeparatedInts(
day: Int,
name: String,
) = openFile(day, name)
.readText() .readText()
.split(",") .split(",")
.map { it.toInt() } .map { it.toInt() }
fun readGraph(day: Int, name: String) = readInput(day, name).fold(mapOf<String, Set<String>>()) { currentGraph, edge -> fun readGraph(
day: Int,
name: String,
) = readInput(day, name).fold(mapOf<String, Set<String>>()) { currentGraph, edge ->
val (fromVertex, toVertex) = edge.split("-") val (fromVertex, toVertex) = edge.split("-")
val fromNeighbours = currentGraph.getOrDefault(fromVertex, emptySet()) + toVertex val fromNeighbours = currentGraph.getOrDefault(fromVertex, emptySet()) + toVertex
val toNeighbours = currentGraph.getOrDefault(toVertex, emptySet()) + fromVertex val toNeighbours = currentGraph.getOrDefault(toVertex, emptySet()) + fromVertex
@ -36,14 +57,24 @@ fun readGraph(day: Int, name: String) = readInput(day, name).fold(mapOf<String,
currentGraph + mapOf(fromVertex to fromNeighbours, toVertex to toNeighbours) currentGraph + mapOf(fromVertex to fromNeighbours, toVertex to toNeighbours)
}.toMap() }.toMap()
fun <A, B> product(xs: Sequence<A>, ys: Sequence<B>): Sequence<Pair<A, B>> = fun <A, B> product(
xs.flatMap { x -> ys.map { y -> x to y } } xs: Sequence<A>,
ys: Sequence<B>,
): Sequence<Pair<A, B>> = xs.flatMap { x -> ys.map { y -> x to y } }
fun <A, B, C> product(xs: Sequence<A>, ys: Sequence<B>, zs: Sequence<C>): Sequence<Triple<A, B, C>> = fun <A, B, C> product(
xs.flatMap { x -> ys.flatMap { y -> zs.map { z -> Triple(x, y, z) } } } xs: Sequence<A>,
ys: Sequence<B>,
zs: Sequence<C>,
): Sequence<Triple<A, B, C>> = xs.flatMap { x -> ys.flatMap { y -> zs.map { z -> Triple(x, y, z) } } }
fun <A, B> product(xs: Iterable<A>, ys: Iterable<B>): Sequence<Pair<A, B>> = fun <A, B> product(
product(xs.asSequence(), ys.asSequence()) xs: Iterable<A>,
ys: Iterable<B>,
): Sequence<Pair<A, B>> = product(xs.asSequence(), ys.asSequence())
fun <A, B, C> product(xs: Iterable<A>, ys: Iterable<B>, zs: Iterable<C>): Sequence<Triple<A, B, C>> = fun <A, B, C> product(
product(xs.asSequence(), ys.asSequence(), zs.asSequence()) xs: Iterable<A>,
ys: Iterable<B>,
zs: Iterable<C>,
): Sequence<Triple<A, B, C>> = product(xs.asSequence(), ys.asSequence(), zs.asSequence())