haskell: 2015 04
This commit is contained in:
parent
227b01bd0f
commit
637232a196
|
@ -20,12 +20,22 @@ library
|
||||||
Day01
|
Day01
|
||||||
Day02
|
Day02
|
||||||
Day03
|
Day03
|
||||||
|
Day04
|
||||||
|
|
||||||
|
default-extensions:
|
||||||
|
ImportQualifiedPost
|
||||||
|
OverloadedStrings
|
||||||
|
|
||||||
-- 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,
|
||||||
|
bytestring >=0.10.10.0,
|
||||||
|
cryptohash-md5 >=0.11.101.0,
|
||||||
|
text >=2.1.1,
|
||||||
|
|
||||||
hs-source-dirs: solutions
|
hs-source-dirs: solutions
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
||||||
|
@ -36,9 +46,8 @@ executable haskell
|
||||||
-- LANGUAGE extensions used by modules in this package.
|
-- LANGUAGE extensions used by modules in this package.
|
||||||
-- other-extensions:
|
-- other-extensions:
|
||||||
build-depends:
|
build-depends:
|
||||||
base >= 4.16.4.0,
|
|
||||||
text >= 2.1.1,
|
|
||||||
aoc2015,
|
aoc2015,
|
||||||
|
base >=4.16.4.0,
|
||||||
|
|
||||||
hs-source-dirs: app
|
hs-source-dirs: app
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
26
2015/haskell/solutions/Day04.hs
Normal file
26
2015/haskell/solutions/Day04.hs
Normal 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
|
Loading…
Reference in a new issue