aoc-2024-haskell/day2/Main.hs

61 lines
1.5 KiB
Haskell
Raw Normal View History

2024-12-03 21:08:20 +01:00
module Main where
import System.Environment (getArgs)
2024-12-04 00:04:53 +01:00
import Control.Exception (assert)
2024-12-03 21:08:20 +01:00
import Text.Parsec (parse, many1, char, sepBy, endBy)
import Text.Parsec.Char (endOfLine)
import Text.ParserCombinators.Parsec (GenParser)
import Text.ParserCombinators.Parsec.Number (decimal)
type Report = [Level]
type Level = Int
reports :: GenParser Char st [Report]
reports = endBy report endOfLine
report :: GenParser Char st Report
report = sepBy level (many1 (char ' '))
level :: GenParser Char st Level
level = decimal
pairUp :: [a] -> [(a, a)]
pairUp (a:b:tl) = (a, b) : pairUp (b:tl)
pairUp _ = []
2024-12-04 00:04:53 +01:00
isSafe :: Report -> Bool
isSafe (a:b:tl) = a /= b && (==) 0 (
length $ filter (not . (\(l, r) -> let
dif = abs (l - r)
cmp = if a < b then (<) else assert (b < a) (>)
in (1 <= dif) && (dif <= 3) && l `cmp` r))
$ pairUp
2024-12-04 00:04:53 +01:00
$ a:b:tl)
isSafe _ = True
with1Removed :: Report -> [Report]
with1Removed (h:tl) = tl : map (h :) (with1Removed tl)
with1Removed [] = []
isKindaSafe :: Report -> Bool
isKindaSafe = any isSafe . with1Removed
part1 :: [Report] -> Int
part1 = length . filter isSafe
part2 :: [Report] -> Int
part2 = length . filter isKindaSafe
2024-12-03 21:08:20 +01:00
main :: IO ()
main = do
args <- getArgs
(fp:_) <- return args
content <- readFile fp
case parse reports fp content of
Left e -> putStrLn ("Input file is incorrectly formatted : " ++ show e)
Right r -> do
2024-12-04 00:04:53 +01:00
putStrLn ("Solution (Part 1) : " ++ show (part1 r))
putStrLn ("Solution (Part 2) : " ++ show (part2 r))