Skip to content

Commit

Permalink
2022 Day 25 refactor to char map to remove toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
markjfisher committed Dec 25, 2022
1 parent 455b9cd commit ff5ae63
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 8 deletions.
9 changes: 2 additions & 7 deletions advents/src/main/kotlin/net/fish/y2022/Day25.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ object Day25 : Day {

fun doPart2(data: List<String>): Int = data.size - 127

private val toDigits = mapOf("0" to 0L, "1" to 1L, "2" to 2L, "-" to -1L, "=" to -2L)
private val toDigits = mapOf('0' to 0L, '1' to 1L, '2' to 2L, '-' to -1L, '=' to -2L)
private val fromDigits = toDigits.entries.associate { it.value to it.key }

fun fromSnafu(s: String): Long {
return s.fold(0L) { ac, d ->
ac * 5L + toDigits[d.toString()]!!
}
}
fun fromSnafu(s: String): Long = s.fold(0L) { ac, d -> ac * 5L + toDigits[d]!! }

fun toSnafu(n: Long): String {
return if (n == 0L) ""
Expand All @@ -31,7 +27,6 @@ object Day25 : Day {
3L, 4L -> toSnafu(n / 5L + 1L) + fromDigits[m - 5L]
else -> throw Exception("modulus fail for $n")
}

}

@JvmStatic
Expand Down
1 change: 0 additions & 1 deletion advents/src/test/kotlin/net/fish/y2022/Day25Test.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.fish.y2022

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource

Expand Down

0 comments on commit ff5ae63

Please sign in to comment.