advent-of-code/2024/day02/day02.hs

25 lines
720 B
Haskell

{-# OPTIONS_GHC -Wno-x-partial #-}
import Aoc
parseFile :: [String] -> [[Int]]
parseFile = map (map read . words)
part1 :: [[Int]] -> Int
part1 reports =
length $ filter isSafe $ map differences reports
where
differences xs = zipWith (-) xs (tail xs)
isGradual dx = 1 <= abs dx && abs dx <= 3
isSafe dxs = all isGradual dxs && (all (<0) dxs || all (>0) dxs)
part2 :: [[Int]] -> Int
part2 reports =
length $ filter isSafe $ map remove1 reports
where
remove n xs = let (front, back) = splitAt n xs in front ++ tail back
remove1 report = map (`remove` report) [0..length report-1]
isSafe = (>0) . part1
main :: IO ()
main = aocMain parseFile part1 part2