This commit is contained in:
opfez 2021-12-02 07:46:27 +01:00
parent 3dd4e18c01
commit f5f0684acd
3 changed files with 1046 additions and 0 deletions

23
day2/first.hs Normal file
View File

@ -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

1000
day2/input Normal file

File diff suppressed because it is too large Load Diff

23
day2/second.hs Normal file
View File

@ -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