This commit is contained in:
KoenDR06 2025-12-04 14:28:58 +01:00
parent aa0c915941
commit 1e816929b0

49
src/Day2503.hs Normal file
View file

@ -0,0 +1,49 @@
module Main where
import Data.List (tails, singleton)
import Data.Maybe (fromMaybe)
import Data.Map qualified as M
import Debug.Trace (trace, traceWith)
pairs :: [a] -> [(a,a)]
pairs l = [(x,y) | (x:ys) <- tails l, y <- ys]
digitCount :: Int -> Int
digitCount 0 = 0
digitCount n = 1 + digitCount (n `div` 10)
--------------------
----- Problems -----
--------------------
part1 :: String -> Int
part1 = sum . map (processBank . map (read . singleton)) . lines
where
processBank :: [Int] -> Int
processBank = maximum . map (\(a,b) -> 10*a+b) . pairs
part2 :: String -> Int
part2 = sum . map (findMax 12 . map (read . singleton)) . lines
where
findMax :: Int -> [Int] -> Int
findMax n xs' = foldl (\acc curr -> curr + 10 * acc) 0 $ go xs' 0
where
go :: [Int] -> Int -> [Int]
go _ p | p == n = []
go xs p | length xs == n-p = xs
go xs@(x:rest) p | lookWidth <= 0 = xs
| maxRem == x = x:go rest (p+1)
| otherwise = go rest p
where
lookWidth = length xs - (n-p) + 1
maxRem = maximum $ take lookWidth xs
main :: IO ()
main = do
str <- getContents
print $ part1 str
print $ part2 str