-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.hs
49 lines (36 loc) · 1 KB
/
07.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import Data.Bifunctor
import Data.List
import Data.Maybe
import Lib
type Card = Int
type Hand = ([Card], Int)
type Input = [Hand]
main :: IO ()
main = aoc 2023 7 setup solve1 solve2 ["1"]
solve1 :: Input -> Int
solve1 = sum . zipWith (*) [1 ..] . map snd . sortOn sortKey
solve2 :: Input -> Int
solve2 = solve1 . map (first replaceJokers)
replaceJokers :: [Card] -> [Card]
replaceJokers = map ((*) <*> fromEnum . (/= 11))
sortKey :: Hand -> [Int]
sortKey = fork (:) ty id . fst
ty :: [Card] -> Int
ty = ty . count . filter (/= 0)
where
ty [] = 6
ty [_] = 6
ty [1, _] = 5
ty [2, _] = 4
ty [1, 1, _] = 3
ty [1, 2, _] = 2
ty [1, 1, 1, _] = 1
ty _ = 0
count :: (Eq a) => [a] -> [Int]
count = sort . (map . (.) length . flip elemIndices <*> nub)
setup :: String -> Input
setup = map parseHand . lines
parseHand :: String -> Hand
parseHand = bimap parseCards read . listToTuple . words
parseCards :: String -> [Card]
parseCards = map ((+ 2) . fromJust . (`elemIndex` "23456789TJQKA"))