Hacker News new | ask | show | jobs
by wodenokoto 1992 days ago

    def day1_1(nums):
        "Find 2 distinct numbers that sum to 2020, and return their product."
        return first(x * y 
                     for x in nums 
                     for y in nums & {2020 - x} 
                     if x != y)
`nums` is a set of integers `nums & {2020 - x}` Finds all the numbers that are in the set of `nums` and the set of `{2020 - x}` - this basically extracts a number, `y`, for which `x+y = 2020`.

So for each x in nums, he extracts a number from nums that, added to x, equals 2020. If there is such a number y, he checks if it is the same as x, and only if it is different, does he yield in his generator, as the product of y and x.

It's succint, but not exactly readable, imho.