day 6
aoc 2020 » Devlog
chrome decided that my input was in Polish lmao, had to make sure I didn't end up with silence and and and and ... heartache
6a.py splits the input up into groups, and then for each group it takes the set of letters in each line, and takes the union over the group to give all letters that appear on at least one line.
# The form asks a series of 26 yes-or-no questions marked a through z. # All you need to do is identify the questions for which anyone in your group answers "yes". # input with open('6.txt', 'r') as file: input = file.read() # turn the input into a list, one element is one group input_list = list(input.split('\n\n')) # counter for result count = 0 # within each group # turn each line into a set of letters for group in input_list: # turn into a list of the string for each member memb_strs = group.split('\n') # this list will contain sets of questions answered memb_sets = [] for memb_str in memb_strs: # get a set of the questions answered memb_set = set(list(memb_str)) # add to list memb_sets.append(memb_set) # take the union over the group grp_set = set().union(*memb_sets) # add the size of this union to the count count += len(grp_set) # we now have the sum of sizes of all unions print(count)
6b.py is very similar - instead of getting all of the letters that appear in any line, you need the letters that appear in every line, so you get the intersection instead of the union. no idea why the syntax changed to set instead of set() though...
# The form asks a series of 26 yes-or-no questions marked a through z. # All you need to do is identify the questions for which everyone in your group answers "yes". # input with open('6.txt', 'r') as file: input = file.read() # turn the input into a list, one element is one group input_list = list(input.split('\n\n')) # counter for result count = 0 # within each group # turn each line into a set of letters for group in input_list: # turn into a list of the string for each member memb_strs = group.split('\n') # this list will contain sets of questions answered memb_sets = [] for memb_str in memb_strs: # get a set of the questions answered memb_set = set(list(memb_str)) # add to list memb_sets.append(memb_set) # take the intersection over the group grp_set = set.intersection(*memb_sets) # add the size of this intersection to the count count += len(grp_set) # we now have the sum of sizes of all intersections print(count)
Files
6.txt 17 kB
Dec 06, 2020
6a.py 1,012 bytes
Dec 06, 2020
6b.py 1 kB
Dec 06, 2020
Get aoc 2020
aoc 2020
not a mod, just some code
Status | Released |
Category | Other |
Author | riv |
Tags | advent-of-code, advent-of-code-2020, python |
Leave a comment
Log in with itch.io to leave a comment.