module Problem42 where

import Data.Char
import Data.List.Split

main :: IO ()
main = do
    names <- readFile "txt/42.txt"
    print . length . filter isTriangular . map value . wordsBy (not . isLetter) $ names

value :: String -> Int
value = sum . map (\x -> ord x - ord 'A' + 1) . filter (/= '\"')

isTriangular :: Int -> Bool
-- n = x(x+1)/2 => x^2 + x - 2n = 0 => x = (-1 +- sqrt(1+8n))/2
isTriangular n = n == x' * (x' + 1) `div` 2
    where x' = floor $ ((sqrt $ fromIntegral (1 + 8 * n) :: Double) - 1) / 2
