Advent of Code Day 1

Advent of Code calendar

Solution to day 1 of Advent of Code https://adventofcode.com/2018/day/1

Day 1 part 1

Part one is simple list comprehension and sum:

In [1]:
with open('datafiles/aoc1input.txt','r') as f:
    print(sum([int(i) for i in f.readlines()]))
543

Day 1 part 2

In part two we can make use of the interesting "cycle" iterator function, that repeats the elements of an iterator over and over.

In [2]:
from itertools import cycle

with open(r'datafiles/aoc1input.txt','r') as file:
    seen_frequencies = set([0])
    current_frequency = 0
    for delta in cycle([int(i) for i in file.readlines()]):
        current_frequency += delta
        if current_frequency in seen_frequencies:
            print(current_frequency)
            break
        else:
            seen_frequencies.add(current_frequency)
621

links

social