|
|
@ -3,17 +3,23 @@ module Day1 (fixExpenseReport) where |
|
|
|
import qualified Data.IntSet as DIS |
|
|
|
import qualified Data.IntSet as DIS |
|
|
|
import Data.Maybe (mapMaybe) |
|
|
|
import Data.Maybe (mapMaybe) |
|
|
|
|
|
|
|
|
|
|
|
yearToFind :: Int |
|
|
|
type Target = Int |
|
|
|
yearToFind = 2020 |
|
|
|
type Expenses = [Int] |
|
|
|
|
|
|
|
type Expenses' = DIS.IntSet |
|
|
|
|
|
|
|
|
|
|
|
-- Turn the list into an IntSet. For each element in the set, look up the |
|
|
|
-- Turn the list into an IntSet. For each element in the set, look up the |
|
|
|
-- number in the set to make it 2020. If found, return the multiplication of |
|
|
|
-- number in the set to make it 2020. If found, return the multiplication of |
|
|
|
-- the two numbers. |
|
|
|
-- the two numbers. |
|
|
|
fixExpenseReport :: [Int] -> Int |
|
|
|
fixExpenseReport :: Target -> Int -> Expenses -> Maybe Int |
|
|
|
fixExpenseReport expenses = |
|
|
|
fixExpenseReport t c es |
|
|
|
let s = DIS.fromList expenses in |
|
|
|
| c <= 0 = error "unsupported" |
|
|
|
head $ mapMaybe (`findOther` s) expenses |
|
|
|
| otherwise = fixExpenseReport' t c (DIS.fromList es) |
|
|
|
|
|
|
|
|
|
|
|
findOther :: Int -> DIS.IntSet -> Maybe Int |
|
|
|
fixExpenseReport' :: Target -> Int -> Expenses' -> Maybe Int |
|
|
|
findOther n s = let n' = yearToFind - n in |
|
|
|
fixExpenseReport' t 1 es = if DIS.member t es then Just t else Nothing |
|
|
|
if DIS.member n' s then Just (n*n') else Nothing |
|
|
|
fixExpenseReport' t c es = |
|
|
|
|
|
|
|
let foo x = (*x) <$> fixExpenseReport' (t-x) (c-1) (DIS.delete x es) |
|
|
|
|
|
|
|
bar = mapMaybe foo (DIS.toList es) in |
|
|
|
|
|
|
|
case bar of |
|
|
|
|
|
|
|
[] -> Nothing |
|
|
|
|
|
|
|
(x:_) -> Just x |