aoc/app/2.hs

26 lines
644 B
Haskell
Raw Normal View History

2024-12-02 07:33:58 +00:00
{-# LANGUAGE LambdaCase, MultiWayIf #-}
module Main where
import Data.Functor
parse :: String -> [[Int]]
parse = ((read <$>) . words <$>) . lines
solve1 :: [[Int]] -> Int
solve1 =
let di a = all (\a' -> a' < 4 && not (a' <= 0)) a ||
all (\a' -> a' > -4 && not (a' >= 0)) a in
sum . ((\a -> if di $ zipWith (flip (-)) a (tail a) then 1 else 0) <$>)
solve2 :: [[Int]] -> Int
solve2 =
let r [] = [[]]
2024-12-02 07:36:57 +00:00
r (r':rs) = rs:((r':) <$> r rs) in
sum . ((\a -> if a > 0 then 1 else 0) . solve1 .
2024-12-02 07:33:58 +00:00
r <$>)
main :: IO ()
main = readFile "inputs/2" <&> parse >>= \i ->
print (solve1 i) >>
print (solve2 i)