-
Notifications
You must be signed in to change notification settings - Fork 0
/
2b.hs
33 lines (24 loc) · 796 Bytes
/
2b.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
stringLToInt :: [String] -> [Int]
stringLToInt = map read
tuplify :: String -> (String, Int)
tuplify s =
let w = words s
in (head w, read $ last w)
parseDir :: (String, Int) -> Int -> (Int, Int, Int)
parseDir ("forward", v) aim = (v, v*aim, aim)
parseDir ("down", v) aim = (0, 0, aim+v)
parseDir ("up", v) aim = (0, 0, aim-v)
addParse :: (Int, Int, Int) -> (String, Int) -> (Int, Int, Int)
addParse (x, y, aim) t =
let (pa, pb, pc) = parseDir t aim
in (x + pa, y + pb, pc)
finalPos :: [(String, Int)] -> (Int, Int, Int)
finalPos = foldl addParse (0, 0, 0)
main :: IO()
main = do
content <- getContents
let strList = lines content
tuples = map tuplify strList
(fpa, fpb, fpaim) = finalPos tuples
fprod = fpa * fpb
putStrLn $ show fprod