r/adventofcode • • Dec 06 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 06 Solutions -🎄-

NEW AND NOTEWORTHY


Advent of Code 2020: Gettin' Crafty With It

  • UNLOCKED! Go forth and create, you beautiful people!
  • Full details and rules are in the Submissions Megathread
  • Make sure you use one of the two templates!
  • Or in the words of AoC 2016: USING A TEMPLATE IS MANDATORY

--- Day 06: Custom Customs ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:04:35, megathread unlocked!

66 Upvotes

1.2k comments sorted by

View all comments

Show parent comment

3

u/Dagur Dec 06 '20

It's always a good idea to make sure that a file isn't open longer than needed so I recommend to do either

with open('input.txt', 'r') as inputfile:
    data = [x.strip() for x in inputfile.readlines()]

or

data = [x.strip() for x in open('input.txt', 'r').readlines()]

Of course it doesn't matter in tiny scripts like AoC but it's a good rule to follow. https://docs.python.org/3/tutorial/inputoutput.html#tut-files

continue does nothing where you're using them

if [every in dictionary]:

The brackets mean you get [True] or [False] which is interpreted as a non-empty list so this is always true.

2

u/frerich Dec 06 '20

inputfile.readlines()]

Even nicer: instead of calling `readlines()` you can just iterate a file object directly to get the lines:

data = [line.rstrip() for line in inputfile]