Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 25, 2022
1 parent 3705cc9 commit 2e78c86
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
8 changes: 4 additions & 4 deletions hs/src/Day25.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ class Day25(private val lines: List<String>) {
}
}
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()
}
Expand Down

0 comments on commit 2e78c86

Please sign in to comment.