-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_2.hs
48 lines (41 loc) · 1.73 KB
/
day_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
module Day2 where
import System.IO ( openFile, hGetContents, IOMode(ReadMode) )
data Command = Forward Int | Down Int | Up Int | Unknown deriving(Show)
solvePart1 :: [Command] -> Int
solvePart1 commands = solve commands (0, 0)
where
solve :: [Command] -> (Int, Int) -> Int
solve (Forward n:xs) (hp, d) = solve xs (hp + n, d)
solve (Down n:xs) (hp, d) = solve xs (hp, d+n)
solve (Up n:xs) (hp, d) = solve xs (hp, d-n)
solve (Unknown:xs) (hp, d) = solve xs (hp, d)
solve [] (hp, d) = hp * d
solvePart2 :: [Command] -> Int
solvePart2 commands = solve commands (0, 0, 0)
where
solve :: [Command] -> (Int, Int, Int) -> Int
solve (Forward n:xs) (hp, d, aim) = solve xs (hp + n, d + (aim * n), aim)
solve (Down n:xs) (hp, d, aim) = solve xs (hp, d, aim+n)
solve (Up n:xs) (hp, d, aim) = solve xs (hp, d, aim-n)
solve (Unknown:xs) (hp, d, aim) = solve xs (hp, d, aim)
solve [] (hp, d, _) = hp * d
parseFile :: String -> [Command]
parseFile str = parseWords $ words str
where
parseWords :: [String] -> [Command]
parseWords [] = []
parseWords (c:n:xs) = parseCommand c n : parseWords xs
parseWords _ = []
parseCommand :: String -> String -> Command
parseCommand command num
| command == "forward" = Forward (read num :: Int)
| command == "down" = Down (read num :: Int)
| command == "up" = Up (read num :: Int)
| otherwise = Unknown
main :: IO()
main = do
handle <- openFile "day_2.txt" ReadMode
content <- hGetContents handle
let commands = parseFile content
print(solvePart1 commands)
print(solvePart2 commands)