From 964dcbf0205772ae2a5e0f3ff711de0156aa59bb Mon Sep 17 00:00:00 2001 From: Daniel Lin Date: Sun, 25 Dec 2022 08:10:29 -0500 Subject: [PATCH] Golf :) --- hs/src/Day25.hs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/hs/src/Day25.hs b/hs/src/Day25.hs index d8eb376..eaff95b 100644 --- a/hs/src/Day25.hs +++ b/hs/src/Day25.hs @@ -2,6 +2,7 @@ Module: Day25 Description: -} +{-# LANGUAGE MultiWayIf #-} module Day25 (day25) where import Data.Char (digitToInt, intToDigit) @@ -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