import Control.Lens
import Data.List
import Control.Monad
import Data.Ord

main :: IO ()
main = do
    input <- lines <$> readFile "input"
    print $ last $ seatList input
    print $ minimumBy (comparing (uncurry (-)) ) $ ap zip tail $ seatList input

seatList :: [String] -> [Int]
seatList = sort . map ((uncurry (+) . (_2 *~ 8)) . parseSeat)

parseSeat :: String -> (Int,Int)
parseSeat line = (parseOn 'R' cols,parseOn 'B' rows)
    where (cols,rows) = splitAt 3 (reverse line)
          parseOn c = sum . map (round . (**) 2 . fst) . filter (\(_,x)->x==c) . zip [0..]
