From e92425f5318f4752492c64502b7a6730df79370f Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Sat, 9 Dec 2017 03:21:51 +0100 Subject: [PATCH] better interface --- adventofcode.cabal | 3 ++- app/Main.hs | 43 +++++++++++++++++++++++++++++++++++++++++++ package.yaml | 2 ++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/adventofcode.cabal b/adventofcode.cabal index b5ac73b..d05024d 100644 --- a/adventofcode.cabal +++ b/adventofcode.cabal @@ -34,8 +34,8 @@ library Day4 Day5 Day6 - other-modules: Day7 + other-modules: Paths_adventofcode build-depends: base >=4.7 && <5 @@ -57,6 +57,7 @@ executable adventofcode-exe , adventofcode , protolude , pretty + , containers default-language: Haskell2010 test-suite adventofcode-test diff --git a/app/Main.hs b/app/Main.hs index e89eb79..363396a 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -3,11 +3,16 @@ module Main where import Protolude + import Text.PrettyPrint hiding ((<>)) +import System.Environment (getArgs) +import qualified Data.Map as Map import qualified Day1 +import qualified Day2 import qualified Day5 import qualified Day6 +import qualified Day7 showSol :: [Char] -> Doc -> IO () showSol txt d = putText . toS . render $ @@ -16,14 +21,43 @@ showSol txt d = putText . toS . render $ main :: IO () main = do + args <- getArgs + fromMaybe (traverse_ snd (Map.toAscList solutions)) + (Map.lookup args solutions) + +solutions :: Map [[Char]] (IO ()) +solutions = Map.fromList [(["1"], day1) + ,(["2"], day2) + ,(["5"], day5) + ,(["6"], day6) + ,(["7"], day7) + ] + +day1 :: IO () +day1 = do putText "Day 1:" input1 <- Day1.ex1code showSol "Solution 1" (int (Day1.solution1 input1)) showSol "Solution 2" (int (Day1.solution2 input1)) + +day2 :: IO () +day2 = do + putText "Day 2:" + input <- Day2.parseInput + let sol1 = maybe 0 Day2.solution1 input + showSol "Solution 1" (integer sol1) + let sol2 = maybe 0 Day2.solution2 input + showSol "Solution 2" (integer sol2) + +day5 :: IO () +day5 = do putText "Day 5:" input5 <- Day5.parseInput sol5 <- Day5.solution2 input5 showSol "Solution 2" (int sol5) + +day6 :: IO () +day6 = do putText "Day 6:" input6_1 <- Day6.parseInput sol6_1 <- Day6.solution1 input6_1 @@ -31,3 +65,12 @@ main = do input6_2 <- Day6.parseInput sol6_2 <- Day6.solution2 input6_2 showSol "Solution 2" (int sol6_2) + +day7 :: IO () +day7 = do + putText "Day 7:" + input <- Day7.parseInput + let sol_1 = Day7.rootOf input + showSol "Solution 1" (text (toS (maybe "" Day7.name sol_1))) + let sol_2 = Day7.solution2 input + showSol "Solution 2" (int (maybe 0 snd sol_2)) diff --git a/package.yaml b/package.yaml index c7b1c83..b3ba6dc 100644 --- a/package.yaml +++ b/package.yaml @@ -19,6 +19,7 @@ library: - Day4 - Day5 - Day6 + - Day7 dependencies: - base >=4.7 && <5 - protolude @@ -40,6 +41,7 @@ executables: - adventofcode - protolude - pretty + - containers tests: adventofcode-test: main: Spec.hs