feat(day2): Fuck it we ball

This commit is contained in:
kale 2024-12-04 00:04:53 +01:00
parent 45c9a200e2
commit 71c6797d81
Signed by: kalmenn
GPG key ID: F500055C44BC3834

View file

@ -2,6 +2,8 @@ module Main where
import System.Environment (getArgs)
import Control.Exception (assert)
import Text.Parsec (parse, many1, char, sepBy, endBy)
import Text.Parsec.Char (endOfLine)
import Text.ParserCombinators.Parsec (GenParser)
@ -23,28 +25,28 @@ pairUp :: [a] -> [(a, a)]
pairUp (a:b:tl) = (a, b) : pairUp (b:tl)
pairUp _ = []
allIncOrDec :: Bool -> Report -> Bool
allIncOrDec dampened (a:b:tl) = if a /= b
then (if dampened then (>=) 0 else (==) 0)
$ length
$ filter (not . uncurry (if a < b then (<) else (>)))
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
$ b:tl
else dampened && allIncOrDec False (b:tl)
allIncOrDec _ _ = True
$ a:b:tl)
isSafe _ = True
notTooDifferent :: Bool -> Report -> Bool
notTooDifferent dampened (a:b:tl) =
let dif = abs (a - b)
in if (1 <= dif) && (dif <= 3)
then notTooDifferent dampened (b:tl)
else dampened && notTooDifferent False (b:tl)
notTooDifferent _ _ = True
with1Removed :: Report -> [Report]
with1Removed (h:tl) = tl : map (h :) (with1Removed tl)
with1Removed [] = []
solve :: Bool -> [Report] -> Int
solve dampened = length
. filter (allIncOrDec dampened)
. filter (notTooDifferent dampened)
isKindaSafe :: Report -> Bool
isKindaSafe = any isSafe . with1Removed
part1 :: [Report] -> Int
part1 = length . filter isSafe
part2 :: [Report] -> Int
part2 = length . filter isKindaSafe
main :: IO ()
main = do
@ -54,5 +56,5 @@ main = do
case parse reports fp content of
Left e -> putStrLn ("Input file is incorrectly formatted : " ++ show e)
Right r -> do
putStrLn ("Solution (Part 1) : " ++ show (solve False r))
putStrLn ("Solution (Part 2) : " ++ show (solve True r))
putStrLn ("Solution (Part 1) : " ++ show (part1 r))
putStrLn ("Solution (Part 2) : " ++ show (part2 r))