Implement day 1+2
This commit is contained in:
commit
27c17c492e
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*.hi
|
||||
*.o
|
20
1.hs
Normal file
20
1.hs
Normal file
@ -0,0 +1,20 @@
|
||||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
|
||||
main = do
|
||||
lines <- readFile "1.in"
|
||||
putStrLn . show . solve . map (read :: String -> Integer) . words $ lines
|
||||
where
|
||||
solve = partB
|
||||
|
||||
-- solves all lines, returns a string
|
||||
-- solve :: [string] -> a
|
||||
partA = sum . map (boolToInt . cmpTuple) . double
|
||||
where
|
||||
double = zip <*> tail
|
||||
cmpTuple = uncurry (<)
|
||||
boolToInt = fromEnum
|
||||
|
||||
partB = partA . sumTuple3 . triple
|
||||
where
|
||||
triple x = zip3 x (tail x) (tail $ tail x)
|
||||
sumTuple3 = map (\(x, y, z) -> x + y + z)
|
30
2.hs
Normal file
30
2.hs
Normal file
@ -0,0 +1,30 @@
|
||||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
|
||||
main = do
|
||||
lines <- readFile "2.in"
|
||||
putStrLn . show . solve . parseInput . words $ lines
|
||||
where
|
||||
solve = partB
|
||||
parseInput :: [ String ] -> [ (String, Integer) ]
|
||||
parseInput [] = []
|
||||
parseInput [x] = []
|
||||
parseInput (x:y:r) = (x, read y) : parseInput r
|
||||
|
||||
-- solves all lines, returns a string
|
||||
partA :: [(String, Integer)] -> Integer
|
||||
partA = (uncurry (*)) . foldl parseCommand (0,0)
|
||||
where
|
||||
-- (horizontal, vertical)
|
||||
parseCommand (a, b) ("forward", x) = (a + x, b)
|
||||
parseCommand (a, b) ("down", x) = (a, b + x)
|
||||
parseCommand (a, b) ("up", x) = (a, b - x)
|
||||
parseCommand _ _ = (0, 0)
|
||||
|
||||
partB :: [(String, Integer)] -> Integer
|
||||
partB = (\(x,y,_) -> x * y) . foldl parseCommand (0, 0, 0)
|
||||
where
|
||||
-- (horiz, vert, aim)
|
||||
parseCommand (a, b, c) ("forward", x) = (a + x, b + c * x, c)
|
||||
parseCommand (a, b, c) ("down", x) = (a, b, c + x)
|
||||
parseCommand (a, b, c) ("up", x) = (a, b, c - x)
|
||||
parseCommand _ _ = (0, 0, 0)
|
Loading…
Reference in New Issue
Block a user