-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.hs
executable file
·44 lines (33 loc) · 1010 Bytes
/
part1.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env runghc
import Data.Array
data Cell = Open | Tree
deriving (Eq)
charToCell '.' = Open
charToCell '#' = Tree
isValidCell '.' = True
isValidCell '#' = True
isValidCell _ = False
instance Read Cell where
readsPrec _ (c:rest) = [(charToCell c, rest)]
readList input =
let (valid, rest) = span isValidCell input
in [(map charToCell valid, rest)]
cellToChar Open = '.'
cellToChar Tree = '#'
instance Show Cell where
show cell = [cellToChar cell]
showList = (++) . map cellToChar
main = interact (show . solve . map (read :: String -> [Cell]) . lines)
solve rows = count (==Tree) $ map at path
where
board = toArray $ map toArray rows
(_, y) = fmap (+1) $ bounds board
(_, x) = fmap (+1) $ bounds $ board ! 0
xIdx i = mod i x
yIdx i = mod i y
deltaY = 1
deltaX = 3
at (x', y') = (board ! (yIdx y')) ! (xIdx x')
path = [(i*3, i) | i <- [0..y]]
count predicate xs = length $ filter predicate xs
toArray l = listArray (0, length l - 1) l