aboutsummaryrefslogtreecommitdiffstats
path: root/2020/day06/custom-customs.py
diff options
context:
space:
mode:
Diffstat (limited to '2020/day06/custom-customs.py')
-rw-r--r--2020/day06/custom-customs.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/2020/day06/custom-customs.py b/2020/day06/custom-customs.py
new file mode 100644
index 0000000..d16e71b
--- /dev/null
+++ b/2020/day06/custom-customs.py
@@ -0,0 +1,47 @@
+
+from typing import Generator
+
+EXAMPLE = """ abc
+
+a
+b
+c
+
+ab
+ac
+
+a
+a
+a
+a
+
+b
+"""
+
+def unique(raw: str) -> Generator[set, None, None]:
+ for group in raw.split('\n\n'):
+ yield set(group.strip().replace('\n', ''))
+
+assert sum((len(g) for g in unique(EXAMPLE))) == 11
+
+with open('data/answers.txt') as f:
+ print(sum((len(g) for g in unique(f.read()))))
+
+# Part Two
+
+def everyone(raw: str) -> int:
+ total = 0
+ for chunk in raw.split('\n\n'):
+ group = chunk.strip().split('\n')
+ counts = {}
+
+ for per in group:
+ for q in per:
+ counts[q] = counts.get(q, 0) + 1
+ total += sum((len(group) == x for x in counts.values()))
+ return total
+
+assert everyone(EXAMPLE) == 6
+
+with open('data/answers.txt') as f:
+ print(everyone(f.read()))