Skip to content

Commit 4e33825

Browse files
committed
Use length . group . sort to find counts
Source: https://www.reddit.com/r/adventofcode/comments/18cnzbm/comment/kcfm7jh/
1 parent 95f428a commit 4e33825

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

07.hs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Data.List (sortOn, nub, elemIndex, delete, minimumBy)
1+
import Data.List (sortOn, elemIndex, delete, minimumBy, sort, group)
22
import Data.Maybe (fromJust)
33
import Data.Ord (Down(Down))
44
import Data.Function (on)
@@ -19,19 +19,14 @@ data HandType = Five | Four | Full | Three | Two | One | High
1919
deriving (Show, Eq, Ord)
2020

2121
handType :: Hand -> HandType
22-
handType (s, _) = case (length . nub) s of
23-
1 -> Five
24-
2 -> if maxCardCount s == 4 then Four else Full
25-
3 -> if maxCardCount s == 3 then Three else Two
26-
4 -> One
27-
5 -> High
28-
29-
maxCardCount :: String -> Int
30-
maxCardCount = maximum . map snd . cardCounts
31-
32-
cardCounts :: String -> [(Char, Int)]
33-
cardCounts = foldl (\as c -> incr as c : as) [] where
34-
incr as c = case lookup c as of Nothing -> (c, 1); Just i -> (c, i + 1)
22+
handType (s, _) = case (sort . map length . group . sort) s of
23+
[5] -> Five
24+
[1,4] -> Four
25+
[2,3] -> Full
26+
[1,1,3] -> Three
27+
[1,2,2] -> Two
28+
[1,1,1,2] -> One
29+
_ -> High
3530

3631
handValue :: [Char] -> Hand -> Int
3732
handValue labelStrength (s, _) =

0 commit comments

Comments
 (0)