Skip to content

Instantly share code, notes, and snippets.

Created February 10, 2012 22:04
Show Gist options
  • Save anonymous/1793369 to your computer and use it in GitHub Desktop.
Save anonymous/1793369 to your computer and use it in GitHub Desktop.
Embd.ly Challenge
-- Sum Of Digits
sod = go 0
where go acc i | i == 0 = acc
| otherwise = go (r+acc) q
where (q,r) = quotRem i 10
solve n = go 1 facs
where go j (a:as) | sod a == n = j
| otherwise = go (j+1) as
facs = scanl1 (*) [1..] -- save some recomputation
main = print $ solve 8001
-- primitive html parser
html2depth = go [] 0 []
where go :: [Int] -> Int -> [String] -> String -> [Int]
go acc 0 _ "" = acc
go acc d ts ('<':'p':'>':rs) = go (d:acc) (d+1) ("p":ts) rs
go acc d (t:ts) ('<':'/':rs) | t == s = go acc (d-1) ts $ dropWhile (/= '<') rs
where s = takeWhile (/= '>') rs
go acc d ts ('<':rs) = go acc (d+1) (s:ts) $ dropWhile (/= '<') rs
where s = takeWhile (/= '>') rs
go acc d ts rs = go acc d ts $ dropWhile (/= '<') rs
solve ls = sqrt $ q/n
where n = fromIntegral $ length ls'
s = sum ls'
q = sum $ map (\i -> (i-m)^2) ls'
m = s / n
ls' :: [Double]
ls' = map fromIntegral ls
-- call as "embedly-2 < input.html"
-- strip everything before and after article tag first
main = interact (show . solve . html2depth)
solve n = (1+) $ length $ takeWhile (<t) $ scanl1 (+) fs'
where fs = [1..n] -- no need
m = foldl1 lcm fs -- for fractions
fs' = map (m `div`) fs
t = (sum fs') `div` 2 -- half of 'all' the words
main = print $ solve 900
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment