This commit is contained in:
Gattix 2021-12-09 23:43:50 +01:00
parent 2564dad444
commit daf53550fd
2 changed files with 11 additions and 9 deletions

@ -2,8 +2,8 @@ main :: IO ()
main = do
input <- map read . lines <$> readFile "input"
print $ day1 input
print $ day1 $ zipWith3 (\x y z->x+y+z) input (tail input) (tail $ tail input)
print $
day1 $ zipWith3 (\x y z -> x + y + z) input (tail input) (tail $ tail input)
day1 :: [Int] -> Int
day1 a = sum $ map fromEnum $ zipWith (<) a (tail a)

@ -2,12 +2,14 @@ import Control.Arrow
main :: IO ()
main = do
input <- map (head . words &&& read . flip (:) [] . last) <$> lines <$> readFile "input"
print $ (\(x,y,z,d2) -> (x*y,x*d2)) $ day2 input (0,0,0,0)
input <-
map (head . words &&& read . flip (:) [] . last) <$> lines <$>
readFile "input"
print $ (\(x, y, _, d2) -> (x * y, x * d2)) $ day2 input (0, 0, 0, 0)
day2 :: [(String,Int)] -> (Int,Int,Int,Int) -> (Int,Int,Int,Int)
day2 ((d,l):xs) (x,y,z,d2)
| d=="forward" = day2 xs (x+l,y,z,d2+l*z)
| d=="up" = day2 xs (x,y-l,z-l,d2)
| d=="down" = day2 xs (x,y+l,z+l,d2)
day2 :: [(String, Int)] -> (Int, Int, Int, Int) -> (Int, Int, Int, Int)
day2 ((d, l):xs) (x, y, z, d2)
| d == "forward" = day2 xs (x + l, y, z, d2 + l * z)
| d == "up" = day2 xs (x, y - l, z - l, d2)
| d == "down" = day2 xs (x, y + l, z + l, d2)
day2 [] x = x