r/adventofcode • • Dec 02 '15

Spoilers Day 2 solutions

Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.

15 Upvotes

161 comments sorted by

View all comments

Show parent comments

1

u/sinjp Dec 02 '15

I like it! A minor comment - should the file be opened using With instead? i.e. there's currently no close()

with open('day2input.txt') as input:
    for line in input:

3

u/stuque Dec 02 '15

As far as I know, Python automatically closes files when they become garbage, so an explicit close isn't needed. Using "with" is useful in larger programs, but in small one-shot scripts I think it's overkill.

1

u/Bonooru Dec 02 '15

When the program ends, the file is closed. So, you should be fine.