From 51df7adc0d5bce1d0d604640b75dfdb59c7d4961 Mon Sep 17 00:00:00 2001 From: toastyandwarm Date: Wed, 25 Dec 2024 17:15:40 +0000 Subject: [PATCH] day 25 --- 25/solution.hs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 25/solution.hs diff --git a/25/solution.hs b/25/solution.hs new file mode 100644 index 0000000..9c5a01d --- /dev/null +++ b/25/solution.hs @@ -0,0 +1,45 @@ +import Data.List +import Data.Maybe +import qualified Data.List.Split as S +import Debug.Trace + +traceOut :: Show a => a -> a +traceOut x = (traceShow x x) + +addFirst :: [[a]] -> a -> [[a]] +addFirst [] x = [[x]] +addFirst (y:ys) x = ([x] ++ y) : ys + +split :: Eq a => [a] -> [a] -> [[a]] +split _ [] = [] +split delims (x:xs) + | elem x delims = [[]] ++ split delims xs + | otherwise = addFirst (split delims xs) x + +data Schematic = Key [Int] | Lock [Int] deriving (Eq, Show) + +splitInput :: [String] -> [Schematic] +splitInput = map $ (\x -> case x of + ((y:_):_) -> (if y == '#' then Lock else Key) $ map (length . filter (=='#')) x + _ -> Lock [] + ) . transpose . split "\n" + +matchKeys :: Schematic -> Schematic -> Bool +matchKeys a b = case (a, b) of + (Key _, Key _) -> False + (Lock _, Lock _) -> False + (Lock x, Key y) -> all (<=7) $ zipWith (+) x y + (Key x, Lock y) -> all (<=7) $ zipWith (+) x y + +solve1 :: [Schematic] -> Int +solve1 xs = (`div` 2) $ sum $ map sum $ map (\x -> map (\y -> if matchKeys x y then 1 else 0) xs) xs + +main :: IO() +main = do + fileinp <- readFile "input.txt" + let parsed = splitInput $ S.splitOn "\n\n" fileinp + print parsed + let solved1 = solve1 parsed +-- let solved2 = solve2 parsed + print solved1 +-- print solved2