haskell: 2015 02a

This commit is contained in:
Maciej Jur 2024-07-30 01:11:49 +02:00
parent 81e1d5792f
commit fee1d10eb7
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
5 changed files with 71 additions and 47 deletions

View file

@ -1,18 +1,19 @@
{-# LANGUAGE TypeApplications #-}
module Main where module Main where
import Text.Printf (printf)
import qualified Day01 import qualified Day01
import qualified Day02
import qualified Day03 import qualified Day03
import Text.Printf (printf)
readDay :: Int -> IO String readDay :: Int -> IO String
readDay n = readFile $ getPath n readDay n = readFile $ getPath n
where where
getPath n = "../.inputs/" <> printf "%02d" n <> ".txt" getPath n = "../.inputs/" <> printf "%02d" n
main :: IO () main :: IO ()
main = do main = do
content <- readDay 3 content <- readDay 2
let parsed = Day03.parse content let parsed = Day02.parse content
print $ Day03.solve1 parsed print $ Day02.solveA parsed
print $ Day03.solve2 parsed print @Int $ Day02.solveB parsed

View file

@ -1,18 +1,15 @@
cabal-version: 3.0 cabal-version: 3.0
name: haskell name: aoc2015
version: 0.1.0.0 version: 0.1.0.0
synopsis: Advent of Code 2015 solutions synopsis: Advent of Code 2015 solutions
-- A longer description of the package. -- A longer description of the package.
-- description: -- description:
homepage: homepage:
-- A URL where users can report bugs. -- A URL where users can report bugs.
-- bug-reports: -- bug-reports:
license: MIT license: MIT
author: Maciej Jur author: Maciej Jur
maintainer: maciej@kamoshi.org maintainer: maciej@kamoshi.org
-- A copyright notice. -- A copyright notice.
-- copyright: -- copyright:
-- category: -- category:
@ -21,28 +18,26 @@ extra-source-files: CHANGELOG.md
library library
exposed-modules: exposed-modules:
Day01 Day01
Day02
Day03 Day03
-- Modules included in this library but not exported. -- Modules included in this library but not exported.
-- other-modules: -- other-modules:
-- LANGUAGE extensions used by modules in this package. -- LANGUAGE extensions used by modules in this package.
-- other-extensions: -- other-extensions:
build-depends: base ^>=4.16.4.0 build-depends: base >=4.16.4.0
hs-source-dirs: src hs-source-dirs: solutions
default-language: Haskell2010 default-language: Haskell2010
executable haskell executable haskell
main-is: Main.hs main-is: Main.hs
-- Modules included in this executable, other than Main. -- Modules included in this executable, other than Main.
-- other-modules: -- other-modules:
-- LANGUAGE extensions used by modules in this package. -- LANGUAGE extensions used by modules in this package.
-- other-extensions: -- other-extensions:
build-depends: build-depends:
base ^>=4.16.4.0, base >=4.16.4.0,
haskell aoc2015,
hs-source-dirs: app hs-source-dirs: app
default-language: Haskell2010 default-language: Haskell2010

View file

@ -1,10 +1,9 @@
module Day01 (parse, solve1, solve2) where module Day01 (parse, solve1, solve2) where
import Data.Char (isPrint) import Data.Char (isPrint)
import Data.List (scanl, findIndex) import Data.List (findIndex, scanl)
import Data.Maybe (fromJust) import Data.Maybe (fromJust)
parse :: String -> String parse :: String -> String
parse = filter isPrint parse = filter isPrint
@ -21,4 +20,3 @@ solve2 :: String -> Int
solve2 = fromJust . findIndex (< 0) . scanl reducer 0 solve2 = fromJust . findIndex (< 0) . scanl reducer 0
where where
reducer acc next = acc + mapper next reducer acc next = acc + mapper next

View file

@ -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