From 70ce1e469533bb1179b4b86c390ecae76a4c8b84 Mon Sep 17 00:00:00 2001 From: Sebastian Banert <sebastian.banert@control.lth.se> Date: Wed, 25 Dec 2024 21:11:53 +0100 Subject: [PATCH] Sebastian's day 25. --- day 25/day_25_sebastian.hs | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 day 25/day_25_sebastian.hs diff --git a/day 25/day_25_sebastian.hs b/day 25/day_25_sebastian.hs new file mode 100644 index 0000000..f0075a5 --- /dev/null +++ b/day 25/day_25_sebastian.hs @@ -0,0 +1,46 @@ +module Main where + +data SType = Key | Lock deriving Show + +isKey :: SType -> Bool +isKey Key = True +isKey Lock = False + +isLock :: SType -> Bool +isLock = not . isKey + +main :: IO () +main = interact solve + +solve :: String -> String +solve s = unlines ["Part 1: " ++ part1 s, "Part 2: " ++ part2 s] + +part1 :: String -> String +part1 = show . nCombinations . keysLocks . getSchematics . lines + +part2 :: String -> String +part2 = const "no Part 2" + +getSchematics :: [String] -> [(SType, [Int])] +getSchematics [] = [] +getSchematics ("":r) = getSchematics r +getSchematics ls = (t, p):getSchematics r where + (s, r) = span (/= "") ls + t = if all (== '#') (head s) then Lock else Key + p = foldl (\acc l -> zipWith (+) acc (map toInt l)) (repeat (-1)) s + +keysLocks :: [(SType, [Int])] -> ([[Int]], [[Int]]) +keysLocks l = (keys, locks) where + keys = map snd $ filter (isKey . fst) l + locks = map snd $ filter (isLock . fst) l + +nCombinations :: ([[Int]], [[Int]]) -> Int +nCombinations (keys, locks) = sum $ map (\k -> length (filter (`fits` k) locks)) keys + +toInt :: Char -> Int +toInt '#' = 1 +toInt '.' = 0 +toInt _ = undefined + +fits :: [Int] -> [Int] -> Bool +fits a = all (<= 5) . zipWith (+) a -- GitLab