diff --git a/README.md b/README.md index 2992317..8044e17 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,5 @@ |02 |★ ★ |08 | | |03 |★ ★ |09 | | |04 |★ ★ |10 | | -|05 | |11 | | +|05 |★ |11 | | |06 | |12 | | diff --git a/adventofcode2025.cabal b/adventofcode2025.cabal index 6b87b78..0d92603 100644 --- a/adventofcode2025.cabal +++ b/adventofcode2025.cabal @@ -34,3 +34,4 @@ executable adventofcode2025 Day02 Day03 Day04 + Day05 diff --git a/src/Day05.hs b/src/Day05.hs new file mode 100644 index 0000000..d51539e --- /dev/null +++ b/src/Day05.hs @@ -0,0 +1,34 @@ +{-# OPTIONS_GHC -Wno-x-partial #-} + +module Day05 + ( day05_1, + ) +where + +import Data.List.Split (splitOn) + +getRange :: String -> (Int, Int) +getRange [] = (0, 0) +getRange r = + let x = read . head $ splitOn "-" r + y = read . last $ splitOn "-" r + in (x, y) + +isFresh :: Int -> [(Int, Int)] -> Bool +isFresh _ [] = False +isFresh ingredient (r : rs) + | ingredient >= fst r && ingredient <= snd r = True + | otherwise = isFresh ingredient rs + +parseInput :: IO [String] +parseInput = do + lines <$> readFile "input/day5.txt" + +day05_1 :: IO () +day05_1 = do + input <- parseInput + let ranges = map getRange $ takeWhile (/= "") input + freshIngredients = filter (`isFresh` ranges) . map read . drop 1 $ dropWhile (/= "") input :: [Int] + putStrLn $ + "Day 5, Puzzle 1 solution: " + ++ show (length freshIngredients) diff --git a/src/Main.hs b/src/Main.hs index 4badcf1..47e33a8 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -4,6 +4,7 @@ import Day01 (day01_1, day01_2) import Day02 (day02_1, day02_2) import Day03 (day03_1, day03_2) import Day04 (day04_1, day04_2) +import Day05 (day05_1) import System.Environment (getArgs) main :: IO () @@ -30,6 +31,9 @@ main = do "4" : _ -> do day04_1 day04_2 + "5" : "1" : _ -> day05_1 + "5" : _ -> do + day05_1 "all" : _ -> do day01_1 day01_2 @@ -39,4 +43,5 @@ main = do day03_2 day04_1 day04_2 + day05_1 _ -> error "Not implemented"