From b3967f419310e250dcdbfb0f0e8920e08df87404 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Thu, 3 Dec 2020 07:04:31 +0100 Subject: [PATCH] make more elegant --- 3/one.icl | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/3/one.icl b/3/one.icl index cd566ea..3bac706 100644 --- a/3/one.icl +++ b/3/one.icl @@ -2,25 +2,22 @@ module one 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 -- 2.20.1