-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1-2.hs
60 lines (49 loc) · 2.03 KB
/
day1-2.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import System.IO
import Control.Monad
import Data.Char (isDigit)
import Data.Maybe (maybeToList)
readMaybeSInt :: String -> [Maybe Int]
readMaybeSInt ('o':'n':'e':ss) = [Just 1] <> readMaybeSInt (['n','e'] <> ss)
readMaybeSInt ('t':'w':'o':ss) = [Just 2] <> readMaybeSInt (['w','o'] <> ss)
readMaybeSInt ('t':'h':'r':'e':'e':ss) = [Just 3] <> readMaybeSInt (['h','r','e','e'] <> ss)
readMaybeSInt ('f':'o':'u':'r':ss) = [Just 4] <> readMaybeSInt (['o','u','r'] <> ss)
readMaybeSInt ('f':'i':'v':'e':ss) = [Just 5] <> readMaybeSInt (['i','v','e'] <> ss)
readMaybeSInt ('s':'i':'x':ss) = [Just 6] <> readMaybeSInt (['i','x'] <> ss)
readMaybeSInt ('s':'e':'v':'e':'n':ss) = [Just 7] <> readMaybeSInt (['e','v','e','n'] <> ss)
readMaybeSInt ('e':'i':'g':'h':'t':ss) = [Just 8] <> readMaybeSInt (['i','g','h','t'] <> ss)
readMaybeSInt ('n':'i':'n':'e':ss) = [Just 9] <> readMaybeSInt (['i','n','e'] <> ss)
readMaybeSInt (c:ss) = [readMaybeCInt c] <> readMaybeSInt ss
readMaybeSInt [] = []
readMaybeCInt :: Char -> Maybe Int
readMaybeCInt c = if isDigit c then Just (readCInt c) else Nothing
readCInt :: Char -> Int
readCInt c = read [c]
-- getMaybeInts :: String -> [Maybe Int]
-- getMaybeInts s = map readMaybeInt s
getInts :: [Maybe Int] -> [Int]
getInts [] = []
getInts (x:xs) = case x of
Just i -> [i] <> getInts xs
Nothing -> getInts xs
getFirstAndLastInt :: String -> (Int,Int)
getFirstAndLastInt s = (ints!!0,last(ints))
where ints = getInts(readMaybeSInt s)
showIntPair :: (Int,Int) -> String
showIntPair (a,b) = show a ++ show b
readInt :: String -> Int
readInt s = read s
main = do
handle <- openFile "inputs/day1/input" ReadMode
contents <- hGetContents handle
let list = lines contents
let ints = map readInt(map showIntPair (map getFirstAndLastInt list))
let sumints = sum ints
print (length(list))
print (length(ints))
print (list!!0)
print (ints!!0)
print (list!!273)
print (ints!!273)
print (last(list))
print (last(ints))
print sumints