summary refs log tree commit diff stats
path: root/day5.py
diff options
context:
space:
mode:
Diffstat (limited to 'day5.py')
-rw-r--r--day5.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/day5.py b/day5.py
new file mode 100644
index 0000000..25c6bc4
--- /dev/null
+++ b/day5.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+
+from collections import Counter
+from itertools import product
+
+with open('day5.txt') as data:
+    strings = [line.strip() for line in data]
+
+def isNice(string):
+    count_letters = Counter(string)
+    if sum(count_letters[x] for x in 'aeiou') < 3:
+        return False
+    if any(map(lambda x: x in string, ['ab', 'cd', 'pq', 'xy'])):
+        return False
+    if not any(map(lambda x: x*2 in string, 'abcdefghijklmnopqrstuvwxyz')):
+        return False
+    return True
+
+nice_counts = Counter(map(isNice, strings))
+print(nice_counts[True])
+
+def isNice2(string):
+    def windows(size):
+        for i in range(len(string) - size + 1):
+            yield string[i:i+size]
+    if not any(window[0] == window[2] for window in windows(3)):
+        return False
+    pairs = map(lambda x: x[0] + x[1], product('abcdefghijklmnopqrstuvwxyz', repeat=2))
+    if not any(map(lambda x: string.count(x) >= 2, pairs)):
+        return False
+    return True
+
+nice2_counts = Counter(map(isNice2, strings))
+print(nice2_counts[True])