Skip to content

Instantly share code, notes, and snippets.

@mkulak
Last active December 3, 2022 18:33
Show Gist options
  • Save mkulak/bf1f7c6b8bf0783aa3a18b482398e8ba to your computer and use it in GitHub Desktop.
Save mkulak/bf1f7c6b8bf0783aa3a18b482398e8ba to your computer and use it in GitHub Desktop.
fun main() {
println(solve1(File("input1.txt").readLines()))
}
fun solve1(input: List<String>): Int =
input.fold(0 to 0) { (m, acc), str -> if (str.isEmpty()) Math.max(m, acc) to 0 else m to acc + str.toInt() }.first
fun solve2(input: List<String>): Int =
input.fold(arrayListOf(0)) { acc, str -> if (str == "") acc.add(0) else acc[acc.lastIndex] += str.toInt(); acc }.sortedBy { -it }.take(3).sum()
// minimum but inefficient:
fun main() {
println(File("input1.txt").readText().split("\n\n").map { it.split("\n").sumOf { it.toInt() } }.sortedBy { -it }.take(3).sum())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment