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

View file

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

View file

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

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