Spoiler alert
This is a full solution for Advent of Code 2026, day 1 puzzles with explanations.
If you want to try to solve it yourself first, head over to https://adventofcode.com/2026/day/1 and give it a go and then come back and compare notes.
You can find the full code at https://github.com/Hamatti/adventofcode-2026/blob/main/src/day_1.py
Christmas is coming 🎄! But oh no, there’s a shortage of parts for the presents at Korvatunturi (the real home of Santa, y’all) due to plane issues so it’s time to bring out trains. Who doesn’t love trains? I do!
We have 12 days to gather up supplies and deliver them to Santa’s village so the elves can make all the presents for all the good kids in the world.
My goal with these explanations is to help readers learn programming — especially Python — and a bit of puzzle solving. I won’t start from scratch so to follow along, you do need to have a basic understanding of how to write Python and how to run Python programs on your computer.
Read input
Since I’ve been doing Advent of Code for quite a few years now, I’ve built a small toolkit of tools for myself, mainly to help deal with input data parsing and debugging.
Since Advent of Code pretty much always relies on line-by-line input data, my basic data reading is two-fold.
In utils.py, I have a read_input function that accepts the day number, a function to be executed on each line of the input and a boolean flag for whether to read the example input (if True) or the actual input (if False). I hate spending my best brain power every day on writing data parsing code and debugging it when it goes wrong and I realise it too late.
def read_input[T](
day: int, map_fn: Callable[[str], T] = str, example: bool = False
) -> List[T]:
"""
Read puzzle input for file `../inputs/day_{day}.txt'
and apply map_fn function to each line.
If `example` is set to True, read file `../inputs/day_{day}_example.txt`
instead
:param day Which day's puzzle input to read
:param map_fn Cast each line into new type
:param example Read example input?
:returns Day's input processed into the desired format
"""
try:
if example:
filename = f"day_{day}_example.txt"
else:
filename = f"day_{day}.txt"
with open(os.path.join("..", "inputs", filename)) as input_file:
return [map_fn(line.strip()) for line in input_file]
except FileNotFoundError as e:
print(e)
sys.exit(1)The second part is what I always write into my daily solution files: a mapper function that tells the read_input function what to do with each line.
Since today, we’ll be reading in numbers (container ids) and later need to do math on those numbers, my mapping function converts all input lines into integers:
def mapper(line: str) -> int:
"""
The input is a number on each
line and we need to do math on those
numbers so let's parse it into an
integer.
eg. "74" -> 74
"""
return int(line)This way of reading the input doesn’t always map perfectly to the inputs of Advent of Code but they solve a large portion of the problems and on days when it doesn’t, I can work around it.
Part 1
In the first part, we get a list of container IDs and need to figure out which 3 are most commonly appearing in the list. We then need to multiply the IDs together to get our result.
There are many ways to solve this puzzle. Python is a wonderful language with a great standard library and at the end, I’ll show how you can utilise that library to solve the problem with the code almost reading the same as the puzzle description.
But what if you don’t know all that and immediately recognise the elegant, built-in solution to this puzzle. I’ll do my best to put myself into a position of someone new to programming.
We start by reading in our data
from utils import read_input
def part_1() -> int:
container_ids = read_input(1, mapper)Whenever you need to keep track of counts of items, a dictionary is a great data structure. Its Python’s key→value store so we can use the IDs as keys and count how many we’ve seen as values.
counts = {}
for container_id in container_ids:
if id not in counts:
counts[id] = 1
else:
counts[id] += 1Here, we go through every container_id in the list and either add it to the dictionary if it doesn’t exist or we increment its value by one.
This if/else structure is so common when working with dictionaries in Python that there’s a special dictionary — defaultdict — implemented to help clean up the code and make it easier to read. We could implement the above as
from collections import defaultdict
counts = defaultdict(int)
for container_id in container_ids:
counts[container_id] += 1We initialise defaultdict with int to tell it that if we try to access a key that doesn’t exist, it should default to 0.
Now, if we run this with the example input, we have a dictionary that looks like this:
{57: 6, 55: 1, 76: 4, 78: 3, 45: 2, 43: 5}As Advent of Code is technology-agnostic, we could open up a calculator and calculate the result by hand by finding the most common ones from the dictionary with our eyes. But let’s continue with Python.
We can now sort this dictionary by the counts by utilising sorted:
sorted_counts = sorted(counts.items(), key=lambda container: container[1])Since we need access to the container IDs (keys) and sort by the counts (values), we need to give sorted both and we can access those in dictionary with .items() method that returns a list of tuples with keys and values. The key parameter tells the sorting function what to sort by: in this case, the second item in the tuple, ie. the count.
To get the 3 most common one, we look at the three last items in the sorted list with
top_3 = sorted_counts[-3:]In Python, we can slice lists with the square bracket notation and colons. [-3:] means “start with the third last and get everything until the end”.
We get a list of three container IDs and their counts:
[(533, 6), (271, 6), (272, 7)]To calculate the result, we then multiply the IDs together:
puzzle_solution = 1
for container_id, count in top_3:
puzzle_solution *= container_id
return puzzle_solutionAll of this is such a common case that there’s a special class for Python to take care of all of this: Counter. It’s a subclass of dictionary that can be created from a list and it does not only the counting for you but also gives you the most common items:
def part_1() -> int:
"""
Our input is a list of container IDs
and we need to find three most common
IDs in the list.
Our result is the product of these three
IDs.
"""
ids = read_input(1, mapper)
counts = Counter(ids)
top_3 = counts.most_common(3)
puzzle_solution = 1
for container_id, _ in top_3:
puzzle_solution *= container_id
return puzzle_solutionPart 2
The second part is almost exactly the same as the first one but this time we’re interested in the three least common IDs.
There is no least_common method in Counter so instead, we use most_common to get a sorted list of all counts and then slice with the negative index to get the last 3 items.
def part_2() -> int:
"""
Our input is a list of container IDs
and we need to find three least common
IDs in the list.
Our result is the product of these three
IDs.
"""
container_ids = read_input(1, mapper)
counts = Counter(container_ids)
least_common = counts.most_common()[-3:]
puzzle_solution = 1
for container_id, _ in least_common:
puzzle_solution *= container_id
return puzzle_solutionOne habit I got into years ago is adding a block at the end of the file to run to check the results:
if __name__ == "__main__":
part_1_result = part_1()
print(f"Part 1: {part_1_result}")
assert part_1_result == "<confirmed correct result>"
part_2_result = part_2()
print(f"Part 2: {part_2_result}")
assert part_2_result == "<confirmed correct result>"Once I get a result that is confirmed correct by Advent of Code website, I put that result into the assertion and this way, I can safely refactor and improve my code and every time I make a change, I can run the code again and the assertion will catch if I made a mistake.
Get in touch
If you want to comment on these solutions, I share them in Mastodon where you can look for the post for the right date or you can start a discussion over email with juhis@hamatti.org.
Follow via RSS
I have created a custom RSS feed for these solutions so you can follow along and not miss any of them!