-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.hs
50 lines (37 loc) · 860 Bytes
/
run.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
45
46
47
48
49
50
import Data.List
data Cell = Tree | NoTree
deriving (Show, Eq)
pp NoTree = '.'
pp Tree = '#'
ppCells n = map (take n . map pp)
parse '.' = NoTree
parse '#' = Tree
parseAll :: String -> [[Cell]]
parseAll =
map (cycle . map parse)
. lines
traverseStep :: (Int, Int) -> [[Cell]] -> [[Cell]]
traverseStep (dx, dy) =
drop dy
. map (drop dx)
traverseAll :: (Int, Int) -> [[Cell]] -> [Cell]
traverseAll v =
map (head . head)
. takeWhile (not . null)
. iterate (traverseStep v)
printCells n =
putStrLn . unlines . ppCells n
countTrees v = length . filter (== Tree) . traverseAll v
part1 = countTrees (3, 1)
slopes =
[ (1, 1)
, (3, 1)
, (5, 1)
, (7, 1)
, (1, 2) ]
part2 input =
product $ map (flip countTrees input) slopes
main = do
input <- parseAll <$> readFile "input.txt"
print (part1 input)
print (part2 input)