Skip to content

Commit bc21e34

Browse files
committed
Solved day15 part 1
1 parent decefee commit bc21e34

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2023'
7-
version '0.14.2'
7+
version '0.14.3'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
package de.havox_design.aoc2023.day15
22

33
class Day15(private var filename: String) {
4+
private val ELEMENT_DELIMITER = ","
5+
46
fun solvePart1(): Long =
5-
1320L
7+
getResourceAsText(filename)[0]
8+
.split(ELEMENT_DELIMITER)
9+
.sumOf { word -> calculateHash(word) }
10+
.toLong()
611

712
fun solvePart2(): Long =
813
0L
914

15+
private fun calculateHash(word: String): Int =
16+
word
17+
.fold(0) { acc, c -> ((acc + c.code) * 17) % 256 }
18+
1019
private fun getResourceAsText(path: String): List<String> =
1120
this.javaClass.classLoader.getResourceAsStream(path)!!.bufferedReader().readLines()
1221
}

0 commit comments

Comments
 (0)