Skip to content
advent of code 2018

day 01

chronal calibration

approach

the frequency changes are read as a list of ints. part 1 is just their sum, written as a functools.reduce over addition.

part 2 keeps a set of every frequency seen so far, starting with 0, and walks the list over and over with i % len(changes), returning the first running total that is already in the set.

the trick

wrap the index with a modulo so one loop cycles the list forever, and stop at the first repeat in the set.

python
def part2():
	s={0}
	freq=0
	i=0
	while True:
		freq+=changes[i%len(changes)]
		if freq in s:
			return freq
		s.add(freq)
		i+=1
	return None