29 lines
756 B
Haskell
29 lines
756 B
Haskell
|
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"
|