module Main where data Direction = Upper | Lower doHalf :: [Direction] -> (Int, Int) -> Int doHalf ls start = fst $ foldl fn start ls where fn (l, u) Lower = (l, (u+l) `div` 2) fn (l, u) Upper = ((u+l) `div` 2 + 1, u) doValidate :: String -> Int doValidate s = row * 8 + col where row = flip doHalf (0, 127) $ map readDir (take 7 s) col = flip doHalf (0, 7) $ map readDir (drop 7 s) readDir :: Char -> Direction readDir c | c `elem` "FL" = Lower | otherwise = Upper main :: IO () main = do n <- lines <$> readFile "input/05" let valids = map doValidate n ans1 = maximum valids print ans1 print $ sum [minimum valids .. ans1] - sum valids