advent-of-code/2023/11/Main1.hs

29 lines
756 B
Haskell
Raw Normal View History

2023-12-14 15:35:02 -08:00
import Data.List (scanl', tails, transpose)
import Data.Function (on)
import Data.Bool (bool)
expandEmpty :: Int
expandEmpty = 2
sumDistances :: (Int, Int) -> [(Int, Int)] -> Int
sumDistances (n, d) = sum . map sumDistance
where
sumDistance (n', d') = n' * n * (d' - d)
genDistances :: [Int] -> [Int]
genDistances = scanl' (flip $ (+) . bool 1 expandEmpty . (== 0)) 0
solveDirection :: [String] -> Int
solveDirection = sum
. (zipWith sumDistances <*> tail . tails)
. (zip <*> genDistances)
. map countHashes
where
countHashes = length . filter (== '#')
solveAll :: [String] -> Int
solveAll = ((+) `on` solveDirection) <*> transpose
main :: IO ()
main = print . solveAll . lines =<< readFile "data.txt"