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!

64 Upvotes

1.2k comments sorted by

View all comments

1

u/ReplyNo7609 Dec 06 '20

hey there!

with AoC I am just learning python. I saw some of very amazing solutions, which i had to try step by step to even understand what is happening! :D

I would be glad if you could comment my code and help improving my skills. Thanks!

btw: with this code, you need two blank lines at the end of the input

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

dictionary={}
alphabet=list(string.ascii_lowercase)

for each in alphabet:
    dictionary[each]=0

#-----------------------------Part1-------------------------------
yescounts=[]
for line in data:
    if line=='':
        yescounts.append(sum(list(dictionary.values())))
        for x in dictionary:
            dictionary[x]=0
            continue
    else:
        for letter in line:
            if [letter in dictionary]:
                dictionary[letter]=1
    
print('Answer Part 1: %d' % sum(yescounts))      

#-----------------------------Part2-------------------------------


def checkyes(dic, n):
    count=0
    if n>0:
        for each in (list(dic.values())):
            #print(each)
            if each==n:
                count+=1
        return count
    else:
        return 0


yescounts=[]
people=0
for each in data:
    if each=='':
        yescounts.append(checkyes(dictionary, people))
        people=0
        for x in dictionary:
            dictionary[x]=0
            continue
    else:
        people+=1
        for every in each:
            if [every in dictionary]:
                dictionary[every]+=1
                
print('Answer Part 2: %d' % sum(yescounts))

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]

1

u/daggerdragon Dec 06 '20

Please follow the posting guidelines and edit your post to add what language(s) you used. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.