from collections import Counter
from Common import time_function, get_lines
"""
--- Day 6: Custom Customs ---

As your flight approaches the regional airport where you'll switch to a much larger plane, customs declaration forms are distributed to the passengers.

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". Since your group is just you, this doesn't take very long.

However, the person sitting next to you seems to be experiencing a language barrier and asks if you can help. For each of the people in their group, you write down the questions for which they answer "yes", one per line. For example:

abcx
abcy
abcz

In this group, there are 6 questions to which anyone answered "yes": a, b, c, x, y, and z. (Duplicate answers to the same question don't count extra; each question counts at most once.)

Another group asks for your help, then another, and eventually you've collected answers from every group on the plane (your puzzle input). Each group's answers are separated by a blank line, and within each group, each person's answers are on a single line. For example:

abc

a
b
c

ab
ac

a
a
a
a

b

This list represents answers from five groups:

    The first group contains one person who answered "yes" to 3 questions: a, b, and c.
    The second group contains three people; combined, they answered "yes" to 3 questions: a, b, and c.
    The third group contains two people; combined, they answered "yes" to 3 questions: a, b, and c.
    The fourth group contains four people; combined, they answered "yes" to only 1 question, a.
    The last group contains one person who answered "yes" to only 1 question, b.

In this example, the sum of these counts is 3 + 3 + 3 + 1 + 1 = 11.

For each group, count the number of questions to which anyone answered "yes". What is the sum of those counts?

--- Part Two ---

As you finish the last group's customs declaration, you notice that you misread one word in the instructions:

You don't need to identify the questions to which anyone answered "yes"; you need to identify the questions to which everyone answered "yes"!

Using the same example as above:

abc

a
b
c

ab
ac

a
a
a
a

b

This list represents answers from five groups:

    In the first group, everyone (all 1 person) answered "yes" to 3 questions: a, b, and c.
    In the second group, there is no question to which everyone answered "yes".
    In the third group, everyone answered yes to only 1 question, a. Since some people did not answer "yes" to b or c, they don't count.
    In the fourth group, everyone answered yes to only 1 question, a.
    In the fifth group, everyone (all 1 person) answered "yes" to 1 question, b.

In this example, the sum of these counts is 3 + 0 + 1 + 1 + 1 = 6.

For each group, count the number of questions to which everyone answered "yes". What is the sum of those counts?

"""
# part 1
class group:
    def __init__(self) -> None:
        self.questions = []

    def add_question(self, string:str):
        if string not in self.questions:
            self.questions.append(string)

    def get_count(self):
        return len(self.questions)

def sum_of_questions():
    g = group()
    count = 0
    for line in lines:
        if line == "":
            count += g.get_count()
            g = group()
        else:
            for ch in line:
                g.add_question(ch)

    count += g.get_count()
    return count


# part 2
class Person:
    def __init__(self) -> None:
        self.questions = []

    def add_question(self, string:str):
        if string not in self.questions:
            self.questions.append(string)

    def get_questions(self) -> list:
        return self.questions

class new_group:
    def __init__(self) -> None:
        self.people = []
    
    def add_person(self, person: Person) -> None:
        self.people.append(person)
    
    def get_all_questions(self) -> list:
        new_list = []
        for person in self.people:
            new_list.extend(person.get_questions())
        return new_list
    
    def get_count(self) -> int:
        counter = Counter(self.get_all_questions())
        sum = 0
        for char in counter:
            if counter.get(char) == len(self.people):
                sum+=1
        return sum


def sum_of_questions_2():
    

    g = new_group()
    p = Person()
    count = 0

    for line in lines:
        line = line.strip()
        if line == "":
            count += g.get_count()
            g = new_group()
        else:
            for ch in line:
                p.add_question(ch)
            g.add_person(p)
            p = Person()

    count+=g.get_count()

    return count


if __name__ == "__main__":
    lines = get_lines("Files/input_Day6.txt")
    print(sum_of_questions()) # 6297
    print(sum_of_questions_2()) # 3158

    # # 0.003639436
    # print(time_function(func=sum_of_questions, iterations=100))
    # # 0.006841989
    # print(time_function(func=sum_of_questions_2, iterations=100))
