Tested one puzzle.

Change-Id: I60720151667814f13881e16868d570282e509c77
This commit is contained in:
Chris Hodges 2023-11-17 11:53:16 +01:00
parent 4201e90672
commit 13a3503196
2 changed files with 42 additions and 0 deletions

View File

@ -8,6 +8,25 @@ import java.security.MessageDigest
fun readInput(name: String) = File("src", "$name.txt") fun readInput(name: String) = File("src", "$name.txt")
.readLines() .readLines()
fun listOfIntegerLists(input: List<String>): ArrayList<List<Int>> {
val output = ArrayList<List<Int>>()
var currList = ArrayList<Int>()
for(i in input)
{
if(i.isEmpty()){
output.add(currList)
currList = ArrayList<Int>()
} else {
currList.add(i.toInt())
}
}
if(currList.isNotEmpty())
{
output.add(currList)
}
return output
}
/** /**
* Converts string to md5 hash. * Converts string to md5 hash.
*/ */

23
src/aoc2022/Day01.kt Normal file
View File

@ -0,0 +1,23 @@
package aoc2022
import listOfIntegerLists
import println
import readInput
fun main() {
fun part1(input: List<String>): Int {
return listOfIntegerLists(input).map { it.sum() }.max()
}
fun part2(input: List<String>): 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()
}