haskell: 2015 02a
This commit is contained in:
parent
81e1d5792f
commit
fee1d10eb7
|
@ -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"
|
||||
|
||||
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
|
||||
|
|
|
@ -1,18 +1,15 @@
|
|||
cabal-version: 3.0
|
||||
name: haskell
|
||||
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
|
||||
|
||||
-- A copyright notice.
|
||||
-- copyright:
|
||||
-- category:
|
||||
|
@ -21,28 +18,26 @@ extra-source-files: CHANGELOG.md
|
|||
library
|
||||
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
|
||||
build-depends: base >=4.16.4.0
|
||||
hs-source-dirs: solutions
|
||||
default-language: Haskell2010
|
||||
|
||||
executable haskell
|
||||
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,
|
||||
haskell
|
||||
base >=4.16.4.0,
|
||||
aoc2015,
|
||||
|
||||
hs-source-dirs: app
|
||||
default-language: Haskell2010
|
||||
|
|
|
@ -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
|
||||
|
||||
|
@ -21,4 +20,3 @@ solve2 :: String -> Int
|
|||
solve2 = fromJust . findIndex (< 0) . scanl reducer 0
|
||||
where
|
||||
reducer acc next = acc + mapper next
|
||||
|
30
2015/haskell/solutions/Day02.hs
Normal file
30
2015/haskell/solutions/Day02.hs
Normal 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
|
Loading…
Reference in a new issue