Add solution for day 15 part 1

This commit is contained in:
William Brawner 2023-12-16 22:00:41 -07:00
parent 4dad375d17
commit 1529c9709b
Signed by: wbrawner
GPG key ID: 8FF12381C6C90D35

26
src/Day15.kt Normal file
View file

@ -0,0 +1,26 @@
fun main() {
fun part1(input: List<String>): Int {
return input.first()
.split(',')
.fold(0) { acc, s ->
acc + s.hash()
}
}
fun part2(input: List<String>): Int {
return input.size
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day15_test")
check(part1(testInput) == 1320)
val input = readInput("Day15")
part1(input).println()
check(part2(testInput) == 0)
part2(input).println()
}
fun String.hash(): Int = toCharArray().fold(0) { sum, char ->
((sum + char.code) * 17) % 256
}