2023 day 11: Haskell solution
This commit is contained in:
parent
e9fd31ce38
commit
fc5af8bb0b
28
2023/11/Main1.hs
Normal file
28
2023/11/Main1.hs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
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"
|
28
2023/11/Main2.hs
Normal file
28
2023/11/Main2.hs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
import Data.List (scanl', tails, transpose)
|
||||||
|
import Data.Function (on)
|
||||||
|
import Data.Bool (bool)
|
||||||
|
|
||||||
|
expandEmpty :: Int
|
||||||
|
expandEmpty = 1000000
|
||||||
|
|
||||||
|
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"
|
Loading…
Reference in a new issue