aoc/app/7.hs

25 lines
761 B
Haskell
Raw Normal View History

2024-12-02 10:51:49 +00:00
module Main where
import Data.Functor
data D = D { target :: Int, parts :: [Int]}
2024-12-02 10:51:49 +00:00
parse :: String -> [D]
parse = ((\(a:as) -> D (read $ takeWhile (/= ':') a) (read <$> as)) . words <$>) . lines
2024-12-02 10:51:49 +00:00
2024-12-07 11:03:45 +00:00
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
2024-12-07 11:03:45 +00:00
solve1 = sum . (target <$>) . filter (\(D t p) -> canBeTarget t [(*), (+)] p)
solve2 :: [D] -> Int
2024-12-07 11:03:45 +00:00
solve2 = sum . (target <$>) . filter (\(D t p) -> canBeTarget t [(*), (+), \a b -> read (show a ++ show b)] p)
2024-12-02 10:51:49 +00:00
main :: IO ()
main = readFile "inputs/7" <&> parse >>= \i ->
print (solve1 i)
>> print (solve2 i)