Compare commits

...

5 Commits

Author SHA1 Message Date
Daniele Fucini
8b4873f9a9 Add Day 6 solutions 2025-12-06 10:56:46 +01:00
Daniele Fucini
d57a39fb48 Fix output for Day 2, Puzzle 2 2025-12-06 10:55:54 +01:00
Daniele Fucini
934a2d437f Add Day 5, Puzzle 2 solution 2025-12-05 12:55:01 +01:00
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
7 changed files with 144 additions and 20 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,5 @@ executable adventofcode2025
Day02 Day02
Day03 Day03
Day04 Day04
Day05
Day06

View File

@@ -1,6 +1,6 @@
module Day02 module Day02
( day02_1, ( day02_1,
day02_2 day02_2,
) )
where where
@@ -9,25 +9,25 @@ import Data.List.Split (splitOn)
sumInvalid :: [Int] -> Int sumInvalid :: [Int] -> Int
sumInvalid = foldl addInvalid 0 sumInvalid = foldl addInvalid 0
where where
addInvalid acc n addInvalid acc n
| odd (length (show n)) = acc | odd (length (show n)) = acc
| let l2 = length (show n) `div` 2, take l2 (show n) == drop l2 (show n) = acc + n | let l2 = length (show n) `div` 2, take l2 (show n) == drop l2 (show n) = acc + n
| otherwise = acc | otherwise = acc
sumInvalid' :: [Int] -> Int sumInvalid' :: [Int] -> Int
sumInvalid' = foldl addInvalid 0 sumInvalid' = foldl addInvalid 0
where addInvalid acc n where
| let s = drop 1 $ show n ++ show n, show n `isInfixOf` take (length s - 1) s = acc + n addInvalid acc n
| otherwise = acc | let s = drop 1 $ show n ++ show n, show n `isInfixOf` take (length s - 1) s = acc + n
| 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)]
parseInput :: IO [Int] parseInput :: IO [Int]
parseInput = do parseInput = do
concatMap getRange . splitOn "," <$> readFile "input/day2.txt" concatMap getRange . splitOn "," <$> readFile "input/day2.txt"
day02_1 :: IO () day02_1 :: IO ()
day02_1 = do day02_1 = do
@@ -35,12 +35,12 @@ day02_1 = do
let result = sumInvalid values let result = sumInvalid values
putStrLn $ putStrLn $
"Day 2, Puzzle 1 solution: " "Day 2, Puzzle 1 solution: "
++ show result ++ show result
day02_2 :: IO () day02_2 :: IO ()
day02_2 = do day02_2 = do
values <- parseInput values <- parseInput
let result = sumInvalid' values let result = sumInvalid' values
putStrLn $ putStrLn $
"Day 2, Puzzle 1 solution: " "Day 2, Puzzle 2 solution: "
++ show result ++ show result

View File

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

53
src/Day05.hs Normal file
View File

@@ -0,0 +1,53 @@
{-# OPTIONS_GHC -Wno-x-partial #-}
module Day05
( day05_1,
day05_2,
)
where
import Data.List (sortOn)
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
countIngredients :: [(Int, Int)] -> Int -> Int
countIngredients [] tot = tot
countIngredients [r] tot = snd r - fst r + 1 + tot
countIngredients (r : s : rs) tot
| snd r >= snd s = countIngredients (r : rs) tot
| snd r >= fst s = countIngredients ((fst r, fst s - 1) : s : rs) tot
| otherwise = countIngredients (s : rs) (snd r - fst r + 1 + tot)
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)
day05_2 :: IO ()
day05_2 = do
input <- parseInput
let ranges = sortOn fst . sortOn snd . map getRange $ takeWhile (/= "") input
result = countIngredients ranges 0
putStrLn $
"Day 5, Puzzle 2 solution: "
++ show result

53
src/Day06.hs Normal file
View File

@@ -0,0 +1,53 @@
module Day06
( day06_1,
day06_2,
)
where
import Data.List (groupBy, transpose)
solveProblem :: [String] -> Int
solveProblem problem =
let op = last problem
values = map read $ init problem
in case op of
"+" -> sum values
"*" -> product values
_ -> 0
solveProblem' :: [String] -> Int
solveProblem' [] = 0
solveProblem' (x : xs) =
let op = last x
v1 = read $ init x
values = v1 : [read v | v <- xs]
in case op of
'+' -> sum values
'*' -> product values
_ -> 0
parseInput :: IO [[String]]
parseInput = do
transpose . map words . lines <$> readFile "input/day6.txt"
parseInput' :: IO [[String]]
parseInput' = do
input <- transpose . lines <$> readFile "input/day6.txt"
let problems = map (filter (not . all (== ' '))) $ groupBy (\_ y -> not (all (== ' ') y)) input
return problems
day06_1 :: IO ()
day06_1 = do
input <- parseInput
let result = sum $ map solveProblem input
putStrLn $
"Day 6, Puzzle 1 solution: "
++ show result
day06_2 :: IO ()
day06_2 = do
input <- parseInput'
let result = sum $ map solveProblem' input
putStrLn $
"Day 6, Puzzle 2 solution: "
++ show result

View File

@@ -4,6 +4,8 @@ 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, day05_2)
import Day06 (day06_1, day06_2)
import System.Environment (getArgs) import System.Environment (getArgs)
main :: IO () main :: IO ()
@@ -30,6 +32,16 @@ main = do
"4" : _ -> do "4" : _ -> do
day04_1 day04_1
day04_2 day04_2
"5" : "1" : _ -> day05_1
"5" : "2" : _ -> day05_2
"5" : _ -> do
day05_1
day05_2
"6" : "1" : _ -> day06_1
"6" : "2" : _ -> day06_2
"6" : _ -> do
day06_1
day06_2
"all" : _ -> do "all" : _ -> do
day01_1 day01_1
day01_2 day01_2
@@ -39,4 +51,8 @@ main = do
day03_2 day03_2
day04_1 day04_1
day04_2 day04_2
day05_1
day05_2
day06_1
day06_2
_ -> error "Not implemented" _ -> error "Not implemented"