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

23 lines
696 B
Haskell
Raw Normal View History

2024-12-02 18:55:47 +11:00
import Aoc ( aocMain )
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