import Data.List (sort) main :: IO () main = do raw <- readFile "day5.txt" let valid = foldr (&&) True $ map validate $ lines raw ls = if valid then map parse $ lines raw else error "invalid input" ansA = maximum ls ansB = findGap $ sort ls in do putStrLn $ "day5a: " ++ (show ansA) putStrLn $ "day5b: " ++ (show ansB) validate :: String -> Bool validate s = length s == 10 && all (`elem` ['F', 'B']) (take 7 s) && all (`elem` ['L', 'R']) (drop 7 s) parse :: String -> Int parse s = row * 8 + column where rowString = (take 7 s) columnString = (drop 7 s) row = fromBinaryString 'B' rowString column = fromBinaryString 'R' columnString fromBinaryString :: Char -> String -> Int fromBinaryString t s = foldr (+) 0 $ map snd $ filter (\x -> fst x == t) $ zip (reverse s) powList findGap :: [Int] -> Int findGap [] = error "empty list" findGap [x] = error "short list" findGap (x:y:xs) = if y == succ x then findGap (y:xs) else succ x powList :: [Int] powList = 1 : map (* 2) powList