import StdEnv
-read :: *File -> [String]
+read :: *File -> [[Int]]
read f
# (l, f) = freadline f
| l.[size l - 1] <> '\n' = []
- = [l % (0, size l - 2):read f]
+ = [let c = [if (c == '#') 1 0\\c<-:l | c <> '\n'] ++ c in c:read f]
Start w
# (io, w) = stdio w
# ls = read io
- = (one 0 ls, two ls)
+ = (one ls, two ls)
-one :: Int [String] -> Int
-one _ [] = 0
-one i [x:xs] = if (x.[i rem size x] == '#') 1 0 + one (i+3) xs
+one ls :== slope ls (3, 1)
-two :: [String] -> Int
-two ls = prod [slope right down 0 ls\\(right, down)<-[(1,1), (3,1), (5,1), (7,1), (1,2)]]
+two :: [[Int]] -> Int
+two ls = prod (map (slope ls) [(1,1), (3,1), (5,1), (7,1), (1,2)])
-slope :: Int Int Int [String] -> Int
-slope right down i [x:xs]
- = (if (x.[i rem size x] == '#') 1 0) + slope right down (i + right) (drop (dec down) xs)
-slope _ _ _ [] = 0
+slope :: [[Int]] (Int, Int) -> Int
+slope [] _ = 0
+slope xs=:[[c:_]:_] d=:(right, down) = c + slope (map (drop right) (drop down xs)) d