Compare commits

...

2 Commits

Author SHA1 Message Date
Daniele Fucini
8828c9fe7f Add Day 5, Puzzle 1 solution 2025-12-05 09:07:57 +01:00
Daniele Fucini
7a68416c24 Fix indentation 2025-12-05 09:04:23 +01:00
6 changed files with 58 additions and 18 deletions

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
module Day02 module Day02
( day02_1, ( day02_1,
day02_2 day02_2,
) )
where where
@@ -17,11 +17,11 @@ sumInvalid = foldl addInvalid 0
sumInvalid' :: [Int] -> Int sumInvalid' :: [Int] -> Int
sumInvalid' = foldl addInvalid 0 sumInvalid' = foldl addInvalid 0
where addInvalid acc n where
addInvalid acc n
| let s = drop 1 $ show n ++ show n, show n `isInfixOf` take (length s - 1) s = acc + n | let s = drop 1 $ show n ++ show n, show n `isInfixOf` take (length s - 1) s = acc + n
| otherwise = acc | otherwise = acc
getRange :: String -> [Int] getRange :: String -> [Int]
getRange r = [read (takeWhile (/= '-') r) .. read $ drop 1 (dropWhile (/= '-') r)] getRange r = [read (takeWhile (/= '-') r) .. read $ drop 1 (dropWhile (/= '-') r)]

View File

@@ -1,6 +1,6 @@
module Day03 module Day03
( day03_1, ( day03_1,
day03_2 day03_2,
) )
where where

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 Day02 (day02_1, day02_2)
import Day03 (day03_1, day03_2) import Day03 (day03_1, day03_2)
import Day04 (day04_1, day04_2) import Day04 (day04_1, day04_2)
import Day05 (day05_1)
import System.Environment (getArgs) import System.Environment (getArgs)
main :: IO () main :: IO ()
@@ -30,6 +31,9 @@ main = do
"4" : _ -> do "4" : _ -> do
day04_1 day04_1
day04_2 day04_2
"5" : "1" : _ -> day05_1
"5" : _ -> do
day05_1
"all" : _ -> do "all" : _ -> do
day01_1 day01_1
day01_2 day01_2
@@ -39,4 +43,5 @@ main = do
day03_2 day03_2
day04_1 day04_1
day04_2 day04_2
day05_1
_ -> error "Not implemented" _ -> error "Not implemented"