Optimised day 19
[advent-of-code-22.git] / advent25 / Main.hs
index cc0a464c9fbd83ec28652aaedab4c7fe306ce08f..c58c47dc015da9bcd9e060d6eb1d885a2882c1db 100644 (file)
@@ -10,7 +10,6 @@ main =
       numStrs <- readFile dataFileName
       let fuels = fmap readSnafu $ lines numStrs
       putStrLn $ showSnafu $ sum fuels
-      -- print $ part1 fuels
 
 readSnafu :: String -> Int
 readSnafu cs = foldl' go 0 cs
@@ -33,7 +32,10 @@ toBase5R n = (r : (toBase5R k))
   where (k, r) = n `divMod` 5
 
 packSnafu :: [Int] -> String
-packSnafu = snd . foldl' packSnafuDigit (0, "")
+packSnafu digits
+  | carry == 0 = shown
+  | otherwise = (snafuRep carry) : shown
+  where (carry, shown) = foldl' packSnafuDigit (0, "") digits
 
 packSnafuDigit :: (Int, String) -> Int -> (Int, String)
 packSnafuDigit (carry, acc) d 
@@ -41,7 +43,6 @@ packSnafuDigit (carry, acc) d
   | otherwise = (1, (snafuRep (d' - 5) : acc))
   where d' = d + carry
 
-
 snafuRep :: Int -> Char
 snafuRep 2 = '2'
 snafuRep 1 = '1'