Day 3
This commit is contained in:
parent
aa0c915941
commit
1e816929b0
1 changed files with 49 additions and 0 deletions
49
src/Day2503.hs
Normal file
49
src/Day2503.hs
Normal 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
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue