Skip to content

Instantly share code, notes, and snippets.

@lgastako
Created April 28, 2019 16:08
Show Gist options
  • Save lgastako/a8c6a4e1f4ea267567dc8d80f20501f6 to your computer and use it in GitHub Desktop.
Save lgastako/a8c6a4e1f4ea267567dc8d80f20501f6 to your computer and use it in GitHub Desktop.
main = interact $ show . f . map (map read . words) . lines
f :: [[Int]] -> Int
f ([n]:xs:_)
| isArith xs = nthArith n xs
| otherwise = nthGeom n xs
isArith :: [Int] -> Bool
isArith (a:b:c:_) = b - a == c - b
nthArith :: Int -> [Int] -> Int
nthArith n (a:b:_) = seq !! n
where
seq = [a, a+(b-a)..]
nthGeom :: Int -> [Int] -> Int
nthGeom n (a:b:_) = seq !! n
where
seq = geoFrom a b (b `div` a)
geoFrom :: Int -> Int -> Int -> [Int]
geoFrom n n' m = seq
where
seq
| signum n /= signum n' = n:map (*m) seq
| n < n' = n:map (*m) seq
| otherwise = decreasing n n'
decreasing n n' = seq
where
seq = n:map (`div` d) seq
d = n `div` n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment