From 1e297ced3ea1bd890c0ce1398af773c856ad68f7 Mon Sep 17 00:00:00 2001 From: KoenDR06 Date: Sun, 7 Dec 2025 13:32:19 +0100 Subject: [PATCH] Day 7 --- src/Day2507.hs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/Day2507.hs diff --git a/src/Day2507.hs b/src/Day2507.hs new file mode 100644 index 0000000..ab69d90 --- /dev/null +++ b/src/Day2507.hs @@ -0,0 +1,50 @@ +module Main where + +import Data.List (singleton, elemIndex, nub) +import Data.Maybe (fromJust) + +part1 :: String -> Int +part1 str = go (drop 1 input) 0 beams + where + input = lines str + + beams = [ fromJust $ elemIndex 'S' $ head input ] + + go :: [String] -> Int -> [Int] -> Int + go [] acc _ = acc + go (line:lines) acc beams = go lines (acc + countSplits beams) $ nub $ concatMap splitBeam beams + where + countSplits :: [Int] -> Int + countSplits = length . filter (\beam -> (line !! beam) == '^') + + splitBeam beam | (line !! beam) == '^' = [beam-1, beam+1] + | otherwise = [beam] + + +type Beam = (Int, Int) +part2 :: String -> Int +part2 str = go (drop 1 input) beams + where + input = lines str + + beams = [ (fromJust $ elemIndex 'S' $ head input, 1) ] + + go :: [String] -> [Beam] -> Int + go [] = sum . map snd + go (line:lines) = go lines . mergeNub . concatMap splitBeam + where + splitBeam (index, count) | (line !! index) == '^' = [(index-1, count), (index+1,count)] + | otherwise = [(index, count)] + + mergeNub :: [Beam] -> [Beam] + mergeNub [] = [] + mergeNub ((i,n):xs) = (i,n + merged) : mergeNub (filter (\(i',n') -> i /= i') xs) + where + merged = sum (map snd $ filter (\(i', n') -> i == i') xs) + +main :: IO () +main = do + str <- getContents + + print $ part1 str + print $ part2 str