-
Notifications
You must be signed in to change notification settings - Fork 0
/
3a.hs
26 lines (21 loc) · 802 Bytes
/
3a.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import Prelude
main = do
input <- getContents
putStr $ show $ fn $ lines input
safeIndex :: [a] -> Int -> Maybe a
safeIndex xs i
| (i > -1) && (length xs > i) = Just (xs !! i)
| otherwise = Nothing
charMatchAt :: Int -> Char -> [Char] -> Bool
charMatchAt x char str = case safeIndex str x of
Just y -> y == char
_ -> False
folder :: (Int, Int, Int) -> [String] -> Int
folder (trees, _, _) [] = trees
folder (trees, runningOffset, offset) (head : xs) =
if charMatchAt (runningOffset `mod` length head) '#' head
then folder (trees + 1, runningOffset + offset, offset) xs
else folder (trees, runningOffset + offset, offset) xs
-- (trees, offset, runningOffset)
fn :: [String] -> Int
fn = folder (0, 0, 3) -- This works because my 0 is a '.' If it was a '#', this wouldn't work.