diff --git a/hs/src/Day25.hs b/hs/src/Day25.hs index bda4c3e..d8eb376 100644 --- a/hs/src/Day25.hs +++ b/hs/src/Day25.hs @@ -14,7 +14,7 @@ day25 = T.reverse . T.unfoldr g . sum . map (T.foldl' f 0) . T.lines where f k '-' = 5 * k - 1 f k c = 5 * k + digitToInt c g 0 = Nothing - g n = Just $ case n `divMod` 5 of - (q, 3) -> ('=', q + 1) - (q, 4) -> ('-', q + 1) - (q, r) -> (intToDigit r, q) + g n = let (q, r) = (n + 2) `divMod` 5 in Just (intToSnafuDigit $ r - 2, q) + intToSnafuDigit (-2) = '=' + intToSnafuDigit (-1) = '-' + intToSnafuDigit d = intToDigit d diff --git a/kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day25.kt b/kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day25.kt index ef7574d..c962141 100644 --- a/kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day25.kt +++ b/kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day25.kt @@ -14,11 +14,8 @@ class Day25(private val lines: List) { } } while (n != 0L) { - n = n.floorDiv(5) + when (val d = n.mod(5)) { - 3 -> 1.also { append('=') } - 4 -> 1.also { append('-') } - else -> 0.also { append(d.digitToChar()) } - } + append("012=-"[n.mod(5)]) + n = (n + 2).floorDiv(5) } reverse() }