Add Day 5, Puzzle 1 solution

This commit is contained in:
Daniele Fucini
2025-12-05 09:07:57 +01:00
parent 7a68416c24
commit 8828c9fe7f
4 changed files with 41 additions and 1 deletions

View File

@@ -6,5 +6,5 @@
|02 |★ ★ |08 | |
|03 |★ ★ |09 | |
|04 |★ ★ |10 | |
|05 | |11 | |
|05 | |11 | |
|06 | |12 | |

View File

@@ -34,3 +34,4 @@ executable adventofcode2025
Day02
Day03
Day04
Day05

34
src/Day05.hs Normal file
View File

@@ -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)

View File

@@ -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"