module Main where import Data.Char (digitToInt) import Data.Either (rights) import Debug.Trace import System.Environment (getArgs) -- | A candidate is either a left-most number we've just selected, or -- a complete candidate represented as a pair. type Candidate = Either Int (Int, Int) main :: IO () main = do input <- readFile . head =<< getArgs putStrLn $ "Part 1: " ++ show (part1 input) putStrLn $ "Part 2: " ++ show (part2 input) -- | Maintain a list of candidates (either partial, or complete) and -- then select the greatest one for each line. -- -- Insight: By keeping a list of possible candidates we can always -- backtrack out of an accidental corner (like making a partial -- candidate of the last digit). part1 :: String -> Int part1 = sum . map (maximum . map combine . rights . foldl go [] . map digitToInt) . lines where go :: [Candidate] -> Int -> [Candidate] go [] x = pure $ Left x go (Left a : candidates') x | x > a = Left x : Right (a, x) : candidates' | otherwise = Right (a, x) : candidates' go candidates@(Right (a, b) : candidates') x | x > a = Left x : Right (a, x) : candidates | x > b = Right (a, x) : candidates' | otherwise = candidates combine :: (Int, Int) -> Int combine (a, b) = a * 10 + b part2 :: String -> Int part2 = error "Not implemented" testInput :: String testInput = unlines [ "987654321111111", "811111111111119", "234234234234278", "818181911112111" ]