Explore Books related to Python. **Click Here**

I decided to give the Advent of Code challenge a try this year. Advent of Code is a series of challenges that are given once a day. You can solve them in any programming language that you want. Once you have the solution, you enter it on the website to earn points. In this year's case, you are earning stars. You can earn two stars per day.

Note: Advent of Code does not run your code. It doesn't know what programming language you are using. It only cares about how quickly you answer the questions after the challenge is released. If you happen to answer the questions within 5 minutes of the challenge being posted, you will get a better score than those who answer it 5 hours after the challenge appears.

I don't know if I will complete all the challenges, or even if I can. If the challenges end up taking too much time, then I will have to drop them as I have lots of other projects I need to work on. But for the ones that I do complete, I will write about them here. If you are working through the challenges yourself and you don't want to see my answers, you should STOP reading!

Each day is split into two problems. The problem is detailed here. The first part of the problem is that you are given a file with one number per line. You are you find the t**wo numbers that when added, equal 2020**. Then take those two numbers and multiply them to get the answer.

My first thought when I saw this problem was to use Python's itertools library, which has a handy combinations function.

Using that, I came up with the following code:

from itertools import combinations result = [pair for pair in combinations(numbers, 2) if sum(pair) == 2020]

This gave me the pair of numbers I wanted, although the output looked like this: [(number_1, number_2)]

I extracted the two numbers from this and multiplied them to get the answer to the first part of the Advent question.

The next part of the problem is that now they want you to take the same input, but find the only** three numbers that add up to 2020**. Then multiply those numbers to get the answer.

You can modify **combinations **to look for more than the two number I had hard-coded above. You just pass it 3 instead of 2.

To make the code smarter, I also imported Python's **math** module and used its **prod()** function to multiple the result.

Here is the full code:

import math from itertools import combinations def get_answer(numbers: list[int], combos: int = 2) -> int: result = [pair for pair in combinations(numbers, combos) if sum(pair) == 2020] multiplied = math.prod(result[0]) print(f"The answer is {multiplied}") return multiplied if __name__ == '__main__': with open('input.txt') as f: numbers = [int(item.strip()) for item in f.readlines()] get_answer(numbers, combos=3)

All my code is also on Github and some of the answers include unit tests. I have some very rudimentary tests for this challenge up there now.

Copyright © 2022 Mouse Vs Python | Powered by Pythonlibrary