haskell: 2015 02a
This commit is contained in:
parent
81e1d5792f
commit
fee1d10eb7
|
@ -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
|
||||||
|
|
|
@ -1,48 +1,43 @@
|
||||||
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:
|
||||||
extra-source-files: CHANGELOG.md
|
extra-source-files: CHANGELOG.md
|
||||||
|
|
||||||
library
|
library
|
||||||
exposed-modules:
|
exposed-modules:
|
||||||
Day01
|
Day01
|
||||||
Day03
|
Day02
|
||||||
|
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: solutions
|
||||||
hs-source-dirs: src
|
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.
|
||||||
|
-- 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.
|
hs-source-dirs: app
|
||||||
-- other-modules:
|
default-language: Haskell2010
|
||||||
|
|
||||||
-- 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
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
@ -18,7 +17,6 @@ solve1 :: String -> Int
|
||||||
solve1 = sum . map mapper
|
solve1 = sum . map mapper
|
||||||
|
|
||||||
solve2 :: String -> Int
|
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
|
||||||
|
|
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