From bc08c78977337ad3dffc5cf4110e4967eab8ce15 Mon Sep 17 00:00:00 2001 From: toastyandwarm Date: Mon, 2 Dec 2024 10:21:49 +0000 Subject: [PATCH] day 2 --- 2/solution.hs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 2/solution.hs diff --git a/2/solution.hs b/2/solution.hs new file mode 100644 index 0000000..2be9d7d --- /dev/null +++ b/2/solution.hs @@ -0,0 +1,47 @@ +import Data.List +import Debug.Trace + +addFirst :: [[a]] -> a -> [[a]] +addFirst [] x = [[x]] +addFirst (y:ys) x = ([x] ++ y) : ys + +split :: [Char] -> [Char] -> [[Char]] +split _ [] = [] +split delims (x:xs) + | elem x delims = [[]] ++ split delims xs + | otherwise = addFirst (split delims xs) x + +splitInput :: String -> [[Int]] +splitInput text = map (map read) $ map (filter (/="")) $ map (split [' ']) $ split ['\n'] text + +solve1 :: [[Int]] -> Int +solve1 x = length $ filter (\x -> ((decreasing x) || (increasing x)) && (differences x)) x + +solve2 :: [[Int]] -> Int +solve2 x = length $ filter (foldl (||) False) $ map (map (\x -> ((decreasing x) || (increasing x)) && (differences x))) $ map modifiedSets x + +modifiedSets :: [Int] -> [[Int]] +modifiedSets x = map (removeElem x) ([0..length x]) + +removeElem :: [Int] -> Int -> [Int] +removeElem [] _ = [] +removeElem (x:xs) 0 = xs +removeElem (x:xs) n = x : removeElem xs (n-1) + +increasing :: [Int] -> Bool +increasing x = (\(x, y) -> x) $ foldl' (\(x, y) z -> (x && (y < z), z)) (True, 0) x + +decreasing :: [Int] -> Bool +decreasing x = (\(x, y) -> x) $ foldr (\z (x, y) -> (x && (y < z), z)) (True, 0) x + +differences :: [Int] -> Bool +differences x = (\(x, y) -> x) $ foldl (\(x, y) z -> (x && (0 < abs(y-z) && abs(y-z) < 4), z)) (True, head x-2) x + +main :: IO() +main = do + fileinp <- readFile "input.txt" + let parsed = splitInput fileinp + let solved1 = solve1 parsed + let solved2 = solve2 parsed + print solved1 + print solved2