Implement day 1+2

This commit is contained in:
Dominic Zimmer 2021-12-02 13:47:03 +01:00
commit 27c17c492e
6 changed files with 3055 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.hi
*.o

20
1.hs Normal file
View 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)

2000
1.in Normal file

File diff suppressed because it is too large Load Diff

30
2.hs Normal file
View 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)

1000
2.in Normal file

File diff suppressed because it is too large Load Diff

3
Makefile Normal file
View File

@ -0,0 +1,3 @@
target:
ghc 1.hs
ghc 2.hs