Add solution for day 15 part 1
This commit is contained in:
parent
4dad375d17
commit
1529c9709b
1 changed files with 26 additions and 0 deletions
26
src/Day15.kt
Normal file
26
src/Day15.kt
Normal 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
|
||||||
|
}
|
Loading…
Reference in a new issue