aoc2020/day3.hs
2020-12-03 18:34:36 +13:00

41 lines
1.0 KiB
Haskell

main :: IO ()
main = do
raw <- readFile "day3.txt"
let ls = map parse $ lines raw
ansA = solveSlope 3 1 ls
ansB = foldr (*) 1 $
[ (solveSlope 1 1 ls)
, (solveSlope 3 1 ls)
, (solveSlope 5 1 ls)
, (solveSlope 7 1 ls)
, (solveSlope 1 2 ls)
]
in do
putStrLn $ "day3a: " ++ (show ansA)
putStrLn $ "day3b: " ++ (show ansB)
parse :: String -> [Bool]
parse = map (== '#')
solveLine :: [Bool] -> Int -> Bool
solveLine [] _ = error "empty list"
solveLine bs i = bs !! index
where
index = i `mod` (length bs)
solveSlope :: Int -> Int -> [[Bool]] -> Int
solveSlope r d bs =
length $ filter (== True) $ solveSlope' r d bs 0 []
solveSlope' :: Int -> Int -> [[Bool]] -> Int -> [Bool] -> [Bool]
solveSlope' _ _ [] _ as = as
solveSlope' r d bs@(b:cs) i as =
if ns == []
then as
else solveSlope' r d ns ni $ (solveLine (head ns) ni) : as
where
ni = (i + r) `mod` (length b)
ns = if (length bs) <= d
then []
else (drop d bs)