Advertisement
Armandur

6-12

Dec 6th, 2020
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. entries = []
  2. with open("6 - input", 'r') as file:
  3.     lines = file.readlines()
  4.     entry = []
  5.     for line in lines:
  6.         if line != "\n":
  7.             entry.append(line.strip("\n"))
  8.         if line == "\n" or line is lines[-1]:
  9.             entry.sort()
  10.             entries.append(entry)
  11.             entry = []
  12. print(entries)
  13.  
  14. concatEntries = []
  15. for entry in entries:
  16.     _entry = []
  17.     for person in entry:
  18.         for char in person:
  19.             _entry.append(char)
  20.     concatEntries.append(_entry)
  21.  
  22. print(concatEntries)
  23.  
  24. questionsSum = 0
  25. for entry in concatEntries:
  26.     questionsSum += len(set(entry))
  27.  
  28. print(questionsSum)
  29.  
  30.  
  31. # second part
  32. # sum all answers in each group that is equal to num of people
  33.  
  34. alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  35.  
  36.  
  37. numOfYesByAll = 0
  38. for entry in entries:   #   Check each group
  39.  
  40.     concatAnswer = []   #   Concat all answers in group to count occurences of chars
  41.     for answer in entry:
  42.         for char in answer:
  43.             concatAnswer.append(char)
  44.     concatAnswer.sort() #   Sort the string with all answers, because why not
  45.  
  46.     yesByAll = []
  47.     numPeople = len(entry)
  48.  
  49.     print(f"Checking group of {numPeople} people, with answers:\n{concatAnswer}")
  50.     for char in alphabet:
  51.         if concatAnswer.count(char) == numPeople:
  52.             print(f"'{char}' has correct num of occurrences: {numPeople}")
  53.             yesByAll.append(char)
  54.  
  55.     numOfYesByAll += len(yesByAll)
  56.     print(f"num of questions answered yes by all: {len(yesByAll)}")
  57.     print(f"Current sum of questions to which everyone answered yes: {numOfYesByAll}")
  58.     print()
  59.  
  60. print(numOfYesByAll)
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement