diff --git a/src/Utils.kt b/src/Utils.kt index 543b7c4..24d5a7f 100644 --- a/src/Utils.kt +++ b/src/Utils.kt @@ -8,6 +8,25 @@ import java.security.MessageDigest fun readInput(name: String) = File("src", "$name.txt") .readLines() +fun listOfIntegerLists(input: List): ArrayList> { + val output = ArrayList>() + var currList = ArrayList() + for(i in input) + { + if(i.isEmpty()){ + output.add(currList) + currList = ArrayList() + } else { + currList.add(i.toInt()) + } + } + if(currList.isNotEmpty()) + { + output.add(currList) + } + return output +} + /** * Converts string to md5 hash. */ diff --git a/src/aoc2022/Day01.kt b/src/aoc2022/Day01.kt new file mode 100644 index 0000000..7195847 --- /dev/null +++ b/src/aoc2022/Day01.kt @@ -0,0 +1,23 @@ +package aoc2022 + +import listOfIntegerLists +import println +import readInput + +fun main() { + fun part1(input: List): Int { + return listOfIntegerLists(input).map { it.sum() }.max() + } + + fun part2(input: List): Int { + return listOfIntegerLists(input).map { it.sum() }.sortedDescending().take(3).sum() + } + + // test if implementation meets criteria from the description, like: + val testInput = readInput("Day01") + check(part1(testInput) == 69836) + + val input = readInput("Day01") + part1(input).println() + part2(input).println() +}