How to solve Advent of Code 2022 - Day 1 with Python

How to solve Advent of Code 2022 - Day 1 with Python

Before I get into my solution of the day's challenge:

What even IS Advent of Code??!!

Good question, my friend. Here is this year's about page: https://adventofcode.com/2022/about and it says:

Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

If you're wondering why you should participate, read my Advent Of Code 2022 – 7 Reasons why you should participate article ;)

But chances are, if you're here, you already want to get going and see how the challenge is solved, so let's get going.

My GitHub Repository for Advent Of Code

https://github.com/GalaxyInfernoCodes/Advent_Of_Code_2022

I will upload all of my solutions there - in the form of Python (or Scala alternatives) notebooks. You have to use your own input for the "input.txt" since everyone gets their own and everyone needs to provide a different solution based on their input.

Locally, I use in VSCode as my IDE, but you can work with whatever you know best.

Day 1 Puzzle

In this blog post I'll describe the puzzle and solution steps, but if you want to jump ahead to said solution, you can do so here: https://github.com/GalaxyInfernoCodes/Advent_Of_Code_2022/blob/main/Day01_Python/AdventOfCode_Day01_Py.ipynb

Here is the challenge, if you want to read the full puzzle: https://adventofcode.com/2022/day/1

Part 1

Day 1 gives you an input text with integers in groups such as this:

1000
2000
3000

4000

5000
6000

7000
8000
9000

10000

These are the amount of calories each elf carries, separated by empty lines.

First thing is copy-pasting the input - first the example input for test purposes as "example.txt", then the real input that is different for each person - into a text file within the same folder. Then reading in each line:

with open('input.txt', 'r') as f:
    lines = f.readlines()
    calories = [entry.strip() for entry in lines]

The goal in the first part was to find the elf carrying the most calories. For that I created a list to save the calorie sums for each elf, and a variable for the current_sum I'm calculating. Then I looped over the entries adding them together until I reach the empty line. Then it gets appended to the list and the variable is reset to zero. At the end I need to add the last elf's calories, because there was no empty line afterwards in the example (if you copied empty lines at the end of your input, you should skip this last step).

elf_sums = []
current_sum = 0
for entry in calories:
    if entry != '':
        current_sum += int(entry)
    elif entry == '':
        elf_sums.append(current_sum)
        current_sum = 0
elf_sums.append(current_sum)

Then we get the solution with max(elf_sums) :)

Part 2

Because the calories of the top elf might not be enough, in part 2, we are supposed to add the calories of the top 3 elves.

For that I simply sorted the list of sums in descending order and added up the first 3 values:

elf_sums.sort(reverse=True)
elf_sums[0]+elf_sums[1]+elf_sums[2]

Done :)

Conclusion

Nice introductory challenge - but remember, if you were bored by this, the challenges will get increasingly more difficult, so I'm excited to see how far I can get :)