diff --git a/README.md b/README.md index 9a1e159..eeab69a 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ |Day|Stars|Day|Stars| |---|-----|---|-----| |01 |★ ★ |07 | | -|02 | |08 | | +|02 |★ |08 | | |03 | |09 | | |04 | |10 | | |05 | |11 | | diff --git a/adventofcode2025.cabal b/adventofcode2025.cabal index d01961a..77c3e26 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 + , split ghc-options: -Wall -Wcompat -Widentities @@ -30,3 +31,4 @@ executable adventofcode2025 -Wredundant-constraints other-modules: Day01 + Day02 diff --git a/src/Day02.hs b/src/Day02.hs new file mode 100644 index 0000000..862d761 --- /dev/null +++ b/src/Day02.hs @@ -0,0 +1,46 @@ +module Day02 + ( day02_1, + day02_2 + ) +where + +import Data.List (isInfixOf) +import Data.List.Split (splitOn) + +sumInvalid :: [Int] -> Int +sumInvalid = foldl addInvalid 0 + where + addInvalid acc n + | odd (length (show n)) = acc + | let l2 = length (show n) `div` 2, take l2 (show n) == drop l2 (show n) = acc + n + | otherwise = acc + +sumInvalid' :: [Int] -> Int +sumInvalid' = foldl addInvalid 0 + where addInvalid acc n + | let s = drop 1 $ show n ++ show n, show n `isInfixOf` take (length s - 1) s = acc + n + | otherwise = acc + + +getRange :: String -> [Int] +getRange r = [read (takeWhile (/= '-') r)..read $ drop 1 (dropWhile (/= '-') r)] + +parseInput :: IO [Int] +parseInput = do + concatMap getRange . splitOn "," <$> readFile "input/day2.txt" + +day02_1 :: IO () +day02_1 = do + values <- parseInput + let result = sumInvalid values + putStrLn $ + "Day 2, Puzzle 1 solution: " + ++ show result + +day02_2 :: IO () +day02_2 = do + values <- parseInput + let result = sumInvalid' values + putStrLn $ + "Day 2, Puzzle 1 solution: " + ++ show result diff --git a/src/Main.hs b/src/Main.hs index 9206283..a10307c 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,6 +1,7 @@ module Main (main) where import Day01 (day01_1, day01_2) +import Day02 (day02_1, day02_2) import System.Environment (getArgs) main :: IO () @@ -12,7 +13,14 @@ main = do "1" : _ -> do day01_1 day01_2 + "2" : "1" : _ -> day02_1 + "2" : "2" : _ -> day02_2 + "2" : _ -> do + day02_1 + day02_2 "all" : _ -> do day01_1 day01_2 + day02_1 + day02_2 _ -> error "Not implemented"