Last active
December 3, 2022 18:33
-
-
Save mkulak/bf1f7c6b8bf0783aa3a18b482398e8ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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