|
| 1 | +module Main where |
| 2 | + |
| 3 | +{- ORMOLU_DISABLE -} |
| 4 | +--- Day imports |
| 5 | +import qualified Days.Day01 as Day01 (runDay) |
| 6 | +import qualified Days.Day02 as Day02 (runDay) |
| 7 | +import qualified Days.Day03 as Day03 (runDay) |
| 8 | +import qualified Days.Day04 as Day04 (runDay) |
| 9 | +import qualified Days.Day05 as Day05 (runDay) |
| 10 | +import qualified Days.Day06 as Day06 (runDay) |
| 11 | +import qualified Days.Day07 as Day07 (runDay) |
| 12 | +import qualified Days.Day08 as Day08 (runDay) |
| 13 | +import qualified Days.Day09 as Day09 (runDay) |
| 14 | +import qualified Days.Day10 as Day10 (runDay) |
| 15 | +import qualified Days.Day11 as Day11 (runDay) |
| 16 | +import qualified Days.Day12 as Day12 (runDay) |
| 17 | +import qualified Days.Day13 as Day13 (runDay) |
| 18 | +import qualified Days.Day14 as Day14 (runDay) |
| 19 | +import qualified Days.Day15 as Day15 (runDay) |
| 20 | +import qualified Days.Day16 as Day16 (runDay) |
| 21 | +import qualified Days.Day17 as Day17 (runDay) |
| 22 | +import qualified Days.Day18 as Day18 (runDay) |
| 23 | +import qualified Days.Day19 as Day19 (runDay) |
| 24 | +import qualified Days.Day20 as Day20 (runDay) |
| 25 | +import qualified Days.Day21 as Day21 (runDay) |
| 26 | +import qualified Days.Day22 as Day22 (runDay) |
| 27 | +import qualified Days.Day23 as Day23 (runDay) |
| 28 | +import qualified Days.Day24 as Day24 (runDay) |
| 29 | +import qualified Days.Day25 as Day25 (runDay) |
| 30 | + |
| 31 | +--- Other imports |
| 32 | +import Data.Map (Map) |
| 33 | +import qualified Data.Map as Map |
| 34 | +import Data.Maybe (fromMaybe) |
| 35 | +import Options.Applicative |
| 36 | +import qualified Control.Applicative.Combinators as C (option) |
| 37 | +import Program.RunDay (Day, Verbosity (Quiet, Timings, Verbose)) |
| 38 | +import Data.List (intercalate) |
| 39 | +import Control.Monad (unless, forM_) |
| 40 | + |
| 41 | +-- Data Output |
| 42 | +import Text.Printf (printf) |
| 43 | +import Program.Color ( withColor ) |
| 44 | +import System.Console.ANSI (Color(..)) |
| 45 | +{- ORMOLU_ENABLE -} |
| 46 | + |
| 47 | +data Days |
| 48 | + = AllDays |
| 49 | + | OneDay |
| 50 | + { day :: Int, |
| 51 | + input :: Maybe String |
| 52 | + } |
| 53 | + deriving (Show) |
| 54 | + |
| 55 | +data Options = Options Days Verbosity |
| 56 | + |
| 57 | +dayParser :: Parser Days |
| 58 | +dayParser = (OneDay <$> day <*> input) <|> allDays |
| 59 | + where |
| 60 | + day = |
| 61 | + option auto $ |
| 62 | + long "day" <> short 'd' <> metavar "DAY" |
| 63 | + <> help "Present the solutions for one day." |
| 64 | + |
| 65 | + input = |
| 66 | + optional $ |
| 67 | + strOption $ |
| 68 | + long "input" <> short 'i' <> metavar "FILE" |
| 69 | + <> help "The file to read the selected day's input from." |
| 70 | + |
| 71 | + allDays = |
| 72 | + flag' AllDays $ |
| 73 | + long "all-days" |
| 74 | + <> help |
| 75 | + ( unwords |
| 76 | + [ "Present solutions for all of the days of", |
| 77 | + "Advent of Code, with default input file names." |
| 78 | + ] |
| 79 | + ) |
| 80 | + |
| 81 | +optionsParser :: Parser Options |
| 82 | +optionsParser = Options <$> dayParser <*> verbosityParser |
| 83 | + where |
| 84 | + verbosityParser :: Parser Verbosity |
| 85 | + verbosityParser = |
| 86 | + C.option Quiet $ |
| 87 | + ( flag' Verbose $ |
| 88 | + long "verbose" <> short 'v' |
| 89 | + <> help |
| 90 | + ( unwords |
| 91 | + [ "Whether to print out extra info, such as the", |
| 92 | + "result of the input parser, and more detailed", |
| 93 | + "error messages.", |
| 94 | + "Also enables timing of solutions." |
| 95 | + ] |
| 96 | + ) |
| 97 | + ) |
| 98 | + <|> ( flag' Timings $ |
| 99 | + long "timings" <> short 't' |
| 100 | + <> help |
| 101 | + ( unwords |
| 102 | + ["Whether to enable timing of the solutions."] |
| 103 | + ) |
| 104 | + ) |
| 105 | + |
| 106 | +days :: Map Int (Day, String) |
| 107 | +days = |
| 108 | + Map.fromList . zip [1 ..] $ |
| 109 | + [ (Day01.runDay, "input/Day01.txt"), |
| 110 | + (Day02.runDay, "input/Day02.txt"), |
| 111 | + (Day03.runDay, "input/Day03.txt"), |
| 112 | + (Day04.runDay, "input/Day04.txt"), |
| 113 | + (Day05.runDay, "input/Day05.txt"), |
| 114 | + (Day06.runDay, "input/Day06.txt"), |
| 115 | + (Day07.runDay, "input/Day07.txt"), |
| 116 | + (Day08.runDay, "input/Day08.txt"), |
| 117 | + (Day09.runDay, "input/Day09.txt"), |
| 118 | + (Day10.runDay, "input/Day10.txt"), |
| 119 | + (Day11.runDay, "input/Day11.txt"), |
| 120 | + (Day12.runDay, "input/Day12.txt"), |
| 121 | + (Day13.runDay, "input/Day13.txt"), |
| 122 | + (Day14.runDay, "input/Day14.txt"), |
| 123 | + (Day15.runDay, "input/Day15.txt"), |
| 124 | + (Day16.runDay, "input/Day16.txt"), |
| 125 | + (Day17.runDay, "input/Day17.txt"), |
| 126 | + (Day18.runDay, "input/Day18.txt"), |
| 127 | + (Day19.runDay, "input/Day19.txt"), |
| 128 | + (Day20.runDay, "input/Day20.txt"), |
| 129 | + (Day21.runDay, "input/Day21.txt"), |
| 130 | + (Day22.runDay, "input/Day22.txt"), |
| 131 | + (Day23.runDay, "input/Day23.txt"), |
| 132 | + (Day24.runDay, "input/Day24.txt"), |
| 133 | + (Day25.runDay, "input/Day25.txt") |
| 134 | + ] |
| 135 | + |
| 136 | +performDay :: Options -> IO () |
| 137 | +performDay (Options d v) = case d of |
| 138 | + AllDays -> do |
| 139 | + results <- |
| 140 | + let eachDay d (dayFunc, inputFile) = do |
| 141 | + withColor Magenta $ putStrLn $ printf "\n***Day %02d***" d |
| 142 | + dayFunc v inputFile |
| 143 | + in sequence $ Map.mapWithKey eachDay days |
| 144 | + |
| 145 | + printSummary results |
| 146 | + OneDay {..} -> case days Map.!? day of |
| 147 | + Nothing -> putStrLn "Invalid day provided. There are 25 days in Advent." |
| 148 | + Just (dayFunc, inputFile) -> do |
| 149 | + let i' = fromMaybe inputFile input |
| 150 | + withColor Magenta $ putStrLn $ printf "\n***Day %02d***" day |
| 151 | + dayFunc v i' |
| 152 | + withColor Magenta $ putStrLn "************" |
| 153 | + |
| 154 | +printSummary :: Map Int (Maybe Double, Maybe Double) -> IO () |
| 155 | +printSummary results = do |
| 156 | + putStrLn "\n************\n Summary: " |
| 157 | + let partsA = Map.mapKeys ((++ " (a)") . printf "%02d") $ fmap fst results |
| 158 | + partsB = Map.mapKeys ((++ " (b)") . printf "%02d") $ fmap snd results |
| 159 | + parts = Map.toList $ partsA <> partsB |
| 160 | + |
| 161 | + fails = [p | (p, Nothing) <- parts] |
| 162 | + fasts = [(p, t) | (p, Just t) <- parts, t < 1] |
| 163 | + slows = [(p, t) | (p, Just t) <- parts, t >= 1] |
| 164 | + |
| 165 | + putStr $ printf "\n%d parts " $ length fasts |
| 166 | + withColor Green $ putStr "completed in under 1 second" |
| 167 | + putStrLn ".\nOf the remainder:" |
| 168 | + unless (null fails) $ do |
| 169 | + putStr $ printf " %d parts" $ length fails |
| 170 | + withColor Red $ putStr " failed" |
| 171 | + putStrLn $ ":\n " ++ intercalate ", " fails |
| 172 | + unless (null slows) $ do |
| 173 | + putStr $ printf " %d parts" $ length slows |
| 174 | + withColor Yellow $ putStr " took over 1 second to complete" |
| 175 | + putStrLn ":" |
| 176 | + forM_ slows $ |
| 177 | + \(p, t) -> putStrLn $ printf " %s took %.2f seconds" p t |
| 178 | + |
| 179 | +main :: IO () |
| 180 | +main = performDay =<< execParser opts |
| 181 | + where |
| 182 | + opts = |
| 183 | + info |
| 184 | + (optionsParser <**> helper) |
| 185 | + (fullDesc <> progDesc "Prints out some Advent of Code solutions.") |
0 commit comments