From: Mart Lubbers Date: Thu, 3 Dec 2020 06:04:31 +0000 (+0100) Subject: make more elegant X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=b3967f419310e250dcdbfb0f0e8920e08df87404;p=aoc20.git make more elegant --- 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