haskell: 2015 04

This commit is contained in:
Maciej Jur 2024-08-04 15:06:49 +02:00
parent 227b01bd0f
commit 637232a196
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
2 changed files with 38 additions and 3 deletions

View file

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

View file

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