diff options
author | tzlil <tzlils@protonmail.com> | 2023-12-02 13:57:02 +0200 |
---|---|---|
committer | tzlil <tzlils@protonmail.com> | 2023-12-02 13:57:02 +0200 |
commit | 1552251e0ecf7915f682464eb3e71974ec41633d (patch) | |
tree | c3a26a68faf29970d397bd5e3e64da6262087590 /1/2.hs |
solved day2
Diffstat (limited to '1/2.hs')
-rw-r--r-- | 1/2.hs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/1/2.hs b/1/2.hs new file mode 100644 index 0000000..dd932d5 --- /dev/null +++ b/1/2.hs @@ -0,0 +1,19 @@ +import Prelude +import Text.Read +import Data.Maybe +import Data.List +import Data.Monoid +import System.Environment + +digits = ["one","two","three","four","five","six","seven","eight","nine"] + +parse [] = (mempty,mempty) +parse l@(x:xs) = case findIndex (`isPrefixOf` l) digits of + Just i -> f (Just $ i+1) <> parse xs + Nothing -> f (readMaybe [x]) <> parse xs + where + f = (,) . First <*> Last + +add (First x, Last y) = (+) . (*10) <$> x <*> y + +main = head <$> getArgs >>= readFile >>= print . sum . catMaybes . map (add . parse) <$> lines |