Created
February 10, 2012 22:04
-
-
Save anonymous/1793369 to your computer and use it in GitHub Desktop.
Embd.ly Challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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