day 2
This commit is contained in:
parent
3dd4e18c01
commit
f5f0684acd
|
@ -0,0 +1,23 @@
|
|||
data Direction = Forward | Up | Down
|
||||
deriving (Show, Eq)
|
||||
|
||||
loop :: Integer -> Integer -> [(Direction, Integer)] -> Integer
|
||||
loop horizontal depth [] = horizontal * depth
|
||||
loop horizontal depth ((dir, mag):rest) = case dir of
|
||||
Forward -> loop (horizontal + mag) depth rest
|
||||
Up -> loop horizontal (depth - mag) rest
|
||||
Down -> loop horizontal (depth + mag) rest
|
||||
|
||||
-- helper functions
|
||||
|
||||
stringToDirection :: String -> Direction
|
||||
stringToDirection str = case str of
|
||||
"forward" -> Forward
|
||||
"up" -> Up
|
||||
"down" -> Down
|
||||
|
||||
convert :: [String] -> [(Direction, Integer)]
|
||||
convert [] = []
|
||||
convert (a:b:rest) = (stringToDirection a, read b):convert rest
|
||||
|
||||
main = interact $ show . loop 0 0 . convert . words
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,23 @@
|
|||
data Direction = Forward | Up | Down
|
||||
deriving (Show, Eq)
|
||||
|
||||
loop :: Integer -> Integer -> Integer -> [(Direction, Integer)] -> Integer
|
||||
loop horizontal depth aim [] = horizontal * depth
|
||||
loop horizontal depth aim ((dir, mag):rest) = case dir of
|
||||
Forward -> loop (horizontal + mag) (depth + aim * mag) aim rest
|
||||
Up -> loop horizontal depth (aim - mag) rest
|
||||
Down -> loop horizontal depth (aim + mag) rest
|
||||
|
||||
-- helper functions
|
||||
|
||||
stringToDirection :: String -> Direction
|
||||
stringToDirection str = case str of
|
||||
"forward" -> Forward
|
||||
"up" -> Up
|
||||
"down" -> Down
|
||||
|
||||
convert :: [String] -> [(Direction, Integer)]
|
||||
convert [] = []
|
||||
convert (a:b:rest) = (stringToDirection a, read b):convert rest
|
||||
|
||||
main = interact $ show . loop 0 0 0 . convert . words
|
Loading…
Reference in New Issue