-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6_custom_customs.py
More file actions
64 lines (46 loc) · 1.81 KB
/
day6_custom_customs.py
File metadata and controls
64 lines (46 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Day 6 : Custom Customs
#
# Input : a list of group answer to questions for which people answered yes
# Goal : count the sum of number of question for whom anyone answered yes
import unittest
import re
from typing import List
from collections import Counter
def countQuestionAnswered(answers: str) -> int:
usedLetters = set(answers)
if '\n' in usedLetters: usedLetters.remove('\n')
return len(usedLetters)
def sumQuestionAnsweredInGroups(groupAnswers: str) -> int:
splittedGroups = re.split(r'\n\n', groupAnswers)
return sum([countQuestionAnswered(a) for a in splittedGroups])
def countQuestionEveryoneAnswered(answers: str) -> int:
groupSize = len(answers.strip().split('\n'))
lettersCount = Counter(answers)
del lettersCount['\n']
return len([ l for l in lettersCount if lettersCount[l] == groupSize])
def sumQuestionEveryoneAnsweredInGroups(groupAnswers: str) -> int:
splittedGroups = re.split(r'\n\n', groupAnswers)
return sum([countQuestionEveryoneAnswered(a) for a in splittedGroups])
sampleAnswers = """
abcx
abcy
abcz
"""
sampleGroupAnswers = """
abc
ab
"""
class SamplesTests(unittest.TestCase):
def test_part_one(self):
self.assertEqual(countQuestionAnswered(sampleAnswers), 6)
self.assertEqual(countQuestionAnswered(sampleGroupAnswers), 3)
self.assertEqual(sumQuestionAnsweredInGroups(sampleGroupAnswers), 5)
def test_part_two(self):
self.assertEqual(countQuestionEveryoneAnswered(sampleAnswers), 3)
self.assertEqual(sumQuestionEveryoneAnsweredInGroups(sampleGroupAnswers), 5)
unittest.main(argv=[''], verbosity=2, exit=False)
print("\n------\n")
with open("puzzle_inputs/day6.txt", "r") as f:
input = f.read()
print('Part 1:', sumQuestionAnsweredInGroups(input))
print('Part 2:', sumQuestionEveryoneAnsweredInGroups(input))