Refactor Haskell day 6 to look a bit more like Rust version
This commit is contained in:
parent
3daa7a1ad1
commit
5a6727ba05
@ -14,13 +14,12 @@ puzzle =
|
|||||||
, parts =
|
, parts =
|
||||||
( sum
|
( sum
|
||||||
. uncurry (zipWith applyToList)
|
. uncurry (zipWith applyToList)
|
||||||
. second (transpose . map (map (digitsToInt @Int . catMaybes) . filter notNull . splitOn [Nothing]))
|
. second (transpose . map (map (digitsToInt @Int) . filter notNull . groupJusts))
|
||||||
)
|
)
|
||||||
/\ ( sum
|
/\ ( sum
|
||||||
. uncurry (zipWith applyToList)
|
. uncurry (zipWith applyToList)
|
||||||
. second
|
. second
|
||||||
( map catMaybes
|
( groupJusts
|
||||||
. splitOn [Nothing]
|
|
||||||
. map (\l -> if all isNothing l then Nothing else Just $ digitsToInt @Int $ catMaybes l)
|
. map (\l -> if all isNothing l then Nothing else Just $ digitsToInt @Int $ catMaybes l)
|
||||||
. transpose
|
. transpose
|
||||||
)
|
)
|
||||||
@ -31,13 +30,16 @@ puzzle =
|
|||||||
|
|
||||||
data Op = Add | Multiply
|
data Op = Add | Multiply
|
||||||
deriving (Eq, Ord, Show, Enum, Bounded, Generic, NFData)
|
deriving (Eq, Ord, Show, Enum, Bounded, Generic, NFData)
|
||||||
apply :: Op -> Int -> Int -> Int
|
apply :: (Num a) => Op -> a -> a -> a
|
||||||
apply = \case
|
apply = \case
|
||||||
Add -> (+)
|
Add -> (+)
|
||||||
Multiply -> (*)
|
Multiply -> (*)
|
||||||
unit :: Op -> Int
|
unit :: (Num a) => Op -> a
|
||||||
unit = \case
|
unit = \case
|
||||||
Add -> 0
|
Add -> 0
|
||||||
Multiply -> 1
|
Multiply -> 1
|
||||||
applyToList :: Op -> [Int] -> Int
|
applyToList :: (Num a) => Op -> [a] -> a
|
||||||
applyToList op = foldl' (apply op) (unit op)
|
applyToList op = foldl' (apply op) (unit op)
|
||||||
|
|
||||||
|
groupJusts :: (Eq a) => [Maybe a] -> [[a]]
|
||||||
|
groupJusts = map catMaybes . splitOn [Nothing]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user