Skip to content

Commit

Permalink
Golf :)
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 25, 2022
1 parent 9a0c901 commit 964dcbf
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions hs/src/Day25.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Module: Day25
Description: <https://adventofcode.com/2022/day/25 Day 25: Full of Hot Air>
-}
{-# LANGUAGE MultiWayIf #-}
module Day25 (day25) where

import Data.Char (digitToInt, intToDigit)
Expand All @@ -10,11 +11,8 @@ import qualified Data.Text as T (foldl', lines, unfoldr, reverse)

day25 :: Text -> Text
day25 = T.reverse . T.unfoldr g . sum . map (T.foldl' f 0) . T.lines where
f k '=' = 5 * k - 2
f k '-' = 5 * k - 1
f k c = 5 * k + digitToInt c
f k c = 5 * k + if | '=' <- c -> -2 | '-' <- c -> -1 | otherwise -> digitToInt c
g 0 = Nothing
g n = let (q, r) = (n + 2) `divMod` 5 in Just (intToSnafuDigit $ r - 2, q)
intToSnafuDigit (-2) = '='
intToSnafuDigit (-1) = '-'
intToSnafuDigit d = intToDigit d
g n = Just (c, q) where
(q, r) = (n + 2) `divMod` 5
c | 0 <- r = '=' | 1 <- r = '-' | otherwise = intToDigit $ r - 2

0 comments on commit 964dcbf

Please sign in to comment.