Advent of Code 2022 Day 1

📬 Get the future posts directly in your inbox:

2022 is the first year I am participating in Advent of Code. I am using this repository to store my solutions.

For solving the puzzles, I will be working with with Python and trying to solve all the solutions using standard Python libraries. The code is stored in the 2022 directory and each solution is stored in day{number}.py files. For those of you that want to replicate them, you can create a new environment and use the dependencies that are stored in requirements.txt and can be installed using pip install -r requirements.txt.

For maintaining code quality I am using pre-commit hooks. The hooks are stored in .pre-commit-config.yaml and can be installed using pre-commit install.

Here is the first problem:

This list represents the Calories of the food carried by five Elves

In case the Elves get hungry and need extra snacks, they need to know which Elf to ask: they’d like to know how many Calories are being carried by the Elf carrying the most Calories. In the example above, this is 24000 (carried by the fourth Elf).

Find the Elf carrying the most Calories. How many total Calories is that Elf carrying?


⚠️ Warning

Do not keep reading if you are participating in Advent of Code and do not want to see the solution.

Part 1

First, to get the data I used the aocd library and stored the data for the first challenge in a variable:

import aocd

data = aocd.get_data(day=1, year=2022)

The data was stored as a string, I parsed it by splitting the lines between data entries, and stored it in a list of lists, where I converted and stored each entire as an integer.

elves_food = [[int(food) for food in elves.splitlines()] for elves in data.split("\n\n")]

Finally for getting the maximum number of calories I got the maximum of the sum of lists:

print("Max food calories: ", max(sum(elf) for elf in elves_food))

Part 2

The second part of the challenge was to find the three Elves who have the most calories in their food, and sum the calories count to get the total of calories by the top three Elves.

To solve this part one can sum all the calories by Elf and then sort the list by the sum, or one can use a Heap queue, that was the way I went for. It was a nice way to get to know about Heap queues.

The solution is as follows:

import heapq 

top_three = heapq.nlargest(3, elves_food, key=sum)

print("Total food calories: ", sum(sum(elf) for elf in top_three))

📬 Get the blog posts directly in your inbox:

💬 Join the conversation:

Keep to conversation with a comment or reach out in my social networks.