Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 28, 2023
1 parent 25071db commit ab8faa3
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions solutions/src/2023/07.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Main (main) where

import Advent (format, counts)
import Data.Foldable (toList)
import Data.List (sortOn, sort, elemIndex, nub)
import Data.List (sortOn, sortBy, elemIndex, nub)
import Data.Maybe (fromJust)

-- |
Expand Down Expand Up @@ -63,7 +63,7 @@ winnings strength input =
-- >>> strength1 "T55J5" < strength1 "QQQJA"
-- True
strength1 :: String -> [Int]
strength1 hand = category hand : map val hand
strength1 hand = category hand ++ map val hand
where
val x = fromJust (x `elemIndex` "23456789TJQKA")

Expand All @@ -81,19 +81,10 @@ strength2 hand =
[ category (map rpl hand)
| alt <- nub hand
, let rpl x = if x == 'J' then alt else x
] : map val hand
] ++ map val hand
where
val x = fromJust (x `elemIndex` "J23456789TQKA")

-- | Map a hand to an integer representing its set size.
category :: String -> Int
category hand =
case sort (toList (counts hand)) of
[5] -> 6
[1,4] -> 5
[2,3] -> 4
[1,1,3] -> 3
[1,2,2] -> 2
[1,1,1,2] -> 1
[1,1,1,1,1] -> 0
_ -> error "bad hand"
category :: String -> [Int]
category = sortBy (flip compare) . toList . counts

0 comments on commit ab8faa3

Please sign in to comment.