Wordle comparison: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Used bimap to eliminate a couple of redundant name bindings)
Line 147: Line 147:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List (intercalate, mapAccumL)
<lang haskell>import Data.Bifunctor (bimap)
import Data.List (intercalate, mapAccumL)
import qualified Data.Map.Strict as M
import qualified Data.Map.Strict as M
import Data.Maybe (fromMaybe)
import Data.Maybe (fromMaybe)
Line 157: Line 158:
wordleScore :: String -> String -> [Int]
wordleScore :: String -> String -> [Int]
wordleScore target guess =
wordleScore target guess =
snd $
let (residue, matches) =
uncurry (mapAccumL amber) $
bimap charCounts (zip guess) $
mapAccumL green [] (zip target guess)
mapAccumL green [] (zip target guess)
in snd $
mapAccumL
amber
(charCounts residue)
(zip guess matches)


green :: String -> (Char, Char) -> (String, Int)
green :: String -> (Char, Char) -> (String, Int)
Line 172: Line 170:
amber :: Tally -> (Char, Int) -> (Tally, Int)
amber :: Tally -> (Char, Int) -> (Tally, Int)
amber tally (_, 2) = (tally, 2)
amber tally (_, 2) = (tally, 2)
amber tally (c, 0)
amber tally (c, _)
| 0 < fromMaybe 0 (M.lookup c tally) =
| 0 < fromMaybe 0 (M.lookup c tally) =
(M.adjust pred c tally, 1)
(M.adjust pred c tally, 1)