From 7e0c66b8f6f142420041972620f3795c83a4c22f Mon Sep 17 00:00:00 2001 From: Andreas Grois Date: Wed, 21 Dec 2022 00:05:25 +0100 Subject: Day8 partial input parsing. --- Day8/CHANGELOG.md | 5 +++++ Day8/Day8.cabal | 34 ++++++++++++++++++++++++++++++++++ Day8/app/Main.hs | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 Day8/CHANGELOG.md create mode 100644 Day8/Day8.cabal create mode 100644 Day8/app/Main.hs diff --git a/Day8/CHANGELOG.md b/Day8/CHANGELOG.md new file mode 100644 index 0000000..e61d908 --- /dev/null +++ b/Day8/CHANGELOG.md @@ -0,0 +1,5 @@ +# Revision history for Day8 + +## 0.1.0.0 -- YYYY-mm-dd + +* First version. Released on an unsuspecting world. diff --git a/Day8/Day8.cabal b/Day8/Day8.cabal new file mode 100644 index 0000000..765b9f1 --- /dev/null +++ b/Day8/Day8.cabal @@ -0,0 +1,34 @@ +cabal-version: 2.4 +name: Day8 +version: 0.1.0.0 + +-- A short (one-line) description of the package. +-- synopsis: + +-- A longer description of the package. +-- description: + +-- A URL where users can report bugs. +-- bug-reports: + +-- The license under which the package is released. +-- license: +author: Andreas Grois +maintainer: andi@grois.info + +-- A copyright notice. +-- copyright: +-- category: +extra-source-files: CHANGELOG.md + +executable Day8 + main-is: Main.hs + + -- Modules included in this executable, other than Main. + -- other-modules: + + -- LANGUAGE extensions used by modules in this package. + -- other-extensions: + build-depends: base ^>=4.16.3.0 + hs-source-dirs: app + default-language: Haskell2010 diff --git a/Day8/app/Main.hs b/Day8/app/Main.hs new file mode 100644 index 0000000..fc36d66 --- /dev/null +++ b/Day8/app/Main.hs @@ -0,0 +1,41 @@ +module Main (main) where + +import System.Environment ( getArgs ) + +main :: IO () +main = getArgs >>= readFile . head >>= print . solveDay8 + +solveDay8 :: String -> String +solveDay8 = formatDay8Result . fmap solveDay8Parts . parseDay8Input + +formatDay8Result :: Maybe (Int, Int) -> String +formatDay8Result Nothing = "Input didn't parse. Check that it is a rectangular grid with values from 0-9." +formatDay8Result (Just (p1, p2)) = "Part 1: " ++ show p1 ++ ", Part 2: " ++ show p2 + +newtype Day8Input = Day8Input [[Height]] -- but with guarantee that it's rectangular + +data Height = Zero | One | Two | Three | Four | Five | Six | Seven | Eight | Nine + deriving (Eq, Ord) + +heightFromChar :: Char -> Maybe Height -- this could also be done using the Enum typeclass' toEnum function and an intermediate Int +heightFromChar '0' = Just Zero +heightFromChar '1' = Just One +heightFromChar '2' = Just Two +heightFromChar '3' = Just Three +heightFromChar '4' = Just Four +heightFromChar '5' = Just Five +heightFromChar '6' = Just Six +heightFromChar '7' = Just Seven +heightFromChar '8' = Just Eight +heightFromChar '9' = Just Nine +heightFromChar _ = Nothing + +validateDay8Input :: [[Height]] -> Maybe Day8Input +validateDay8Input [] = Nothing +validateDay8Input (h:hs) = if all ((== length h) . length) hs then Just $ Day8Input $ h:hs else Nothing + +parseDay8Input :: String -> Maybe Day8Input +parseDay8Input = (=<<) validateDay8Input . mapM (mapM heightFromChar) . lines + +solveDay8Parts :: Day8Input -> (Int, Int) +solveDay8Parts = undefined \ No newline at end of file -- cgit v1.2.3