diff --git a/2015/haskell/app/Main.hs b/2015/haskell/app/Main.hs index 53dc4c3..f2e2b73 100644 --- a/2015/haskell/app/Main.hs +++ b/2015/haskell/app/Main.hs @@ -1,18 +1,19 @@ +{-# LANGUAGE TypeApplications #-} module Main where -import Text.Printf (printf) import qualified Day01 +import qualified Day02 import qualified Day03 +import Text.Printf (printf) readDay :: Int -> IO String readDay n = readFile $ getPath n - where - getPath n = "../.inputs/" <> printf "%02d" n <> ".txt" - + where + getPath n = "../.inputs/" <> printf "%02d" n main :: IO () main = do - content <- readDay 3 - let parsed = Day03.parse content - print $ Day03.solve1 parsed - print $ Day03.solve2 parsed + content <- readDay 2 + let parsed = Day02.parse content + print $ Day02.solveA parsed + print @Int $ Day02.solveB parsed diff --git a/2015/haskell/haskell.cabal b/2015/haskell/haskell.cabal index 4b897c9..0bb9fe5 100644 --- a/2015/haskell/haskell.cabal +++ b/2015/haskell/haskell.cabal @@ -1,48 +1,43 @@ -cabal-version: 3.0 -name: haskell -version: 0.1.0.0 -synopsis: Advent of Code 2015 solutions - +cabal-version: 3.0 +name: aoc2015 +version: 0.1.0.0 +synopsis: Advent of Code 2015 solutions -- A longer description of the package. -- description: homepage: - -- A URL where users can report bugs. -- bug-reports: -license: MIT -author: Maciej Jur -maintainer: maciej@kamoshi.org - +license: MIT +author: Maciej Jur +maintainer: maciej@kamoshi.org -- A copyright notice. -- copyright: -- category: extra-source-files: CHANGELOG.md library - exposed-modules: - Day01 - Day03 + exposed-modules: + Day01 + Day02 + Day03 - -- Modules included in this library but not exported. - -- other-modules: - - -- LANGUAGE extensions used by modules in this package. - -- other-extensions: - build-depends: base ^>=4.16.4.0 - hs-source-dirs: src - default-language: Haskell2010 + -- Modules included in this library but not exported. + -- other-modules: + -- LANGUAGE extensions used by modules in this package. + -- other-extensions: + build-depends: base >=4.16.4.0 + hs-source-dirs: solutions + default-language: Haskell2010 executable haskell - main-is: Main.hs + 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.4.0, + aoc2015, - -- 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.4.0, - haskell - - hs-source-dirs: app - default-language: Haskell2010 + hs-source-dirs: app + default-language: Haskell2010 diff --git a/2015/haskell/src/Day01.hs b/2015/haskell/solutions/Day01.hs similarity index 68% rename from 2015/haskell/src/Day01.hs rename to 2015/haskell/solutions/Day01.hs index 287568a..8611fc7 100644 --- a/2015/haskell/src/Day01.hs +++ b/2015/haskell/solutions/Day01.hs @@ -1,10 +1,9 @@ module Day01 (parse, solve1, solve2) where import Data.Char (isPrint) -import Data.List (scanl, findIndex) +import Data.List (findIndex, scanl) import Data.Maybe (fromJust) - parse :: String -> String parse = filter isPrint @@ -18,7 +17,6 @@ solve1 :: String -> Int solve1 = sum . map mapper solve2 :: String -> Int -solve2 = fromJust . findIndex (<0) . scanl reducer 0 - where - reducer acc next = acc + mapper next - +solve2 = fromJust . findIndex (< 0) . scanl reducer 0 + where + reducer acc next = acc + mapper next diff --git a/2015/haskell/solutions/Day02.hs b/2015/haskell/solutions/Day02.hs new file mode 100644 index 0000000..cff0569 --- /dev/null +++ b/2015/haskell/solutions/Day02.hs @@ -0,0 +1,30 @@ +module Day02 (parse, solveA, solveB) where + +splitOn :: (Eq a) => a -> [a] -> [[a]] +splitOn _ [] = [] +splitOn delimiter xs = + let (before, remainder) = break (== delimiter) xs + in before : case remainder of + [] -> [] + _ : rest -> splitOn delimiter rest + +parse :: String -> [(Int, Int, Int)] +parse = map toTuple . lines + where + toTuple :: String -> (Int, Int, Int) + toTuple str = let [a, b, c] = splitOn 'x' str in (read a, read b, read c) + +toArea :: (Int, Int, Int) -> Int +toArea (l, w, h) = + let a = l * w + b = w * h + c = h * l + in 2 * a + 2 * b + 2 * c + min a (min b c) + +solveA :: [(Int, Int, Int)] -> Int +solveA = sum . map toArea + +-- >>> map toArea $ parse "2x3x4" +-- [64] + +solveB = undefined diff --git a/2015/haskell/src/Day03.hs b/2015/haskell/solutions/Day03.hs similarity index 100% rename from 2015/haskell/src/Day03.hs rename to 2015/haskell/solutions/Day03.hs