From 1529c9709b07874c38ca1b625e64972a4f96c3ea Mon Sep 17 00:00:00 2001 From: William Brawner Date: Sat, 16 Dec 2023 22:00:41 -0700 Subject: [PATCH] Add solution for day 15 part 1 --- src/Day15.kt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/Day15.kt diff --git a/src/Day15.kt b/src/Day15.kt new file mode 100644 index 0000000..bfc12de --- /dev/null +++ b/src/Day15.kt @@ -0,0 +1,26 @@ +fun main() { + fun part1(input: List): Int { + return input.first() + .split(',') + .fold(0) { acc, s -> + acc + s.hash() + } + } + + fun part2(input: List): 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 +} \ No newline at end of file