with open("input.txt") as f: l = [int(x.strip()) for x in f] for i, n1 in enumerate(l): for j, n2 in enumerate(l[i+1:]): if n1 + n2 == 2020: part1 = n1 * n2 for n3 in l[i+j+1:]: if n1 + n2 + n3 == 2020: part2 = n1 * n2 * n3 print(part1) print(part2)
Here, I didn’t quite get the second line x.strip for x in f
It’s a list comprehension expressions.
What it does is to read every line from the file handle
f, stores the line in the variable
x, strip leading and trailing space off of the line, and convert the (stripped) line to an
int values are collected into a single list, which is assigned to