0

Advent of Code 2020

 2 years ago
source link: https://www.gkbrk.com/2020/12/advent-of-code-2020/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Hi. It looks like you are running Chrome or a Chromium-based browser.
For the future of the open web, please consider giving Firefox a try.

Advent of Code 2020

Reading time: about 1 minute

For this challenge, we need to find two numbers from our input that sum to 2020, and calculate their multiplication.

In [2]:
def mul(numbers):
    x = 1
    for n in numbers:
        x *= n
    return x
In [3]:
def day_1(nums, N = 2):
    for numbers in itertools.combinations(nums, N):
        if sum(numbers) == 2020:
            return mul(numbers)
In [4]:
day_1(read_numbers("day-1-example.txt"))
Out [4]:
514579
In [5]:
day_1(read_numbers("day-1.txt"))
Out [5]:
224436

Part Two

Part two uses the same input but wants the product of three numbers that add up to 2020.

In [6]:
day_1(read_numbers("day-1.txt"), 3)
Out [6]:
303394260

Citation

If you find this work useful, please cite it as:
@article{yaltirakli202012adventofcode2020,
  title   = "Advent of Code 2020",
  author  = "Yaltirakli, Gokberk",
  journal = "gkbrk.com",
  year    = "2020",
  url     = "https://www.gkbrk.com/2020/12/advent-of-code-2020/"
}
Not using BibTeX? Click here for more citation styles.

Comments

Comment

Name


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK