-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.hs
42 lines (35 loc) · 986 Bytes
/
run.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeApplications #-}
readDigit :: Num n => Char -> n
readDigit = \case '2' -> 2
'1' -> 1
'0' -> 0
'-' -> -1
'=' -> -2
readSnafu :: Num n => String -> n
readSnafu =
sum
. map (\(i, v) -> v * 5 ^ i)
. zip @Int [0..]
. map readDigit
. reverse
decToSnafu :: Integral n => n -> String
decToSnafu = reverse . go 0
where go carry n =
case (carry + n) `divMod` 5 of
(0, 0) -> []
(rest, 0) -> '0':go 0 rest
(rest, 1) -> '1':go 0 rest
(rest, 2) -> '2':go 0 rest
(rest, 3) -> '=':go 1 rest
(rest, 4) -> '-':go 1 rest
parseAll :: Num n => String -> [n]
parseAll = map readSnafu . lines
part1 :: [Int] -> String
part1 = decToSnafu . sum
main :: IO ()
main = main' "input.txt"
main' :: FilePath -> IO ()
main' file = do
input <- parseAll <$> readFile file
putStrLn (part1 input)