-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_1.hs
33 lines (23 loc) · 819 Bytes
/
2_1.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
values = map getValue splitted
collected = foldr (zipWith (+)) [0,0] values
multiplied = product collected
print collected
print multiplied
return ()
splitIntoDirectionAndValue :: String -> ([Char], Int)
splitIntoDirectionAndValue a =
let splitted = words a
direction = head splitted
value = strToInt(last splitted)
in (direction, value)
getValue :: ([Char], Int) -> [Int]
getValue (direction, value)
| direction == "forward" = [0, value]
| direction == "up" = [-value, 0]
| direction == "down" = [value, 0]
| otherwise = error ("Unknown direction " ++ direction)
strToInt str = read str :: Int