From 637232a1965c44a17c4856d6e2f41d6a1c650a05 Mon Sep 17 00:00:00 2001 From: Maciej Jur Date: Sun, 4 Aug 2024 15:06:49 +0200 Subject: [PATCH] haskell: 2015 04 --- 2015/haskell/haskell.cabal | 15 ++++++++++++--- 2015/haskell/solutions/Day04.hs | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 2015/haskell/solutions/Day04.hs diff --git a/2015/haskell/haskell.cabal b/2015/haskell/haskell.cabal index b35249b..8bee55d 100644 --- a/2015/haskell/haskell.cabal +++ b/2015/haskell/haskell.cabal @@ -20,12 +20,22 @@ library Day01 Day02 Day03 + Day04 + + default-extensions: + ImportQualifiedPost + OverloadedStrings -- 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 + build-depends: + base >=4.16.4.0, + bytestring >=0.10.10.0, + cryptohash-md5 >=0.11.101.0, + text >=2.1.1, + hs-source-dirs: solutions default-language: Haskell2010 @@ -36,9 +46,8 @@ executable haskell -- LANGUAGE extensions used by modules in this package. -- other-extensions: build-depends: - base >= 4.16.4.0, - text >= 2.1.1, aoc2015, + base >=4.16.4.0, hs-source-dirs: app default-language: Haskell2010 diff --git a/2015/haskell/solutions/Day04.hs b/2015/haskell/solutions/Day04.hs new file mode 100644 index 0000000..820195e --- /dev/null +++ b/2015/haskell/solutions/Day04.hs @@ -0,0 +1,26 @@ +module Day04 (solveA, solveB) where + +import Crypto.Hash.MD5 (hash) +import Data.Bifunctor (second) +import Data.ByteString (ByteString) +import Data.ByteString qualified as BS +import Data.ByteString.Char8 qualified as Char8 +import Text.Printf (printf) + +hashHex :: String -> String +hashHex = concatMap (printf "%02x") . BS.unpack . hash . Char8.pack + +test :: String +test = "ckczppom" + +solve :: Int -> String -> Integer +solve zeros key = fst . head . filter (isMatch . snd) . map toHash $ [1 ..] + where + toHash n = (n, hashHex $ key <> show n) + isMatch = all (== '0') . take zeros + +solveA :: String -> Integer +solveA = solve 5 + +solveB :: String -> Integer +solveB = solve 6