January 01, 2025

Solution for Advent of Code 2018 - Day 1: Chronal Calibration

Link to the puzzle text

Part 1

In this puzzle, we have a list of numbers. Starting with a base of zero, we should add all the numbers in the list and return the final result.

We can get the total sum by using sum(list).

Part 2

In part 2, we should add the numbers one at a time, repeating the original list multiple times if necessary. We should return the first repeated number.

To save the previoulsy seen numbers, we used a set. For repeating the list infinitely times, the itertools.cycle function was used. The rest was straight forward:

freqs = set()
freq = 0
for l in itertools.cycle(frequencies):
freq += l
if freq in freqs:
return freq
freqs.add(freq)

Link to my solutions

No comments:

Post a Comment