DEV Community

Discussion on: AoC Day 1: Chronal Calibration

Collapse
 
r0f1 profile image
Florian Rohrer • Edited

I'd also suggest to use a context manager (the with keyword) for clean opening and closing of files.

Part 1:

with open("input.txt") as f:
    freq = sum(int(i.strip()) for i in f)
freq

Part 2:

from itertools import cycle

with open("input.txt") as f:
    freqs = [int(i.strip()) for i in f]

seen = set()
current = 0
for f in cycle(freqs):
    if current in seen:
        print(current)
        break
    else:
        seen.add(current)
        current += f

Also: "Hey checkout my Github Repo!"

Collapse
 
aspittel profile image
Ali Spittel

True! Always forget to do that since I really only do file handling for code challenges at this point.