-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_2.hs
33 lines (23 loc) · 952 Bytes
/
2_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
main = do
contents <- getContents
let contentList = lines contents
let splitted = map splitIntoDirectionAndValue contentList
collected = foldl getValue (0, 0, 0) splitted
multiplied = computeResult collected
print collected
print multiplied
return ()
computeResult (a,b,c) = b * c
splitIntoDirectionAndValue :: String -> (String, Int)
splitIntoDirectionAndValue a =
let splitted = words a
direction = head splitted
value = strToInt(last splitted)
in (direction, value)
getValue :: (Int, Int, Int) -> (String, Int) -> (Int, Int, Int)
getValue (aim, horizontal, depth) (direction, value)
| direction == "down" = (aim + value, horizontal, depth)
| direction == "up" = (aim - value, horizontal, depth)
| direction == "forward" = (aim, horizontal + value, depth + aim * value)
| otherwise = error ("Unknown direction " ++ direction)
strToInt str = read str :: Int