-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2a.hs
41 lines (30 loc) · 972 Bytes
/
2a.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
{-
Advent of Code
Mark Hegreberg
2a - calculate final position from submarine headings
-}
import Data.Char
sampleData = [("forward", 5), ("down", 5), ("forward", 8), ("up", 3), ("down", 8), ("forward", 2)]
readH a = init $ init a
readM a = [last a]
main = do
contents <- readFile "./2.input"
let input = lines contents
inputH = map readH input
inputM = map readM input
inputM' = map (read::String->Int) inputM
input' = zip inputH inputM'
print $ solution input'
solution :: Num a => [(String, a)] -> a
solution a = horSum a * vertSum a
horSum :: (Num a) => [(String, a)] -> a
horSum [] = 0
horSum x
| fst (head x) == "forward"= snd (head x) + horSum (tail x)
| otherwise = horSum (tail x)
vertSum :: (Num a) => [(String, a)] -> a
vertSum [] = 0
vertSum y
| fst (head y) == "up" = vertSum (tail y) - snd (head y)
| fst (head y) == "down" = vertSum (tail y) + snd (head y)
| otherwise = vertSum (tail y)