diff --git a/adventofcode2025.cabal b/adventofcode2025.cabal index 710ba8e..eb5756f 100644 --- a/adventofcode2025.cabal +++ b/adventofcode2025.cabal @@ -19,6 +19,7 @@ executable adventofcode2025 main-is: Main.hs default-language: Haskell2010 build-depends: base >= 4.7 && < 5 + , mtl , split ghc-options: -Wall -Wcompat @@ -36,3 +37,4 @@ executable adventofcode2025 Day04 Day05 Day06 + Day07 diff --git a/src/Day07.hs b/src/Day07.hs new file mode 100644 index 0000000..c6f7dc9 --- /dev/null +++ b/src/Day07.hs @@ -0,0 +1,66 @@ +{-# OPTIONS_GHC -Wno-x-partial #-} + +module Day07 + ( day07_1, + ) +where + +import Control.Monad.State +import Data.List (elemIndex) +import Data.Maybe (fromJust, isJust) + +type Coord = (Int, Int) + +type Grid = [String] + +parseInput :: IO [String] +parseInput = do + lines <$> readFile "input/day7.txt" + +getStart :: Grid -> Int -> Coord +getStart [] _ = (-1, -1) +getStart (g : grid) row + | isJust start = (row, fromJust start) + | otherwise = getStart grid (row + 1) + where + start = elemIndex 'S' g + +isInside :: Coord -> Grid -> Bool +isInside coord grid = + let w = length $ head grid + h = length grid + in fst coord >= 0 && fst coord < h && snd coord >= 0 && snd coord < w + +mark :: Coord -> State Grid () +mark coord = state $ \grid -> + ( (), + take (fst coord) grid ++ [take (snd coord) (grid !! fst coord) ++ ['x'] ++ drop (snd coord + 1) (grid !! fst coord)] ++ drop (fst coord + 1) grid + ) + +beamSplitRecursive :: Coord -> State Grid () +beamSplitRecursive start = do + grid <- get + if not $ isInside start grid + then return () + else + if grid !! fst start !! snd start == 'S' || grid !! fst start !! snd start == '.' + then beamSplitRecursive (fst start + 1, snd start) + else if grid !! fst start !! snd start == 'x' then return () + else do + mark start + beamSplitRecursive (fst start, snd start - 1) + beamSplitRecursive (fst start, snd start + 1) + +beamSplit :: State Grid () +beamSplit = do + grid <- get + let start = getStart grid 0 + beamSplitRecursive start + +day07_1 :: IO () +day07_1 = do + input <- parseInput + let grid = execState beamSplit input + putStrLn $ + "Day 7, Puzzle 1 solution: " + ++ show (length $ concatMap (filter (== 'x')) grid) diff --git a/src/Main.hs b/src/Main.hs index 09ad909..ae2b0df 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -6,6 +6,7 @@ import Day03 (day03_1, day03_2) import Day04 (day04_1, day04_2) import Day05 (day05_1, day05_2) import Day06 (day06_1, day06_2) +import Day07 (day07_1) import System.Environment (getArgs) main :: IO () @@ -42,6 +43,9 @@ main = do "6" : _ -> do day06_1 day06_2 + "7" : "1" : _ -> day07_1 + "7" : _ -> do + day07_1 "all" : _ -> do day01_1 day01_2 @@ -55,4 +59,5 @@ main = do day05_2 day06_1 day06_2 + day07_1 _ -> error "Not implemented"