Advertisement
komikat1

aoc day1-2

Dec 23rd, 2023
175
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 1.14 KB | Source Code | 0 0
  1. {-# LANGUAGE OverloadedStrings #-}
  2. import Data.Char
  3. import qualified Data.Text as T
  4.  
  5.  
  6. first string
  7.   | isDigit (T.head string) = digitToInt (T.head string)
  8.   | "one" `T.isPrefixOf` string = 1
  9.   | "two" `T.isPrefixOf` string = 2
  10.   | "three" `T.isPrefixOf` string = 3
  11.   | "four" `T.isPrefixOf` string = 4
  12.   | "five" `T.isPrefixOf` string = 5
  13.   | "six" `T.isPrefixOf` string = 6
  14.   | "seven" `T.isPrefixOf` string = 7
  15.   | "eight" `T.isPrefixOf` string = 8
  16.   | "nine" `T.isPrefixOf` string = 9
  17.   | otherwise = first (T.tail string)
  18.  
  19. last1 string
  20.   | isDigit (T.head string) = digitToInt (T.head string)
  21.   | "eno" `T.isPrefixOf` string = 1
  22.   | "owt" `T.isPrefixOf` string = 2
  23.   | "eerht" `T.isPrefixOf` string = 3
  24.   | "ruof" `T.isPrefixOf` string = 4
  25.   | "evif" `T.isPrefixOf` string = 5
  26.   | "xis" `T.isPrefixOf` string = 6
  27.   | "neves" `T.isPrefixOf` string = 7
  28.   | "thgie" `T.isPrefixOf` string = 8
  29.   | "enin" `T.isPrefixOf` string = 9
  30.   | otherwise = last1 (T.tail string)
  31.  
  32. getResult string = 10 * (first (T.pack (string))) + last1 (T.reverse (T.pack string))
  33.  
  34. main = do
  35.   str <- getContents  
  36.   print (sum(map getResult (lines str)))
  37.  
Tags: Haskell
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement