DEV Community

Discussion on: Advent of Code 2020 Solution Megathread - Day 1: Report Repair

Collapse
 
aspittel profile image
Ali Spittel • Edited

Hey!! So excited that it's this time again here is my code. I would have just done loops without sets but the servers were down when I was trying to get my full input so I just went ahead and did a tiny optimization :).

def part_one(nums, set_nums):
    for num in nums:
        if 2020 - num in set_nums:
            return num * (2020 - num)


def part_two(nums, set_nums):
    for num in nums:
        for num2 in nums:
            if (2020 - num - num2) in set_nums:
                return num * (2020 - num - num2) * num2


with open("input.txt") as _file:
    nums = [int(line) for line in _file]
    set_nums = set(nums)

    print("Part 1", part_one(nums, set_nums))
    print("Part 2", part_two(nums, set_nums))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rpalo profile image
Ryan Palo

Oooh! That's a really slick way to drop out a level of nesting. I like it!