re: AoC Day 1: Chronal Calibration VIEW POST

TOP OF THREAD FULL DISCUSSION
re: So excited about this 🙌🏻! Python Part 1 data = open('input.txt', 'r') total = 0 for line in data: total += int(line) print(total...
 

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!"

 

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

code of conduct - report abuse