fun main() { puzzle("1 & 2") { lensLibrary(input()) } } fun lensLibrary(input: String): Any { val lenses = input.split(",") val part1 = lenses.sumOf { it.hash() } val part2 = buildMap() { for (l in lenses) if (l.contains('-')) remove(l.substringBefore('-')) else l.split('=').let { (a, b) -> this[a] = b.toInt() } }.entries.groupBy { it.key.hash() } .map { (it.key + 1) * it.value.foldIndexed(0) { i, acc, lens -> acc + (i + 1) * lens.value } } .sum() return part1 to part2 } fun String.hash() = this.fold(0) { acc, c -> (acc + c.code) * 17 % 256 }