-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.hs
36 lines (24 loc) · 948 Bytes
/
02.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
import Data.List
import Data.List.Split
import Lib
type CubeCounts = [Int]
type Game = [CubeCounts]
type Input = [Game]
main :: IO ()
main = aoc 2023 2 setup solve1 solve2 ["1"]
solve1 :: Input -> Int
solve1 = sum . map fst . filter (and . zipWith (>=) [12, 13, 14] . snd) . zip [1 ..] . map minConfig
solve2 :: Input -> Int
solve2 = sum . map (product . minConfig)
minConfig :: Game -> CubeCounts
minConfig = foldl1 (zipWith max)
setup :: String -> Input
setup = map parseGame . lines
parseGame :: String -> Game
parseGame = map parseCubeCounts . splitOn "; " . last . splitOn ": "
parseCubeCounts :: String -> CubeCounts
parseCubeCounts = collectCounts . map (fork (curry id) (read . head) last . words) . splitOn ", "
collectCounts :: [(Int, String)] -> CubeCounts
collectCounts = flip map ["red", "green", "blue"] . flip getCount
getCount :: String -> [(Int, String)] -> Int
getCount = (.) (maybe 0 fst) . (find . ((. snd) . (==)))