aoc2020/day3/day3.hs

29 lines
1020 B
Haskell
Raw Permalink Normal View History

2020-12-05 09:53:27 +01:00
import AoC
2020-12-03 22:33:03 +01:00
import Control.Applicative (empty)
import Control.Monad.Zip (mzip)
import Data.List (intercalate)
import Data.Maybe (catMaybes)
compose2 :: (a' -> b' -> c) -> (a -> a') -> (b -> b') -> a -> b -> c
compose2 f g h x y = f (g x) (h y)
2020-12-05 09:53:27 +01:00
(??) :: Functor f => f (a -> b) -> a -> f b
f ?? x = fmap ($ x) f
2020-12-03 22:33:03 +01:00
countHits :: (Int, Int) -> [[Bool]] -> Int
2020-12-04 10:34:46 +01:00
countHits = length . filterMap lookup . catMaybes .: compose2 (zipWith mzip) maybeIndices justLines
where justRightSteps = map (pure . Just) . enumFromThen 0
downNothings = flip replicate Nothing . pred
maybeIndices = uncurry $ compose2 (flip intercalate) justRightSteps downNothings
justLines = map Just
lookup = uncurry $ flip (!!)
filterMap = filter id .: map
2020-12-03 22:33:03 +01:00
treeCharToBool :: Char -> Bool
treeCharToBool = (== '#')
2020-12-06 07:37:17 +01:00
main = runAoC (map (cycle . map treeCharToBool) . lines) part1 part2
2020-12-05 09:53:27 +01:00
where part1 = countHits (3, 1)
part2 = product . (fmap countHits [(1,1), (3,1), (5,1), (7,1), (1,2)] ??)