25 lines
761 B
Haskell
25 lines
761 B
Haskell
module Main where
|
|
|
|
import Data.Functor
|
|
|
|
data D = D { target :: Int, parts :: [Int]}
|
|
|
|
parse :: String -> [D]
|
|
parse = ((\(a:as) -> D (read $ takeWhile (/= ':') a) (read <$> as)) . words <$>) . lines
|
|
|
|
canBeTarget :: Int -> [Int -> Int -> Int] -> [Int] -> Bool
|
|
canBeTarget _ _ [] = False
|
|
canBeTarget t _ [x] = t == x
|
|
canBeTarget t o (x:y:xs) = any (canBeTarget t o . (:xs)) ((uncurry <$> o) <*> pure (x,y))
|
|
|
|
solve1 :: [D] -> Int
|
|
solve1 = sum . (target <$>) . filter (\(D t p) -> canBeTarget t [(*), (+)] p)
|
|
|
|
solve2 :: [D] -> Int
|
|
solve2 = sum . (target <$>) . filter (\(D t p) -> canBeTarget t [(*), (+), \a b -> read (show a ++ show b)] p)
|
|
|
|
main :: IO ()
|
|
main = readFile "inputs/7" <&> parse >>= \i ->
|
|
print (solve1 i)
|
|
>> print (solve2 i)
|