Another year, another Advent of Code. Though I kind of skipped last year. I think I may have been too busy or something. I do enjoy doing these puzzles, though they aren’t really “code projects”. Each day is just a puzzle, that is solved with math. Most of the problem, is figuring out what exactly is being asked. The other part is just, figuring out how to format the data set you are given. A lot of these are kind of repetitive, open your data file, read it in, convert it to a useful format, loop through the data, performing some action, and output some sort of total.
For this year’s theme, Santa’s Elves are taking a journey to a magical grove to collect food for the reindeer.
Day 1 – Elf Food Calories
The Problem Part 1:
The jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves’ expedition traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies. One important consideration is food – in particular, the number of Calories each Elf is carrying (your puzzle input).
The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they’ve brought with them, one item per line. Each Elf separates their own inventory from the previous Elf’s inventory (if any) by a blank line.
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.
The Problem Part 2
By the time you calculate the answer to the Elves’ question, they’ve already realized that the Elf carrying the most Calories of food might eventually run out of snacks.
To avoid this unacceptable situation, the Elves would instead like to know the total Calories carried by the top three Elves carrying the most Calories. That way, even if one of those Elves runs out of snacks, they still have two backups.
So, each day consists of a Part 1 and a Part 2. Generally speaking, each of these two parts is semi related, and can be completed with most of the same code. This one is simple enough, split the list up by lines, then split it into each elf’s calories, sort the list so the highest valies are at the front and print those three values out. Part 1 only needs the highest, Part 2 needs the next two.
with open("Day01Input.txt") as file: calories = file.read() calories = calories.replace("\n"," ") elves = [each.split(" ") for each in calories.split(" ")] totals =  for each in elves: total = 0 for i in each: total += int(i) totals.append(total) totals.sort(reverse=True) print(totals+totals+totals)
Josh Miller aka “Ramen Junkie”. I write about my various hobbies here. Mostly coding, photography, and music. Sometimes I just write about life in general. I also post sometimes about toy collecting and video games at Lameazoid.com.