Safe Haskell | None |
---|
Day01
Description
General - List combination/search problem. Build all combinations of list elements. And look for the one that sums up to be 2020.
Given that +
is commutative, we only need to build [(1,2), (1,3),
(2,3)] out of [1, 2, 3]. And we can stop looking as soon as we find
the first tuple that satisfies the condition (making the evaluation
of the list (of tuples) lazy (build only the part of the list you
need to find the first matching tuple) is another optimisation to
consider).
Another nice variation on the challenge would be to think about how to implement combinationsN. But that is for another day ...
Part 1 - Do it with pairs.
Part 2 - Do it with triplets.
Synopsis
- type Expense = Int
- input :: String -> [Expense]
- combinations2 :: [a] -> [(a, a)]
- combinations3 :: [a] -> [(a, a, a)]
- part1 :: [Expense] -> Int
- part2 :: [Expense] -> Int