summaryrefslogtreecommitdiff
path: root/2023/day01/Main.hs
blob: 5f3158231900b3b82d6272cbdc67caf1501a3bd4 (plain)
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
import Data.List
import Data.Char

-- isDigit :: Char -> Bool
-- isDigit c = c >= '0' && c <= '9'

wordDict = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

findFirst :: String -> [String] -> Int
findFirst [] _ = 0
findFirst s@(x:xs) wordDict
  | isDigit x = read [x]
  | Just y <- elemIndex (take 3 s) wordDict = y
  | Just y <- elemIndex (take 4 s) wordDict = y
  | Just y <- elemIndex (take 5 s) wordDict = y
  | otherwise = findFirst xs wordDict

rev [] = []
rev (x:xs) = rev xs ++ [x]

main :: IO ()
main = do
  inputLines <- lines <$> getContents

  let filterLines = map (filter isDigit) inputLines
  let outerChars = map (\x -> [head x, last x]) filterLines
  mapM_ putStrLn outerChars

  let intList = map read outerChars :: [Int]
  -- let intList = map (\x -> findFirst x wordDict * 10 + findFirst (rev x) (map rev wordDict)) inputLines
  let result = sum intList

  putStrLn $ show result